inviqa/ symfony-container-generator
此包已弃用且不再维护。未建议替换包。
此包的最新版本(1.2.0)没有可用的许可证信息。
通用Symfony DI容器构建器
1.2.0
2017-05-18 13:23 UTC
Requires
- php: >=5.3.3
- symfony/config: ~2.3|~3.0
- symfony/dependency-injection: ~2.3|~3.0
- symfony/filesystem: ~2.3|~3.0
Requires (Dev)
- behat/behat: ^3.0
- bossa/phpspec2-expect: ^1.0
- phpspec/phpspec: ^2.2
This package is not auto-updated.
Last update: 2024-05-25 14:15:56 UTC
README
symfony-container-generator
生成并缓存独立的Symfony DI容器。您可以使用此工具轻松地在任何旧项目中使用Symfonys DI容器,您只需要提供存储DI容器配置的文件夹列表,无论是调试模式还是生产模式,以及您的配置文件格式。
用法
创建一个新的配置对象
$generatorConfiguration = ContainerTools\Configuration::fromParameters( '/var/cache/container.php', // name of file for compiled container ['/etc/services/', '/project/services'], // where to expect services.xml and services_test.xml true | // debug mode - caches container.php with meta file and only regenerates when resources are modified false, // production mode - caches container.php and doesn't regenerate unless deleted 'xml'|'yml' // services extension 'xml' or 'yml' );
实例化一个ContainerGenerator,并从中获取容器
$generator = new \ContainerTools\ContainerGenerator($generatorConfiguration); $container = $generator->getContainer(); $mailer = $container->get('acme.mailer');
ContainerGenerator至少期望存在一个services.xml文件,如果找不到,将抛出异常。
如果您需要定义延迟服务,请安装ProxyManager Bridge包
composer require symfony/proxy-manager-bridge
测试服务
在测试环境中,有时需要提供模拟服务以替换真实服务,这些服务可以在services_test.xml中提供,并使用配置切换
$generatorConfiguration->setTestEnvironment(true);
如果存在任何service_test.xml文件,它们将被随后加载。Symfony的配置加载器将合并配置,并用生产服务定义覆盖测试服务定义。(对于配置文件,如果配置了'yml',则类似地适用于services.yml)
从版本0.3.0开始,ContainerGenerator使用ConfigCache组件,该组件跟踪资源,并且仅在资源被修改时重新生成缓存的容器。这意味着如果启用了调试模式,将在缓存的容器文件夹中生成一个名为/var/cache/container.php.meta
的文件。