Namespaces

  • Nette
    • Application
      • Diagnostics
      • Responses
      • Routers
      • UI
    • Caching
      • Storages
    • ComponentModel
    • Config
      • Adapters
      • Extensions
    • Database
      • Diagnostics
      • Drivers
      • Reflection
      • Table
    • DI
      • Diagnostics
    • Diagnostics
    • Forms
      • Controls
      • Rendering
    • Http
    • Iterators
    • Latte
      • Macros
    • Loaders
    • Localization
    • Mail
    • Reflection
    • Security
      • Diagnostics
    • Templating
    • Utils
      • PhpGenerator
  • NetteModule
  • None
  • PHP

Classes

  • Container
  • ContainerBuilder
  • Helpers
  • ServiceDefinition
  • Statement

Interfaces

  • IContainer

Exceptions

  • MissingServiceException
  • ServiceCreationException
  • Overview
  • Namespace
  • Class
  • Tree
  • Deprecated
  1: <?php
  2: 
  3: /**
  4:  * This file is part of the Nette Framework (http://nette.org)
  5:  *
  6:  * Copyright (c) 2004 David Grudl (http://davidgrudl.com)
  7:  *
  8:  * For the full copyright and license information, please view
  9:  * the file license.txt that was distributed with this source code.
 10:  */
 11: 
 12: namespace Nette\DI;
 13: 
 14: use Nette;
 15: 
 16: 
 17: 
 18: /**
 19:  * The DI helpers.
 20:  *
 21:  * @author     David Grudl
 22:  */
 23: final class Helpers
 24: {
 25: 
 26:     /**
 27:      * Expands %placeholders%.
 28:      * @param  mixed
 29:      * @param  array
 30:      * @param  bool
 31:      * @return mixed
 32:      * @throws Nette\InvalidArgumentException
 33:      */
 34:     public static function expand($var, array $params, $recursive = FALSE)
 35:     {
 36:         if (is_array($var)) {
 37:             $res = array();
 38:             foreach ($var as $key => $val) {
 39:                 $res[$key] = self::expand($val, $params, $recursive);
 40:             }
 41:             return $res;
 42: 
 43:         } elseif ($var instanceof Statement) {
 44:             return new Statement(self::expand($var->entity, $params, $recursive), self::expand($var->arguments, $params, $recursive));
 45: 
 46:         } elseif (!is_string($var)) {
 47:             return $var;
 48:         }
 49: 
 50:         $parts = preg_split('#%([\w.-]*)%#i', $var, -1, PREG_SPLIT_DELIM_CAPTURE);
 51:         $res = '';
 52:         foreach ($parts as $n => $part) {
 53:             if ($n % 2 === 0) {
 54:                 $res .= $part;
 55: 
 56:             } elseif ($part === '') {
 57:                 $res .= '%';
 58: 
 59:             } elseif (isset($recursive[$part])) {
 60:                 throw new Nette\InvalidArgumentException('Circular reference detected for variables: ' . implode(', ', array_keys($recursive)) . '.');
 61: 
 62:             } else {
 63:                 $val = Nette\Utils\Arrays::get($params, explode('.', $part));
 64:                 if ($recursive) {
 65:                     $val = self::expand($val, $params, (is_array($recursive) ? $recursive : array()) + array($part => 1));
 66:                 }
 67:                 if (strlen($part) + 2 === strlen($var)) {
 68:                     return $val;
 69:                 }
 70:                 if (!is_scalar($val)) {
 71:                     throw new Nette\InvalidArgumentException("Unable to concatenate non-scalar parameter '$part' into '$var'.");
 72:                 }
 73:                 $res .= $val;
 74:             }
 75:         }
 76:         return $res;
 77:     }
 78: 
 79: 
 80: 
 81:     /**
 82:      * Expand counterpart.
 83:      * @param  mixed
 84:      * @return mixed
 85:      */
 86:     public static function escape($value)
 87:     {
 88:         if (is_array($value)) {
 89:             array_walk_recursive($value, function(&$val) {
 90:                 $val = is_string($val) ? str_replace('%', '%%', $val) : $val;
 91:             });
 92:         } elseif (is_string($value)) {
 93:             $value = str_replace('%', '%%', $value);
 94:         }
 95:         return $value;
 96:     }
 97: 
 98: 
 99: 
100:     /**
101:      * Generates list of arguments using autowiring.
102:      * @param  Nette\Reflection\GlobalFunction|Nette\Reflection\Method
103:      * @return array
104:      */
105:     public static function autowireArguments(\ReflectionFunctionAbstract $method, array $arguments, $container)
106:     {
107:         $optCount = 0;
108:         $num = -1;
109:         $res = array();
110: 
111:         foreach ($method->getParameters() as $num => $parameter) {
112:             if (array_key_exists($num, $arguments)) {
113:                 $res[$num] = $arguments[$num];
114:                 unset($arguments[$num]);
115:                 $optCount = 0;
116: 
117:             } elseif (array_key_exists($parameter->getName(), $arguments)) {
118:                 $res[$num] = $arguments[$parameter->getName()];
119:                 unset($arguments[$parameter->getName()]);
120:                 $optCount = 0;
121: 
122:             } elseif ($class = $parameter->getClassName()) { // has object type hint
123:                 $res[$num] = $container->getByType($class, FALSE);
124:                 if ($res[$num] === NULL) {
125:                     if ($parameter->allowsNull()) {
126:                         $optCount++;
127:                     } else {
128:                         throw new ServiceCreationException("No service of type {$class} found. Make sure the type hint in $method is written correctly and service of this type is registered.");
129:                     }
130:                 } else {
131:                     if ($container instanceof ContainerBuilder) {
132:                         $res[$num] = '@' . $res[$num];
133:                     }
134:                     $optCount = 0;
135:                 }
136: 
137:             } elseif ($parameter->isOptional()) {
138:                 // PDO::__construct has optional parameter without default value (and isArray() and allowsNull() returns FALSE)
139:                 $res[$num] = $parameter->isDefaultValueAvailable() ? $parameter->getDefaultValue() : NULL;
140:                 $optCount++;
141: 
142:             } else {
143:                 throw new ServiceCreationException("$parameter has no type hint, so its value must be specified.");
144:             }
145:         }
146: 
147:         // extra parameters
148:         while (array_key_exists(++$num, $arguments)) {
149:             $res[$num] = $arguments[$num];
150:             unset($arguments[$num]);
151:             $optCount = 0;
152:         }
153:         if ($arguments) {
154:             throw new ServiceCreationException("Unable to pass specified arguments to $method.");
155:         }
156: 
157:         return $optCount ? array_slice($res, 0, -$optCount) : $res;
158:     }
159: 
160: }
161: 
Nette Framework 2.0.7 API API documentation generated by ApiGen 2.8.0