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
  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:      * @return array
103:      */
104:     public static function autowireArguments(\ReflectionFunctionAbstract $method, array $arguments, $container)
105:     {
106:         $optCount = 0;
107:         $num = -1;
108:         $res = array();
109: 
110:         foreach ($method->getParameters() as $num => $parameter) {
111:             if (array_key_exists($num, $arguments)) {
112:                 $res[$num] = $arguments[$num];
113:                 unset($arguments[$num]);
114:                 $optCount = 0;
115: 
116:             } elseif (array_key_exists($parameter->getName(), $arguments)) {
117:                 $res[$num] = $arguments[$parameter->getName()];
118:                 unset($arguments[$parameter->getName()]);
119:                 $optCount = 0;
120: 
121:             } elseif ($parameter->getClass()) { // has object typehint
122:                 $res[$num] = $container->getByType($parameter->getClass()->getName());
123:                 if ($res[$num] === NULL) {
124:                     if ($parameter->allowsNull()) {
125:                         $optCount++;
126:                     } else {
127:                         throw new Nette\InvalidArgumentException("No service of type {$parameter->getClass()->getName()} found");
128:                     }
129:                 } else {
130:                     if ($container instanceof ContainerBuilder) {
131:                         $res[$num] = '@' . $res[$num];
132:                     }
133:                     $optCount = 0;
134:                 }
135: 
136:             } elseif ($parameter->isOptional()) {
137:                 // PDO::__construct has optional parameter without default value (and isArray() and allowsNull() returns FALSE)
138:                 $res[$num] = $parameter->isDefaultValueAvailable() ? $parameter->getDefaultValue() : NULL;
139:                 $optCount++;
140: 
141:             } else {
142:                 throw new Nette\InvalidArgumentException("$parameter is missing.");
143:             }
144:         }
145: 
146:         // extra parameters
147:         while (array_key_exists(++$num, $arguments)) {
148:             $res[$num] = $arguments[$num];
149:             unset($arguments[$num]);
150:             $optCount = 0;
151:         }
152:         if ($arguments) {
153:             throw new Nette\InvalidArgumentException("Unable to pass specified arguments to $method.");
154:         }
155: 
156:         return $optCount ? array_slice($res, 0, -$optCount) : $res;
157:     }
158: 
159: }
160: 
Nette Framework 2.0.0 API API documentation generated by ApiGen 2.7.0