I use symfony's DI without symfony framework.
This is part of composer.json
"require" : {
"doctrine/orm" : "*",
"doctrine/migrations" : "^1.5",
"gedmo/doctrine-extensions" : "*",
"symfony/dependency-injection" : "3.4.17",
"symfony/config" : "3.4.17",
"symfony/yaml" : "3.4.17",
"twig/twig" : "^2.0",
"symfony/form": "^3.4",
"symfony/validator": "^3.4",
"symfony/doctrine-bridge": "^3.4",
"symfony/serializer": "^3.4",
"ramsey/uuid": "^3.8",
"ramsey/uuid-doctrine": "^1.5",
"monolog/monolog": "^1.24",
"wazaari/monolog-mysql": "^1.0",
"twilio/sdk": "^5.28",
"mobiledetect/mobiledetectlib": "^2.8",
"symfony/console": "~3.4.23",
"guzzlehttp/guzzle": "^6.3",
"lcobucci/jwt": "^3.2",
"symfony/cache": "^3.4",
"symfony/finder": "^3.4",
"beberlei/DoctrineExtensions": "^1.1",
"symfony/http-foundation": "^3.4",
"sinergi/browser-detector": "^6.1",
"mpdf/mpdf": "~8.0",
"symfony/proxy-manager-bridge": "^3.4"
},
This is how DI container is built
if (file_exists($containerFile)) {
require_once($containerFile);
$container = new \ProjectServiceContainer();
} else {
$container = new ContainerBuilder();
$container->setProxyInstantiator(new RuntimeInstantiator());
$loader = new YamlFileLoader($container, new FileLocator(__DIR__));
$loader->load(__DIR__.'/../config/services.yml');
$container->compile();
$dumper = new PhpDumper($container);
$containerDir = dirname($containerFile);
file_put_contents($containerFile, $dumper->dump());
}
In config/services.yml I've marked service as lazy
App\Services\MyService:
public: true
lazy: true
When I run some command
for the first time it makes proxy object for MyService
, when I run some command
for the second time it takes MyService
from cache/container.php
and it is not lazy.
Part of generated cache/container.php
protected function getMyServiceService($lazyLoad = true)
The actual problem is that $lazyLoad
variable is not used at all inside this function so constructor of MyService
is called, but should be delayed, because some command
does not use methods of this service at all.