Namespaces

  • Latte
    • Loaders
    • Macros
    • Runtime
  • Nette
    • Application
      • Responses
      • Routers
      • UI
    • Bridges
      • ApplicationDI
      • ApplicationLatte
      • ApplicationTracy
      • CacheDI
      • CacheLatte
      • DatabaseDI
      • DatabaseTracy
      • DITracy
      • FormsDI
      • FormsLatte
      • Framework
      • HttpDI
      • HttpTracy
      • MailDI
      • ReflectionDI
      • SecurityDI
      • SecurityTracy
    • Caching
      • Storages
    • ComponentModel
    • Database
      • Conventions
      • Drivers
      • Reflection
      • Table
    • DI
      • Config
        • Adapters
      • Extensions
    • Forms
      • Controls
      • Rendering
    • Http
    • Iterators
    • Loaders
    • Localization
    • Mail
    • Neon
    • PhpGenerator
    • Reflection
    • Security
    • Utils
  • none
  • Tracy
    • Bridges
      • Nette

Classes

  • ConstantsExtension
  • DecoratorExtension
  • DIExtension
  • ExtensionsExtension
  • InjectExtension
  • PhpExtension
  • Overview
  • Namespace
  • Class
  • Tree
  • Deprecated
  • Other releases
  • Nette homepage
  1: <?php
  2: 
  3: /**
  4:  * This file is part of the Nette Framework (https://nette.org)
  5:  * Copyright (c) 2004 David Grudl (https://davidgrudl.com)
  6:  */
  7: 
  8: namespace Nette\DI\Extensions;
  9: 
 10: use Nette;
 11: use Nette\DI;
 12: use Nette\DI\PhpReflection;
 13: 
 14: 
 15: /**
 16:  * Calls inject methods and fills @inject properties.
 17:  */
 18: class InjectExtension extends DI\CompilerExtension
 19: {
 20:     const TAG_INJECT = 'inject';
 21: 
 22: 
 23:     public function beforeCompile()
 24:     {
 25:         foreach ($this->getContainerBuilder()->getDefinitions() as $def) {
 26:             if ($def->getTag(self::TAG_INJECT) && $def->getClass()) {
 27:                 $this->updateDefinition($def);
 28:             }
 29:         }
 30:     }
 31: 
 32: 
 33:     private function updateDefinition($def)
 34:     {
 35:         $class = $def->getClass();
 36:         $builder = $this->getContainerBuilder();
 37:         $injects = array();
 38:         foreach (self::getInjectProperties($class) as $property => $type) {
 39:             self::checkType($class, $property, $type, $builder);
 40:             $injects[] = new DI\Statement('$' . $property, array('@\\' . ltrim($type, '\\')));
 41:         }
 42: 
 43:         foreach (self::getInjectMethods($def->getClass()) as $method) {
 44:             $injects[] = new DI\Statement($method);
 45:         }
 46: 
 47:         $setups = $def->getSetup();
 48:         foreach ($injects as $inject) {
 49:             foreach ($setups as $key => $setup) {
 50:                 if ($setup->getEntity() === $inject->getEntity()) {
 51:                     $inject = $setup;
 52:                     unset($setups[$key]);
 53:                 }
 54:             }
 55:             array_unshift($setups, $inject);
 56:         }
 57:         $def->setSetup($setups);
 58:     }
 59: 
 60: 
 61:     /**
 62:      * Generates list of inject methods.
 63:      * @return array
 64:      * @internal
 65:      */
 66:     public static function getInjectMethods($class)
 67:     {
 68:         return array_values(array_filter(get_class_methods($class), function ($name) {
 69:             return substr($name, 0, 6) === 'inject';
 70:         }));
 71:     }
 72: 
 73: 
 74:     /**
 75:      * Generates list of properties with annotation @inject.
 76:      * @return array
 77:      * @internal
 78:      */
 79:     public static function getInjectProperties($class)
 80:     {
 81:         $res = array();
 82:         foreach (get_class_vars($class) as $name => $foo) {
 83:             $rp = new \ReflectionProperty($class, $name);
 84:             if (PhpReflection::parseAnnotation($rp, 'inject') !== NULL) {
 85:                 if ($type = PhpReflection::parseAnnotation($rp, 'var')) {
 86:                     $type = PhpReflection::expandClassName($type, PhpReflection::getDeclaringClass($rp));
 87:                 }
 88:                 $res[$name] = $type;
 89:             }
 90:         }
 91:         return $res;
 92:     }
 93: 
 94: 
 95:     /**
 96:      * Calls all methods starting with with "inject" using autowiring.
 97:      * @return void
 98:      */
 99:     public static function callInjects(DI\Container $container, $service)
100:     {
101:         if (!is_object($service)) {
102:             throw new Nette\InvalidArgumentException(sprintf('Service must be object, %s given.', gettype($service)));
103:         }
104: 
105:         foreach (array_reverse(self::getInjectMethods($service)) as $method) {
106:             $container->callMethod(array($service, $method));
107:         }
108: 
109:         foreach (self::getInjectProperties(get_class($service)) as $property => $type) {
110:             self::checkType($service, $property, $type, $container);
111:             $service->$property = $container->getByType($type);
112:         }
113:     }
114: 
115: 
116:     /** @internal */
117:     private static function checkType($class, $name, $type, $container)
118:     {
119:         $rc = PhpReflection::getDeclaringClass(new \ReflectionProperty($class, $name));
120:         $fullname = $rc->getName() . '::$' . $name;
121:         if (!$type) {
122:             throw new Nette\InvalidStateException("Property $fullname has no @var annotation.");
123:         } elseif (!class_exists($type) && !interface_exists($type)) {
124:             throw new Nette\InvalidStateException("Class or interface '$type' used in @var annotation at $fullname not found. Check annotation and 'use' statements.");
125:         } elseif (!$container->getByType($type, FALSE)) {
126:             throw new Nette\InvalidStateException("Service of type {$type} used in @var annotation at $fullname not found. Did you register it in configuration file?");
127:         }
128:     }
129: 
130: }
131: 
Nette 2.3-20161221 API API documentation generated by ApiGen 2.8.0