/** * Parses single service from configuration file. * @return void */ public static function parseService(ServiceDefinition $definition, $config) { if ($config === NULL) { return; } elseif (is_string($config) && interface_exists($config)) { $config = array('class' => NULL, 'implement' => $config); } elseif ($config instanceof \stdClass && interface_exists($config->value)) { $config = array('class' => NULL, 'implement' => $config->value, 'factory' => array_shift($config->attributes)); } elseif (!is_array($config)) { $config = array('class' => NULL, 'create' => $config); } if (array_key_exists('factory', $config)) { $config['create'] = $config['factory']; unset($config['factory']); } $known = array('class', 'create', 'arguments', 'setup', 'autowired', 'inject', 'parameters', 'implement', 'run', 'tags'); if ($error = array_diff(array_keys($config), $known)) { throw new Nette\InvalidStateException("Unknown or deprecated key '" . implode("', '", $error) . "' in definition of service."); } $arguments = array(); if (array_key_exists('arguments', $config)) { Validators::assertField($config, 'arguments', 'array'); $arguments = self::filterArguments($config['arguments']); $definition->setArguments($arguments); } if (array_key_exists('class', $config) || array_key_exists('create', $config)) { $definition->class = NULL; $definition->factory = NULL; } if (array_key_exists('class', $config)) { Validators::assertField($config, 'class', 'string|stdClass|null'); if ($config['class'] instanceof \stdClass) { $definition->setClass($config['class']->value, self::filterArguments($config['class']->attributes)); } else { $definition->setClass($config['class'], $arguments); } } if (array_key_exists('create', $config)) { Validators::assertField($config, 'create', 'callable|stdClass|null'); if ($config['create'] instanceof \stdClass) { $definition->setFactory($config['create']->value, self::filterArguments($config['create']->attributes)); } else { $definition->setFactory($config['create'], $arguments); } } if (isset($config['setup'])) { if (Config\Helpers::takeParent($config['setup'])) { $definition->setup = array(); } Validators::assertField($config, 'setup', 'list'); foreach ($config['setup'] as $id => $setup) { Validators::assert($setup, 'callable|stdClass', "setup item #{$id}"); if ($setup instanceof \stdClass) { Validators::assert($setup->value, 'callable', "setup item #{$id}"); $definition->addSetup($setup->value, self::filterArguments($setup->attributes)); } else { $definition->addSetup($setup); } } } if (isset($config['parameters'])) { Validators::assertField($config, 'parameters', 'array'); $definition->setParameters($config['parameters']); } if (isset($config['implement'])) { Validators::assertField($config, 'implement', 'string'); $definition->setImplement($config['implement']); $definition->setAutowired(TRUE); } if (isset($config['autowired'])) { Validators::assertField($config, 'autowired', 'bool'); $definition->setAutowired($config['autowired']); } if (isset($config['inject'])) { Validators::assertField($config, 'inject', 'bool'); $definition->setInject($config['inject']); } if (isset($config['run'])) { $config['tags']['run'] = (bool) $config['run']; } if (isset($config['tags'])) { Validators::assertField($config, 'tags', 'array'); if (Config\Helpers::takeParent($config['tags'])) { $definition->tags = array(); } foreach ($config['tags'] as $tag => $attrs) { if (is_int($tag) && is_string($attrs)) { $definition->addTag($attrs); } else { $definition->addTag($tag, $attrs); } } } }
/** * @param string $class * @return ServiceDefinition * @throws \Nette\InvalidArgumentException */ public function createServiceDefinition($class) { // Validate annotation attributes if (count($this->parameters) === 0) { throw new InvalidArgumentException("Annotation @" . get_class() . " of class {$class} expects one or more parameters."); } // Create service definition $definition = new ServiceDefinition(); $definition->setClass($class); $definition->setInject(TRUE); // Parameters for service's constructor $serviceParameters = array(); // One parameter - @DaoManager("Name\Space\EntityClass") if (count($this->parameters) === 1 && isset($this->parameters[0])) { $entityName = $this->parameters[0]; $daoParameters = $this->findConstructorDaoParameters($class); if (count($daoParameters) === 0) { throw new InvalidArgumentException("Annotation @" . get_class() . ": {$class}::__construct() does not contain attribute with type hint " . self::DAO_CLASS); } else { if (count($daoParameters) > 1) { throw new InvalidArgumentException("Annotation @" . get_class() . ": {$class}::__construct() contains multiple attributes with type hint " . self::DAO_CLASS); } } $serviceParameters = array($daoParameters[0] => $this->createDaoStatement($entityName)); } else { foreach ($this->parameters as $attributeName => $entityName) { $serviceParameters[$attributeName] = $this->createDaoStatement($entityName); } } $definition->setArguments($serviceParameters); return $definition; }
public function process(ServiceDefinition $definition, $args) { if ($args === TRUE) { $definition->setInject(TRUE); } }
/** * @return ServiceDefinition */ private function createMetadataServiceDefinition() { $definition = new ServiceDefinition(); $definition->setClass(MappingDriver::class); $definition->setAutowired(false); $definition->setInject(false); return $definition; }