setClass() 공개 메소드

public setClass ( $class, array $args = [] ) : self
$args array
리턴 self
예제 #1
0
 function getAssets()
 {
     $serviceDefinition = new ServiceDefinition();
     $serviceDefinition->setClass('Mike227\\NAssetic\\AssetCollection');
     $serviceDefinition->setArguments(array(array('test6.js'), array(new TestFilter('#FILTER')), __DIR__ . '/../sources/'));
     return array('js1' => new GlobAsset(__DIR__ . '/../sources/test1.*'), 'js2' => array(new GlobAsset(__DIR__ . '/../sources/test2.*'), __DIR__ . '/../sources/test3.js', array(__DIR__ . '/../sources/test4.js', new GlobAsset(__DIR__ . '/../sources/test5.*')), 'serviceDef' => $serviceDefinition));
 }
 /**
  * @param string $class
  * @return ServiceDefinition
  */
 public function createServiceDefinition($class)
 {
     $definition = new ServiceDefinition();
     $definition->setClass($class);
     $definition->addTag('kdyby.subscriber');
     return $definition;
 }
예제 #3
0
 /**
  * Parses single service from configuration file.
  * @return void
  */
 public static function parseService(Nette\DI\ServiceDefinition $definition, $config, $shared = TRUE)
 {
     if ($config === NULL) {
         return;
     } elseif (!is_array($config)) {
         $config = array('class' => NULL, 'factory' => $config);
     }
     $known = $shared ? array('class', 'factory', 'arguments', 'setup', 'autowired', 'run', 'tags') : array('class', 'factory', 'arguments', 'setup', 'autowired', 'tags', 'internal', 'parameters');
     if ($error = array_diff(array_keys($config), $known)) {
         throw new Nette\InvalidStateException("Unknown 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('factory', $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('factory', $config)) {
         Validators::assertField($config, 'factory', 'callable|stdClass|null');
         if ($config['factory'] instanceof \stdClass) {
             $definition->setFactory($config['factory']->value, self::filterArguments($config['factory']->attributes));
         } else {
             $definition->setFactory($config['factory'], $arguments);
         }
     }
     if (isset($config['setup'])) {
         if (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);
             }
         }
     }
     $definition->setShared($shared);
     if (isset($config['parameters'])) {
         Validators::assertField($config, 'parameters', 'array');
         $definition->setParameters($config['parameters']);
     }
     if (isset($config['autowired'])) {
         Validators::assertField($config, 'autowired', 'bool');
         $definition->setAutowired($config['autowired']);
     }
     if (isset($config['internal'])) {
         Validators::assertField($config, 'internal', 'bool');
         $definition->setInternal($config['internal']);
     }
     if (isset($config['run'])) {
         $config['tags']['run'] = (bool) $config['run'];
     }
     if (isset($config['tags'])) {
         Validators::assertField($config, 'tags', 'array');
         if (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);
             }
         }
     }
 }
예제 #4
0
 private function resolveImplement(ServiceDefinition $def, $name)
 {
     $interface = $def->getImplement();
     if (!interface_exists($interface)) {
         throw new ServiceCreationException("Interface {$interface} used in service '{$name}' not found.");
     }
     self::checkCase($interface);
     $rc = new ReflectionClass($interface);
     $method = $rc->hasMethod('create') ? $rc->getMethod('create') : ($rc->hasMethod('get') ? $rc->getMethod('get') : NULL);
     if (count($rc->getMethods()) !== 1 || !$method || $method->isStatic()) {
         throw new ServiceCreationException("Interface {$interface} used in service '{$name}' must have just one non-static method create() or get().");
     }
     $def->setImplementType($methodName = $rc->hasMethod('create') ? 'create' : 'get');
     if (!$def->getClass() && !$def->getEntity()) {
         $returnType = PhpReflection::getReturnType($method);
         if (!$returnType) {
             throw new ServiceCreationException("Method {$interface}::{$methodName}() used in service '{$name}' has no @return annotation.");
         } elseif (!class_exists($returnType)) {
             throw new ServiceCreationException("Check a @return annotation of the {$interface}::{$methodName}() method used in service '{$name}', class '{$returnType}' cannot be found.");
         }
         $def->setClass($returnType);
     }
     if ($methodName === 'get') {
         if ($method->getParameters()) {
             throw new ServiceCreationException("Method {$interface}::get() used in service '{$name}' must have no arguments.");
         }
         if (!$def->getEntity()) {
             $def->setFactory('@\\' . ltrim($def->getClass(), '\\'));
         } elseif (!$this->getServiceName($def->getFactory()->getEntity())) {
             throw new ServiceCreationException("Invalid factory in service '{$name}' definition.");
         }
     }
     if (!$def->parameters) {
         $ctorParams = [];
         if (!$def->getEntity()) {
             $def->setFactory($def->getClass(), $def->getFactory() ? $def->getFactory()->arguments : []);
         }
         if (($class = $this->resolveEntityClass($def->getFactory(), [$name => 1])) && ($ctor = (new ReflectionClass($class))->getConstructor())) {
             foreach ($ctor->getParameters() as $param) {
                 $ctorParams[$param->getName()] = $param;
             }
         }
         foreach ($method->getParameters() as $param) {
             $hint = PhpReflection::getParameterType($param);
             if (isset($ctorParams[$param->getName()])) {
                 $arg = $ctorParams[$param->getName()];
                 if ($hint !== PhpReflection::getParameterType($arg)) {
                     throw new ServiceCreationException("Type hint for \${$param->getName()} in {$interface}::{$methodName}() doesn't match type hint in {$class} constructor.");
                 }
                 $def->getFactory()->arguments[$arg->getPosition()] = self::literal('$' . $arg->getName());
             }
             $paramDef = $hint . ' ' . $param->getName();
             if ($param->isOptional()) {
                 $def->parameters[$paramDef] = $param->getDefaultValue();
             } else {
                 $def->parameters[] = $paramDef;
             }
         }
     }
 }
예제 #5
0
파일: Compiler.php 프로젝트: kukulich/di
 /**
  * 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 = ['class' => NULL, 'implement' => $config];
     } elseif ($config instanceof Statement && is_string($config->getEntity()) && interface_exists($config->getEntity())) {
         $config = ['class' => NULL, 'implement' => $config->getEntity(), 'factory' => array_shift($config->arguments)];
     } elseif (!is_array($config) || isset($config[0], $config[1])) {
         $config = ['class' => NULL, 'create' => $config];
     }
     if (array_key_exists('factory', $config)) {
         $config['create'] = $config['factory'];
         unset($config['factory']);
     }
     $known = ['class', 'create', 'arguments', 'setup', 'autowired', 'dynamic', 'inject', 'parameters', 'implement', 'run', 'tags'];
     if ($error = array_diff(array_keys($config), $known)) {
         throw new Nette\InvalidStateException(sprintf("Unknown or deprecated key '%s' in definition of service.", implode("', '", $error)));
     }
     $config = Helpers::filterArguments($config);
     $arguments = [];
     if (array_key_exists('arguments', $config)) {
         Validators::assertField($config, 'arguments', 'array');
         $arguments = $config['arguments'];
         $definition->setArguments($arguments);
     }
     if (array_key_exists('class', $config) || array_key_exists('create', $config)) {
         $definition->setClass(NULL);
         $definition->setFactory(NULL);
     }
     if (array_key_exists('class', $config)) {
         Validators::assertField($config, 'class', 'string|Nette\\DI\\Statement|null');
         if (!$config['class'] instanceof Statement) {
             $definition->setClass($config['class']);
         }
         $definition->setFactory($config['class'], $arguments);
     }
     if (array_key_exists('create', $config)) {
         Validators::assertField($config, 'create', 'callable|Nette\\DI\\Statement|null');
         $definition->setFactory($config['create'], $arguments);
     }
     if (isset($config['setup'])) {
         if (Config\Helpers::takeParent($config['setup'])) {
             $definition->setSetup([]);
         }
         Validators::assertField($config, 'setup', 'list');
         foreach ($config['setup'] as $id => $setup) {
             Validators::assert($setup, 'callable|Nette\\DI\\Statement', "setup item #{$id}");
             $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['dynamic'])) {
         Validators::assertField($config, 'dynamic', 'bool');
         $definition->setDynamic($config['dynamic']);
     }
     if (isset($config['inject'])) {
         Validators::assertField($config, 'inject', 'bool');
         $definition->addTag(Extensions\InjectExtension::TAG_INJECT, $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->setTags([]);
         }
         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
  */
 public function createServiceDefinition($class)
 {
     $definition = new ServiceDefinition();
     $definition->setClass($class);
     return $definition;
 }
예제 #7
0
 /**
  * @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;
 }
예제 #8
0
	/**
	 * 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(sprintf("Unknown or deprecated key '%s' in definition of service.", implode("', '", $error)));
		}

		$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);
				}
			}
		}
	}
예제 #9
0
파일: Compiler.php 프로젝트: nette/di
 /**
  * Parses single service definition from configuration.
  * @return void
  */
 public static function loadDefinition(ServiceDefinition $definition, $config)
 {
     if ($config === NULL) {
         return;
     } elseif (is_string($config) && interface_exists($config)) {
         $config = ['class' => NULL, 'implement' => $config];
     } elseif ($config instanceof Statement && is_string($config->getEntity()) && interface_exists($config->getEntity())) {
         $config = ['class' => NULL, 'implement' => $config->getEntity(), 'factory' => array_shift($config->arguments)];
     } elseif (!is_array($config) || isset($config[0], $config[1])) {
         $config = ['class' => NULL, 'factory' => $config];
     }
     if (array_key_exists('create', $config)) {
         trigger_error("Key 'create' is deprecated, use 'factory' or 'class' in configuration.", E_USER_DEPRECATED);
         $config['factory'] = $config['create'];
         unset($config['create']);
     }
     $known = ['class', 'factory', 'arguments', 'setup', 'autowired', 'dynamic', 'inject', 'parameters', 'implement', 'run', 'tags'];
     if ($error = array_diff(array_keys($config), $known)) {
         $hints = array_filter(array_map(function ($error) use($known) {
             return Nette\Utils\ObjectMixin::getSuggestion($known, $error);
         }, $error));
         $hint = $hints ? ", did you mean '" . implode("', '", $hints) . "'?" : '.';
         throw new Nette\InvalidStateException(sprintf("Unknown key '%s' in definition of service{$hint}", implode("', '", $error)));
     }
     $config = Helpers::filterArguments($config);
     if (array_key_exists('class', $config) || array_key_exists('factory', $config)) {
         $definition->setClass(NULL);
         $definition->setFactory(NULL);
     }
     if (array_key_exists('class', $config)) {
         Validators::assertField($config, 'class', 'string|Nette\\DI\\Statement|null');
         if (!$config['class'] instanceof Statement) {
             $definition->setClass($config['class']);
         }
         $definition->setFactory($config['class']);
     }
     if (array_key_exists('factory', $config)) {
         Validators::assertField($config, 'factory', 'callable|Nette\\DI\\Statement|null');
         $definition->setFactory($config['factory']);
     }
     if (array_key_exists('arguments', $config)) {
         Validators::assertField($config, 'arguments', 'array');
         $arguments = $config['arguments'];
         if (!Config\Helpers::takeParent($arguments) && !Nette\Utils\Arrays::isList($arguments) && $definition->getFactory()) {
             $arguments += $definition->getFactory()->arguments;
         }
         $definition->setArguments($arguments);
     }
     if (isset($config['setup'])) {
         if (Config\Helpers::takeParent($config['setup'])) {
             $definition->setSetup([]);
         }
         Validators::assertField($config, 'setup', 'list');
         foreach ($config['setup'] as $id => $setup) {
             Validators::assert($setup, 'callable|Nette\\DI\\Statement|array:1', "setup item #{$id}");
             if (is_array($setup)) {
                 $setup = new Statement(key($setup), array_values($setup));
             }
             $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|string|array');
         $definition->setAutowired($config['autowired']);
     }
     if (isset($config['dynamic'])) {
         Validators::assertField($config, 'dynamic', 'bool');
         $definition->setDynamic($config['dynamic']);
     }
     if (isset($config['inject'])) {
         Validators::assertField($config, 'inject', 'bool');
         $definition->addTag(Extensions\InjectExtension::TAG_INJECT, $config['inject']);
     }
     if (isset($config['run'])) {
         trigger_error("Option 'run' is deprecated, use 'run' as tag.", E_USER_DEPRECATED);
         $config['tags']['run'] = (bool) $config['run'];
     }
     if (isset($config['tags'])) {
         Validators::assertField($config, 'tags', 'array');
         if (Config\Helpers::takeParent($config['tags'])) {
             $definition->setTags([]);
         }
         foreach ($config['tags'] as $tag => $attrs) {
             if (is_int($tag) && is_string($attrs)) {
                 $definition->addTag($attrs);
             } else {
                 $definition->addTag($tag, $attrs);
             }
         }
     }
 }
예제 #10
0
 /**
  * @return ServiceDefinition
  */
 private function createMetadataServiceDefinition()
 {
     $definition = new ServiceDefinition();
     $definition->setClass(MappingDriver::class);
     $definition->setAutowired(false);
     $definition->setInject(false);
     return $definition;
 }