Beispiel #1
0
 public function setUp()
 {
     $reader = new \Doctrine\Common\Annotations\AnnotationReader();
     $driver = new \Doctrine\ORM\Mapping\Driver\AnnotationDriver($reader);
     $config = new \Doctrine\ORM\Configuration();
     $config->setMetadataCacheImpl(new \Doctrine\Common\Cache\ArrayCache());
     $config->setQueryCacheImpl(new \Doctrine\Common\Cache\ArrayCache());
     $config->setProxyDir(sys_get_temp_dir());
     $config->setProxyNamespace('SimpleThings\\EntityAudit\\Tests\\Proxies');
     $config->setMetadataDriverImpl($driver);
     $conn = array('driver' => $GLOBALS['DOCTRINE_DRIVER'], 'memory' => $GLOBALS['DOCTRINE_MEMORY'], 'dbname' => $GLOBALS['DOCTRINE_DATABASE'], 'user' => $GLOBALS['DOCTRINE_USER'], 'password' => $GLOBALS['DOCTRINE_PASSWORD'], 'host' => $GLOBALS['DOCTRINE_HOST']);
     $auditConfig = new AuditConfiguration();
     $auditConfig->setCurrentUsername("beberlei");
     $auditConfig->setAuditedEntityClasses($this->auditedEntities);
     $auditConfig->setGlobalIgnoreColumns(array('ignoreme'));
     $this->auditManager = new AuditManager($auditConfig);
     $this->auditManager->registerEvents($evm = new EventManager());
     if (php_sapi_name() == 'cli' && isset($_SERVER['argv']) && (in_array('-v', $_SERVER['argv']) || in_array('--verbose', $_SERVER['argv']))) {
         $config->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger());
     }
     $this->em = \Doctrine\ORM\EntityManager::create($conn, $config, $evm);
     $schemaTool = new \Doctrine\ORM\Tools\SchemaTool($this->em);
     $em = $this->em;
     try {
         $schemaTool->createSchema(array_map(function ($value) use($em) {
             return $em->getClassMetadata($value);
         }, $this->schemaEntities));
     } catch (\Exception $e) {
         if ($GLOBALS['DOCTRINE_DRIVER'] != 'pdo_mysql' || !($e instanceof \PDOException && strpos($e->getMessage(), 'Base table or view already exists') !== false)) {
             throw $e;
         }
     }
 }
 public function setUp()
 {
     $reader = new \Doctrine\Common\Annotations\AnnotationReader();
     $driver = new \Doctrine\ORM\Mapping\Driver\AnnotationDriver($reader);
     $config = new \Doctrine\ORM\Configuration();
     $config->setMetadataCacheImpl(new \Doctrine\Common\Cache\ArrayCache());
     $config->setQueryCacheImpl(new \Doctrine\Common\Cache\ArrayCache());
     $config->setProxyDir(sys_get_temp_dir());
     $config->setProxyNamespace('SimpleThings\\EntityAudit\\Tests\\Proxies');
     $config->setMetadataDriverImpl($driver);
     $conn = array('driver' => 'pdo_sqlite', 'memory' => true);
     $auditConfig = new AuditConfiguration();
     $auditConfig->setCurrentUsername("beberlei");
     $auditConfig->setAuditedEntityClasses(array('SimpleThings\\EntityAudit\\Tests\\ArticleAudit', 'SimpleThings\\EntityAudit\\Tests\\UserAudit'));
     $auditConfig->setGlobalIgnoreColumns(array('ignoreme'));
     $this->auditManager = new AuditManager($auditConfig);
     $this->auditManager->registerEvents($evm = new EventManager());
     #$config->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger());
     $this->em = \Doctrine\ORM\EntityManager::create($conn, $config, $evm);
     $schemaTool = new \Doctrine\ORM\Tools\SchemaTool($this->em);
     $schemaTool->createSchema(array($this->em->getClassMetadata('SimpleThings\\EntityAudit\\Tests\\ArticleAudit'), $this->em->getClassMetadata('SimpleThings\\EntityAudit\\Tests\\UserAudit')));
 }
 public function finish(ContainerInterface $container)
 {
     //finishing locales
     $container->extend('locale.manager', function (LocaleManager $localeManager, ContainerInterface $container) {
         $locales = $container->getParameter('framework.locales');
         foreach ($locales['locales'] as $id => $locale) {
             $localeObject = new Locale();
             $localeObject->setId($id);
             $localeObject->setTitle($locale['title']);
             $localeObject->setActive($locale['active']);
             $localeObject->setCountry($locale['country']);
             $localeObject->setProperties($locale['properties']);
             $localeManager->addLocale($localeObject);
         }
         foreach ($locales['detectors'] as $detector) {
             $localeManager->addDetector($container[$detector]);
         }
         foreach ($locales['storage'] as $storage) {
             $localeManager->addStorage($container[$storage]);
         }
         $localeManager->setCurrent($locales['current']);
         return $localeManager;
     });
     //entity audit
     $container['entity_audit.configuration'] = function (ContainerInterface $container) {
         $config = $container->getParameter('framework.doctrine_audit');
         $configuration = new AuditConfiguration();
         $configuration->setAuditedEntityClasses($config['entities']);
         $configuration->setGlobalIgnoreColumns($config['ignore_columns']);
         $configuration->setRevisionTableName('su_' . $configuration->getRevisionTableName());
         $container->getEventDispatcher()->addListener(AuthController::TOKEN_CHANGE_EVENT, function () use($container, $configuration) {
             $context = $container->getSecurityContext();
             if ($context->getToken() && $context->getToken()->getUser()) {
                 $configuration->setCurrentUsername($context->getToken()->getUser()->getUsername());
             }
         });
         if (!$configuration->getCurrentUsername()) {
             $configuration->setCurrentUsername('anonymous');
         }
         return $configuration;
     };
     $container['entity_audit.manager'] = function (ContainerInterface $container) {
         $config = $container['entity_audit.configuration'];
         return new AuditManager($config);
     };
     //finishing doctrine
     $doctrineConfig = $container->getParameter('framework.doctrine');
     //let's believe that types are needed always
     foreach ($doctrineConfig['types'] as $definition) {
         list($name, $class) = $definition;
         Type::addType($name, $class);
     }
     foreach ($doctrineConfig['type_overrides'] as $definition) {
         list($name, $class) = $definition;
         Type::overrideType($name, $class);
     }
     foreach ($doctrineConfig['event_managers'] as $name => $managerDefinition) {
         $container['doctrine.event_managers.' . $name] = function (ContainerInterface $container) use($managerDefinition) {
             $manager = new EventManager();
             foreach ($managerDefinition['subscribers'] as $id) {
                 $manager->addEventSubscriber($container[$id]);
             }
             $container['entity_audit.manager']->registerEvents($manager);
             return $manager;
         };
     }
     $application = $container->getApplication();
     foreach ($doctrineConfig['configurations'] as $name => $configurationDefinition) {
         $container['doctrine.configurations.' . $name] = function (ContainerInterface $container) use($configurationDefinition, $application) {
             //loading package directories
             $packages = $application->getPackages();
             $paths = array();
             foreach ($packages as $package) {
                 $entityDir = $application->locatePackageRoot($package) . DIRECTORY_SEPARATOR . 'Entity';
                 if (is_dir($entityDir)) {
                     $paths[] = $entityDir;
                 }
             }
             $configuration = Setup::createAnnotationMetadataConfiguration($paths, $container->getParameter('debug'), $container->getParameter('directories.cache') . DIRECTORY_SEPARATOR . 'doctrine');
             if ($container->getParameter('debug')) {
                 $logger = $container['logger.doctrine'];
                 $container['doctrine.logger']->addLogger($logger);
                 $configuration->setSQLLogger($container['doctrine.logger']);
             }
             //Foo:Bar -> \FooPackage\Entity\Bar aliases
             foreach ($packages as $package) {
                 $class = get_class($package);
                 $namespace = substr($class, 0, strrpos($class, '\\')) . '\\Entity';
                 $configuration->addEntityNamespace($application->resolveName($package), $namespace);
             }
             return $configuration;
         };
     }
     foreach ($doctrineConfig['connections'] as $name => $connectionDefinition) {
         $container['doctrine.connections.' . $name] = function (ContainerInterface $container) use($connectionDefinition) {
             if ($connectionDefinition['driver'] != 'mysql') {
                 throw new \Exception('No driver is supported currently but mysql');
             }
             $connection = new Connection(array('host' => $connectionDefinition['host'], 'user' => $connectionDefinition['user'], 'password' => $connectionDefinition['password'], 'dbname' => $connectionDefinition['dbname'], 'charset' => $connectionDefinition['charset']), new PDOMySql\Driver(), $container['doctrine.configurations.' . $connectionDefinition['configuration']], $container['doctrine.event_managers.' . $connectionDefinition['event_manager']]);
             return $connection;
         };
     }
     foreach ($doctrineConfig['entity_managers'] as $name => $entityManagerDefinition) {
         $container['doctrine.entity_managers.' . $name] = function (ContainerInterface $container) use($name, $entityManagerDefinition, $doctrineConfig) {
             $ormConfigurationName = $entityManagerDefinition['configuration'];
             $ormConfiguration = $container['doctrine.configurations.' . $ormConfigurationName];
             $em = EntityManager::create($container['doctrine.connections.' . $entityManagerDefinition['connection']], $ormConfiguration, $container['doctrine.event_managers.' . $entityManagerDefinition['event_manager']]);
             // @DEV, remove
             $em->name = $name;
             foreach ($doctrineConfig['configurations'][$ormConfigurationName]['hydrators'] as $hydratorDefinition) {
                 list($name, $class) = $hydratorDefinition;
                 $reflection = new \ReflectionClass($class);
                 $hydrator = $reflection->newInstanceArgs(array($em));
                 $ormConfiguration->addCustomHydrationMode($name, $hydrator);
             }
             return $em;
         };
     }
     $container['doctrine.doctrine'] = function (ContainerInterface $container) use($doctrineConfig) {
         $connections = array();
         foreach (array_keys($doctrineConfig['connections']) as $name) {
             $connections[$name] = 'doctrine.connections.' . $name;
         }
         $managers = array();
         foreach (array_keys($doctrineConfig['entity_managers']) as $name) {
             $managers[$name] = 'doctrine.entity_managers.' . $name;
         }
         //todo: make default em/con configurable
         return new ManagerRegistry('supra.doctrine', $connections, $managers, $doctrineConfig['default_connection'], $doctrineConfig['default_entity_manager'], 'Doctrine\\ORM\\Proxy\\Proxy');
     };
     //sessions and HttpFoundation
     $sessionConfig = $container->getParameter('framework.session');
     $container['http.session'] = function (ContainerInterface $container) use($sessionConfig) {
         if (PHP_SAPI == 'cli') {
             throw new \Exception('Sessions are not possible in CLI mode');
         }
         $storage = $container[$sessionConfig['storage']];
         $session = new Session($storage);
         $session->start();
         $container['http.request']->setSession($session);
         return $session;
     };
     //mailers
     $mailerConfig = $container->getParameter('framework.swiftmailer');
     $container->setParameter('mailer.mailers', array_map(function ($value) {
         return 'mailer.mailers.' . $value;
     }, array_keys($mailerConfig['mailers'])));
     foreach ($mailerConfig['mailers'] as $id => $configurationDefinition) {
         $container['mailer.mailers.' . $id] = function (ContainerInterface $container) use($configurationDefinition) {
             switch ($configurationDefinition['transport']) {
                 case 'smtp':
                     $transport = \Swift_SmtpTransport::newInstance($configurationDefinition['params']['host'], $configurationDefinition['params']['port']);
                     $transport->setUsername($configurationDefinition['params']['username']);
                     $transport->setPassword($configurationDefinition['params']['password']);
                     break;
                 case 'mail':
                     $transport = \Swift_MailTransport::newInstance();
                     if (isset($transport['params']['extra_params'])) {
                         $transport->setExtraParams($transport['params']['extra_params']);
                     }
                     break;
                 case 'sendmail':
                     $transport = \Swift_SendmailTransport::newInstance();
                     if (isset($configurationDefinition['params']['command'])) {
                         $transport->setCommand($configurationDefinition['params']['command']);
                     }
                     break;
                 case 'null':
                     $transport = \Swift_NullTransport::newInstance();
                     break;
                 default:
                     throw new \Exception(sprintf('Unknown mail transport [%s].', $configurationDefinition['transport']));
             }
             return \Swift_Mailer::newInstance($transport);
         };
     }
     $container['mailer.mailer'] = function (ContainerInterface $container) use($mailerConfig) {
         return $container['mailer.mailers.' . $mailerConfig['default']];
     };
 }