/**
  * Reads configuration from file.
  * @param  string  file name
  * @return array
  */
 public function loadFromFile($file)
 {
     $loader = new Loader();
     $res = $loader->load($file);
     $container = $this->compiler->getContainerBuilder();
     foreach ($loader->getDependencies() as $file) {
         $container->addDependency($file);
     }
     return $res;
 }
Example #2
0
 public function loadConfiguration()
 {
     $container = $this->getContainerBuilder();
     $config = $this->getConfig($this->defaults);
     $entities = $collections = array();
     foreach ($config['entity'] as $domain => $entity) {
         $service = $container->addDefinition($entities[$domain] = $this->prefix($domain . '.entity'));
         Compiler::parseService($service, $entity);
         $service->setClass('NiceORM\\Entity')->setParameters(array('data' => NULL))->setShared(FALSE);
         $container->addDefinition($collections[$domain] = $this->prefix($domain . '.collection'))->setClass('NiceORM\\ICollection')->setFactory('NiceORM\\TableCollection', array($domain, '%data%'))->setParameters(array('data'))->setShared(FALSE);
     }
     $fields = $refs = $related = array();
     foreach ($config['tables'] as $domain => $table) {
         $fields[$domain] = $refs[$domain] = $related[$domain] = array();
         foreach (Arrays::get($config['fields'], $domain, array()) as $name => $column) {
             $fields[$domain][is_int($name) ? $column : $name] = $column;
         }
         foreach (Arrays::get($config['relations'], $domain, array()) as $name => $column) {
             if (is_int($name)) {
                 $refs[$domain][$column] = array($column, NULL, $column);
             } else {
                 $column = array_values((array) $column);
                 switch (count($column)) {
                     case 0:
                         $refs[$domain][$name] = array($name, NULL, $name);
                         break;
                     case 1:
                         $refs[$domain][$name] = array($name, $column[0], $name);
                         break;
                     case 2:
                         $refs[$domain][$name] = array($column[0], $column[1], $name);
                         break;
                     default:
                         $refs[$domain][$name] = array($column[0], $column[1], $column[2]);
                 }
             }
         }
     }
     foreach ($refs as $domain => $tableRefs) {
         foreach ($tableRefs as $name => $ref) {
             list($table, $column, $type) = $ref;
             if ($type === $name || empty($related[$type][$domain . 's'])) {
                 $related[$type][$domain . 's'] = array($config['tables'][$domain], $column, $domain);
             } elseif (!isset($refs[$domain][$type])) {
                 unset($related[$type][$domain . 's']);
             }
             $related[$type][$domain . 's:' . $name] = array($config['tables'][$domain], $column, $domain);
         }
     }
     $mappers = $accessors = array();
     foreach ($config['tables'] as $domain => $table) {
         $mapper = $container->addDefinition($mappers[$domain] = $this->prefix($domain . '.mapper'))->setClass('NiceORM\\ActiveRowMapper', array($domain, $table, $fields[$domain], $refs[$domain], $related[$domain]));
         $container->addDefinition($accessors[$domain] = $this->prefix($domain . '.accessor'))->setClass('NiceORM\\ActiveRowAccessor', array('%data%', $mapper))->setParameters(array('data'))->setShared(FALSE);
     }
     $entityFactory = $container->addDefinition($this->prefix('entityFactory'))->setClass('NiceORM\\DIEntityFactory', array($entities));
     $collectionFactory = $container->addDefinition($this->prefix('collectionFactory'))->setClass('NiceORM\\DICollectionFactory', array($collections));
     $accessorFactory = $container->addDefinition($this->prefix('accessorFactory'))->setClass('NiceORM\\DIAccessorFactory', array($accessors));
     $mapperAccessor = $container->addDefinition($this->prefix('mapperAccessor'))->setClass('NiceORM\\DIMapperAccessor', array($mappers));
     $container->addDefinition($this->prefix('manager'))->setClass('NiceORM\\Manager')->setFactory('NiceORM\\Manager', array($entityFactory, $collectionFactory, $accessorFactory, $mapperAccessor));
 }
Example #3
0
 /**
  * Processes configuration data
  *
  * @throws \Nette\InvalidStateException
  */
 public function loadConfiguration()
 {
     $this->verifyDoctrineVersion();
     $config = $this->getConfig($this->defaults);
     $builder = $this->getContainerBuilder();
     // process custom doctrine services
     \Nette\Config\Compiler::parseServices($builder, $config, $this->name);
     $cache = $builder->addDefinition($this->prefix('cache'))->setClass('Nella\\Doctrine\\Cache', array('@cacheStorage'));
     if ($config['debugger'] === NULL) {
         $config['debugger'] = $builder->parameters['debugMode'];
     }
     if ($config['proxy']['autogenerate'] === NULL) {
         $config['proxy']['autogenerate'] = $builder->parameters['debugMode'];
     }
     $evm = $builder->addDefinition($this->prefix('eventManager'))->setClass('Doctrine\\Common\\EventManager')->addSetup(get_called_class() . '::setupEventManager', array('@self', '@container'));
     if (isset($config['eventManager']) && $config['eventManager']) {
         $evm->setFactory($config['eventManager']);
     }
     $connection = $builder->addDefinition($this->prefix('connection'))->setClass('Doctrine\\DBAL\\Connection')->setFactory(get_called_class() . '::createConnection', array($config, $evm));
     $metadataDriver = $builder->addDefinition($this->prefix('metadataDriver'));
     if (empty($config['metadataDriver'])) {
         if ($config['annotationCacheDriver'] === TRUE) {
             $config['annotationCacheDriver'] = $cache;
         }
         $reader = $builder->addDefinition($this->prefix('annotationReader'))->setClass('Doctrine\\Common\\Annotations\\Reader')->setFactory(get_called_class() . '::createAnnotationReader', array($config['annotationCacheDriver'], $config['useSimpleAnnotation']));
         $builder->addDefinition('discriminatorDiscovery')->setClass('Nella\\Doctrine\\Listeners\\DiscriminatorMapDiscovery', array($reader))->addTag(static::EVENT_TAG_NAME);
         $metadataDriver->setClass('Doctrine\\ORM\\Mapping\\Driver\\AnnotationDriver', array($reader, $config['entityDirs']));
     } else {
         $metadataDriver->setClass('Doctrine\\Common\\Persistence\\Mapping\\Driver\\MappingDriver')->setFactory($config['metadataDriver']);
     }
     $configuration = $builder->addDefinition($this->prefix('configuration'))->setClass('Doctrine\\ORM\\Configuration')->addSetup('setMetadataDriverImpl', array($metadataDriver))->addSetup('setDefaultRepositoryClassName', array($config['repositoryClass']))->addSetup('setProxyDir', array($config['proxy']['dir']))->addSetup('setProxyNamespace', array($config['proxy']['namespace']))->addSetup('setAutoGenerateProxyClasses', array($config['proxy']['autogenerate']));
     if ($config['metadataCacheDriver']) {
         $configuration->addSetup('setMetadataCacheImpl', array($config['metadataCacheDriver'] === TRUE ? $cache : $config['metadataCacheDriver']));
     }
     if ($config['queryCacheDriver']) {
         $configuration->addSetup('setQueryCacheImpl', array($config['queryCacheDriver'] === TRUE ? $cache : $config['queryCacheDriver']));
     }
     if ($config['resultCacheDriver']) {
         $configuration->addSetup('setResultCacheImpl', array($config['resultCacheDriver'] === TRUE ? $cache : $config['resultCacheDriver']));
     }
     $entityManager = $builder->addDefinition($this->prefix('entityManager'))->setClass('Doctrine\\ORM\\EntityManager')->setFactory('Doctrine\\ORM\\EntityManager::create', array($connection, $configuration, $evm));
     if (isset($config['console']) && $config['console']) {
         $this->processConsole($entityManager, $connection);
     }
 }
 /**
  * @return Compiler
  */
 protected function createCompiler()
 {
     $compiler = new Compiler();
     $compiler->addExtension('php', new Extensions\PhpExtension())->addExtension('constants', new Extensions\ConstantsExtension())->addExtension('nette', new Extensions\NetteExtension());
     return $compiler;
 }
 public function afterCompile(Nette\Utils\PhpGenerator\ClassType $class)
 {
     $initialize = $class->methods['initialize'];
     $container = $this->getContainerBuilder();
     $config = $this->getConfig($this->defaults);
     // debugger
     foreach (array('email', 'editor', 'browser', 'strictMode', 'maxLen', 'maxDepth') as $key) {
         if (isset($config['debugger'][$key])) {
             $initialize->addBody('Nette\\Diagnostics\\Debugger::$? = ?;', array($key, $config['debugger'][$key]));
         }
     }
     if (!$container->parameters['productionMode']) {
         if ($config['container']['debugger']) {
             $config['debugger']['bar'][] = 'Nette\\DI\\Diagnostics\\ContainerPanel';
         }
         foreach ((array) $config['debugger']['bar'] as $item) {
             $initialize->addBody($container->formatPhp('Nette\\Diagnostics\\Debugger::$bar->addPanel(?);', Nette\Config\Compiler::filterArguments(array(is_string($item) ? new Nette\DI\Statement($item) : $item))));
         }
         foreach ((array) $config['debugger']['blueScreen'] as $item) {
             $initialize->addBody($container->formatPhp('Nette\\Diagnostics\\Debugger::$blueScreen->addPanel(?);', Nette\Config\Compiler::filterArguments(array($item))));
         }
     }
     if (!empty($container->parameters['tempDir'])) {
         $initialize->addBody($this->checkTempDir($container->expand('%tempDir%/cache')));
     }
     foreach ((array) $config['forms']['messages'] as $name => $text) {
         $initialize->addBody('Nette\\Forms\\Rules::$defaultMessages[Nette\\Forms\\Form::?] = ?;', array($name, $text));
     }
     if ($config['session']['autoStart'] === 'smart') {
         $initialize->addBody('$this->session->exists() && $this->session->start();');
     } elseif ($config['session']['autoStart']) {
         $initialize->addBody('$this->session->start();');
     }
     if (empty($config['xhtml'])) {
         $initialize->addBody('Nette\\Utils\\Html::$xhtml = ?;', array((bool) $config['xhtml']));
     }
     if (isset($config['security']['frames']) && $config['security']['frames'] !== true) {
         $frames = $config['security']['frames'];
         if ($frames === false) {
             $frames = 'DENY';
         } elseif (preg_match('#^https?:#', $frames)) {
             $frames = "ALLOW-FROM {$frames}";
         }
         $initialize->addBody('header(?);', array("X-Frame-Options: {$frames}"));
     }
     foreach ($container->findByTag('run') as $name => $on) {
         if ($on) {
             $initialize->addBody('$this->getService(?);', array($name));
         }
     }
 }
Example #6
0
 /**
  * @return Compiler
  */
 protected function createCompiler()
 {
     $this->compiler = parent::createCompiler();
     $this->compiler->addExtension('venne', new \Venne\Config\Extensions\VenneExtension())->addExtension('console', new \Venne\Config\Extensions\ConsoleExtension())->addExtension('extensions', new \Venne\Config\Extensions\ExtensionsExtension())->addExtension('proxy', new \Venne\Config\Extensions\ProxyExtension());
     return $this->compiler;
 }
 /**
  * Creates the DIC compiler.
  *
  * @return \Nette\Config\Compiler
  */
 protected function createCompiler()
 {
     $compiler = new Compiler();
     $compiler->addExtension('config', new ConfigExtension($this->arguments));
     $compiler->addExtension('apigen', new ApiGenExtension());
     $compiler->addExtension('plugin', new PluginsExtension($this->createRobotLoader()));
     return $compiler;
 }
Example #8
0
Nette\DI\Statement(preg_match('#^[a-z]+$#',$info['reflection'])?'Nette\Database\Reflection\\'.ucfirst($info['reflection']).'Reflection':$info['reflection'])):Nette\Config\Compiler::filterArguments(array($info['reflection'])));}if(!$container->parameters['productionMode']&&$info['debugger']){$panel=$container->addDefinition($this->prefix("database.{$name}ConnectionPanel"))->setClass('Nette\Database\Diagnostics\ConnectionPanel')->setAutowired(FALSE)->addSetup('$explain',!empty($info['explain']))->addSetup('Nette\Diagnostics\Debugger::$bar->addPanel(?)',array('@self'));$connection->addSetup('$service->onQuery[] = ?',array(array($panel,'logQuery')));}}}function
afterCompile(Nette\Utils\PhpGenerator\ClassType$class){$initialize=$class->methods['initialize'];$container=$this->getContainerBuilder();$config=$this->getConfig($this->defaults);foreach(array('email','editor','browser','strictMode','maxLen','maxDepth')as$key){if(isset($config['debugger'][$key])){$initialize->addBody('Nette\Diagnostics\Debugger::$? = ?;',array($key,$config['debugger'][$key]));}}if(!$container->parameters['productionMode']){if($config['container']['debugger']){$config['debugger']['bar'][]='Nette\DI\Diagnostics\ContainerPanel';}foreach((array)$config['debugger']['bar']as$item){$initialize->addBody($container->formatPhp('Nette\Diagnostics\Debugger::$bar->addPanel(?);',Nette\Config\Compiler::filterArguments(array(is_string($item)?new
Nette\DI\Statement($item):$item))));}foreach((array)$config['debugger']['blueScreen']as$item){$initialize->addBody($container->formatPhp('Nette\Diagnostics\Debugger::$blueScreen->addPanel(?);',Nette\Config\Compiler::filterArguments(array($item))));}}if(!empty($container->parameters['tempDir'])){$initialize->addBody($this->checkTempDir($container->expand('%tempDir%/cache')));}foreach((array)$config['forms']['messages']as$name=>$text){$initialize->addBody('Nette\Forms\Rules::$defaultMessages[Nette\Forms\Form::?] = ?;',array($name,$text));}if($config['session']['autoStart']==='smart'){$initialize->addBody('$this->session->exists() && $this->session->start();');}elseif($config['session']['autoStart']){$initialize->addBody('$this->session->start();');}if(empty($config['xhtml'])){$initialize->addBody('Nette\Utils\Html::$xhtml = ?;',array((bool)$config['xhtml']));}if(isset($config['security']['frames'])&&$config['security']['frames']!==TRUE){$frames=$config['security']['frames'];if($frames===FALSE){$frames='DENY';}elseif(preg_match('#^https?:#',$frames)){$frames="ALLOW-FROM $frames";}$initialize->addBody('header(?);',array("X-Frame-Options: $frames"));}foreach($container->findByTag('run')as$name=>$on){if($on){$initialize->addBody('$this->getService(?);',array($name));}}}private