Created
August 25, 2011 06:37
-
-
Save makasim/1170108 to your computer and use it in GitHub Desktop.
symfony2 DI how to compile only doctrine bundle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Foo; | |
//use Doctrine\ORM\Configuration; | |
use Doctrine\ORM\EntityManager; | |
use Symfony\Component\DependencyInjection\ContainerBuilder; | |
use Symfony\Bundle\DoctrineBundle\DependencyInjection\DoctrineExtension; | |
use Symfony\Component\DependencyInjection\Dumper\PhpDumper; | |
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag; | |
class FooClass | |
{ | |
public static function get() | |
{ | |
$parameterBag = new ParameterBag(array( | |
'kernel.debug' => true, | |
'kernel.root_dir' => '/path/to/root/dir', | |
'kernel.cache_dir' => sys_get_temp_dir(), | |
'kernel.environment' => 'test', | |
'kernel.bundles' => array(), | |
)); | |
$doctrineExtenstion = new DoctrineExtension(); | |
$container = new ContainerBuilder($parameterBag); | |
$container->registerExtension($doctrineExtenstion); | |
$container->loadFromExtension($doctrineExtenstion->getAlias(), array( | |
'dbal' => array( | |
'default_connection' => 'default', | |
'connections' => array( | |
'default' => array( | |
'driver' => 'pdo_sqlite', | |
'path' => ':memory:' | |
), | |
), | |
), | |
'orm' => array( | |
'auto_generate_proxy_classes' => true, | |
'proxy_namespace' => 'Proxies', | |
'proxy_dir' => sys_get_temp_dir(), | |
'default_entity_manager' => 'default', | |
'entity_managers' => array( | |
'default' => array( | |
'connection' => 'default', | |
'mappings' => array(), | |
'class_metadata_factory_name' => 'Doctrine\ORM\Mapping\ClassMetadataFactory', | |
'metadata_cache_driver' => 'array', | |
'query_cache_driver' => 'array', | |
), | |
), | |
), | |
)); | |
$container->compile(); | |
// cache the container | |
$dumper = new PhpDumper($container); | |
$content = $dumper->dump(array('class' => 'FooContainer', 'base_class' => 'Container')); | |
$filePath = tempnam(sys_get_temp_dir(), 'foo'); | |
file_put_contents($filePath, $content); | |
require_once $filePath; | |
return new \FooContainer(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment