/** * Set all definitions into array * @param $propertyInjections * @throws \Exception */ public function setPropertyInjection($propertyInjections) { if (!is_array($propertyInjections)) { throw new \Exception(__METHOD__ . " only accept parameter as array."); } foreach ($propertyInjections as $controller => $properties) { foreach ($properties as $key => $value) { $classInstance = Inflector::toNamespace($value); $this->definitions["\\" . ucfirst(APPPATH) . $this->namespace . $controller][$key] = new $classInstance(); } } }
/** * Set all definitions into array. * * @param $propertyInjections * * @throws \Cygnite\DependencyInjection\Exceptions\DependencyException * * @return $this */ public function setPropertyInjection($propertyInjections) { if (!is_array($propertyInjections)) { throw new DependencyException(__METHOD__ . ' only accept parameter as array.'); } $namespace = $this->getAppNamespace(); foreach ($propertyInjections as $controller => $properties) { foreach ($properties as $key => $value) { /* | We will add key value pair into the definition array only if | it is not already exists */ if (!isset($this->cache[$namespace . $controller][$key])) { $classNs = Inflector::toNamespace($value); $this->definitions['\\' . $namespace . $controller][$key] = $classNs; } } } return $this; }
/** * All static methods will get resolve by Proxy Resolver * * @param $method * @param array $arguments * @return mixed */ public static function __callStatic($method, $arguments = array()) { $accessor = $instance = null; $accessor = static::getResolver(); $accessor = Inflector::toNamespace($accessor); $instance = new $accessor(); // calling the method directly is faster then call_user_func_array() ! switch (count($arguments)) { case 0: return $instance->{$method}(); case 1: return $instance->{$method}($arguments[0]); case 2: return $instance->{$method}($arguments[0], $arguments[1]); case 3: return $instance->{$method}($arguments[0], $arguments[1], $arguments[2]); case 4: return $instance->{$method}($arguments[0], $arguments[1], $arguments[2], $arguments[3]); default: return call_user_func_array(array($instance, $method), $arguments); } }
/** * Resolve the class. We will create and return instance if already * not exists. * * @param $class * @param array $arguments * * @return object */ public function resolve($class, $arguments = []) { $class = Inflector::toNamespace($class); return $this->make($class, $arguments); }
public function testToNamespaceMethod() { $this->assertEquals('\\Foo\\Bar\\Baz', Inflector::toNamespace('foo.bar.baz')); }