Kdyby\Doctrine\DI\OrmExtension::loadConfiguration PHP Method

loadConfiguration() public method

public loadConfiguration ( )
    public function loadConfiguration()
    {
        $this->proxyAutoloaders = $this->targetEntityMappings = $this->configuredConnections = $this->managerConfigs = $this->configuredManagers = $this->postCompileRepositoriesQueue = [];
        if (!$this->compiler->getExtensions('Kdyby\\Annotations\\DI\\AnnotationsExtension')) {
            throw new Nette\Utils\AssertionException('You should register \'Kdyby\\Annotations\\DI\\AnnotationsExtension\' before \'' . get_class($this) . '\'.', E_USER_NOTICE);
        }
        $builder = $this->getContainerBuilder();
        $config = $this->getConfig();
        $builder->parameters[$this->prefix('debug')] = !empty($config['debug']);
        if (isset($config['dbname']) || isset($config['driver']) || isset($config['connection'])) {
            $config = ['default' => $config];
            $defaults = ['debug' => $builder->parameters['debugMode']];
        } else {
            $defaults = array_intersect_key($config, $this->managerDefaults) + array_intersect_key($config, $this->connectionDefaults) + ['debug' => $builder->parameters['debugMode']];
            $config = array_diff_key($config, $defaults);
        }
        if (empty($config)) {
            throw new Kdyby\Doctrine\UnexpectedValueException("Please configure the Doctrine extensions using the section '{$this->name}:' in your config file.");
        }
        foreach ($config as $name => $emConfig) {
            if (!is_array($emConfig) || empty($emConfig['dbname']) && empty($emConfig['driver'])) {
                throw new Kdyby\Doctrine\UnexpectedValueException("Please configure the Doctrine extensions using the section '{$this->name}:' in your config file.");
            }
            $emConfig = Nette\DI\Config\Helpers::merge($emConfig, $defaults);
            $this->processEntityManager($name, $emConfig);
        }
        if ($this->targetEntityMappings) {
            if (!$this->isKdybyEventsPresent()) {
                throw new Nette\Utils\AssertionException('The option \'targetEntityMappings\' requires \'Kdyby\\Events\\DI\\EventsExtension\'.', E_USER_NOTICE);
            }
            $listener = $builder->addDefinition($this->prefix('resolveTargetEntityListener'))->setClass('Kdyby\\Doctrine\\Tools\\ResolveTargetEntityListener')->addTag(Kdyby\Events\DI\EventsExtension::SUBSCRIBER_TAG)->setInject(FALSE);
            foreach ($this->targetEntityMappings as $originalEntity => $mapping) {
                $listener->addSetup('addResolveTargetEntity', [$originalEntity, $mapping['targetEntity'], $mapping]);
            }
        }
        $this->loadConsole();
        $builder->addDefinition($this->prefix('registry'))->setClass('Kdyby\\Doctrine\\Registry', [$this->configuredConnections, $this->configuredManagers, $builder->parameters[$this->name]['dbal']['defaultConnection'], $builder->parameters[$this->name]['orm']['defaultEntityManager']]);
    }

Usage Example

Example #1
0
 /**
  * @return void
  */
 public function loadConfiguration()
 {
     // Get container builder
     $builder = $this->getContainerBuilder();
     $builder->addDefinition($this->prefix('validators'))->setClass(Doctrine\Validators::CLASS_NAME);
     $builder->addDefinition($this->prefix('entity.mapper'))->setClass(Mapping\EntityMapper::CLASS_NAME);
     $builder->addDefinition($this->prefix('entity.crudFactory'))->setClass(Crud\EntityCrudFactory::CLASS_NAME);
     // syntax sugar for config
     $builder->addDefinition($this->prefix('crud'))->setClass(Crud\EntityCrud::CLASS_NAME)->setFactory('@IPub\\Doctrine\\Crud\\EntityCrudFactory::createEntityCrud', [new PhpGenerator\PhpLiteral('$entityName')])->setParameters(['entityName']);
     parent::loadConfiguration();
     $configuration = $builder->getDefinition('doctrine.default.ormConfiguration');
     $configuration->addSetup('addCustomStringFunction', ['DATE_FORMAT', Doctrine\StringFunctions\DateFormat::CLASS_NAME]);
 }