This config file is from a Symfony 6.4 application:
<?php
namespace SymfonyComponentDependencyInjectionLoaderConfigurator;
return static function (ContainerConfigurator $containerConfig): void
{
$scrapePatterns = [
'default' => [
'regex' => '/([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+.[a-zA-Z0-9_-]+)/i',
'first_only' => false,
'cancel' => false,
],
];
$templates = [
'header' => 'Some header',
'footer' => 'Some footer',
];
$containerConfig->parameters()->set(
'my.sample_configs',
[
'scrape_patterns' => $scrapePatterns,
'templates' => $templates,
]
);
};
How can I read those definition arrays from a different PHP app that does not use Symfony at all?
I don’t have the ContainerConfigurator
class… I was wondering if I can create my own class with that name and use it to get the values.
A few notes about my (admittedly hacky) question:
- I’m not looking for answers explaining better ways to put the problem, although I fully understand they exist; I am more interested in the actual hacky challenge as described here.
- I can make changes to the Symfony config file as long as the real Symfony app is still able to load it correctly.
- one of the things that is confusing me is that, due to my technical limitations, I don’t understand how Symfony uses that
return static function
, so it’s hard for me to know how to invoke this, or even the limits of what can be done with this code. I think it’s done here, though I am not certain.
3
After understanding what Symfony was doing to run the function, I am able to answer my own question. Not as hard as I thought.
I change the code like this:
<?php
namespace SymfonyComponentDependencyInjectionLoaderConfigurator;
return static function (ContainerConfigurator $containerConfig = null): array {
$scrapePatterns = [
'default' => [
'regex' => '/([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+.[a-zA-Z0-9_-]+)/i',
'first_only' => false,
'cancel' => false,
],
];
$templates = [
'header' => 'Some header',
'footer' => 'Some footer',
];
$configs = [
'scrape_patterns' => $scrapePatterns,
'templates' => $templates,
];
if ($containerConfig) {
$containerConfig->parameters()->set('my.sample_configs', $configs);
}
return $configs;
};
The changes are:
- allow the function argument to be missing (null)
- return an array instead of void
- check if
$containerConfig
exists before using it (it exists when called from Symfony, doesn’t exist when called from elsewhere) - return the arrays I want (Symfony doesn’t seem to have any problems with this)
Then my code on the other app only needs this:
$fn = include 'path/to/the/symfony/configs/processor_configs.php';
if ($fn)
$ret = $fn();
include
, just like require
, doesn’t just execute the code, it evaluates the code. This means that the code results in a value you can store in a variable. In this case, that return static function
makes the included code return a function (a closure), which can then be invoked.
This way I can get the arrays on my app, and Symfony is also able to successfully load that same configuration file.
0