Packages

  • 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

  • DIContainer
  • DIContainerBuilder
  • DIHelpers
  • DIServiceDefinition
  • DIStatement

Interfaces

  • IDIContainer

Exceptions

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