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

Classes

Interfaces

Exceptions

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