If the given class can't be found, an exception is thrown.
public static instance ( string $type, string $name, array $options = [] ) : object | ||
$type | string | The type of class as defined by `Libraries::$_paths`. |
$name | string | The un-namespaced name of the class to instantiate. |
$options | array | An array of constructor parameters to pass to the class. |
리턴 | object | If the class is found, returns an instance of it, otherwise throws an exception. |
/** * Perform initialization. * * @return void */ protected function _init() { Object::_init(); $type = isset($this->_config['type']) ? $this->_config['type'] : null; if ($type === 'text') { $h = function ($data) { return $data; }; } else { $encoding = 'UTF-8'; if ($this->_message) { $encoding =& $this->_message->charset; } $h = function ($data) use(&$encoding) { return htmlspecialchars((string) $data, ENT_QUOTES, $encoding); }; } $this->outputFilters += compact('h') + $this->_config['outputFilters']; foreach (array('loader', 'renderer') as $key) { if (is_object($this->_config[$key])) { $this->{'_' . $key} = $this->_config[$key]; continue; } $class = $this->_config[$key]; $config = array('view' => $this) + $this->_config; $path = 'adapter.template.mail'; $instance = Libraries::instance($path, $class, $config); $this->{'_' . $key} = $instance; } }
/** * Initialize connection * */ protected function _init() { $config = array('classes' => $this->_classes) + $this->_config; try { $this->connection = Libraries::instance('socket', $config['socket'], $config); } catch (ClassNotFoundException $e) { $this->connection = null; } }
public function create($bindings = null, array $options = array()) { $result = parent::create($bindings, $options); if ($this->_binding) { $model = $this->_binding->model(); $this->model = $model; $this->instance = Libraries::instance('model', $model); $this->schema = $model::schema(); $this->rules = $this->instance->validates; } return $result; }
/** * Initialize connection * */ protected function _init() { $config = array('classes' => $this->_classes) + $this->_config; try { $this->connection = Libraries::instance('socket', $config['socket'], $config); } catch (ClassNotFoundException $e) { $this->connection = null; } $this->_responseTypes += array('headers' => function ($response) { return $response->headers; }, 'body' => function ($response) { return $response->body(); }, 'code' => function ($response) { return $response->status['code']; }); }
/** * Returns an instance of a class with given `config`. The `name` could be a key from the * `classes` array, a fully-namespaced class name, or an object. Typically this method is used * in `_init` to create the dependencies used in the current class. * * @param string|object $name A `classes` alias or fully-namespaced class name. * @param array $options The configuration passed to the constructor. * @return object */ protected static function _instance($name, array $options = array()) { $self = static::_object(); if (is_string($name) && isset($self->_classes[$name])) { $name = $self->_classes[$name]; } return Libraries::instance(null, $name, $options); }
/** * Perform initialization of the View. * * @return void */ protected function _init() { parent::_init(); $encoding = 'UTF-8'; if ($this->_response) { $encoding =& $this->_response->encoding; } $h = function($data) use (&$encoding) { return htmlspecialchars((string) $data, ENT_QUOTES, $encoding); }; $this->outputFilters += compact('h') + $this->_config['outputFilters']; foreach (array('loader', 'renderer') as $key) { if (is_object($this->_config[$key])) { $this->{'_' . $key} = $this->_config[$key]; continue; } $class = $this->_config[$key]; $config = array('view' => $this) + $this->_config; $this->{'_' . $key} = Libraries::instance('adapter.template.view', $class, $config); } }
/** * Returns an instance of a class with given `config`. The `name` could be a key from the * `classes` array, a fully namespaced class name, or an object. Typically this method is used * in `_init` to create the dependencies used in the current class. * * @param string|object $name A `classes` key or fully-namespaced class name. * @param array $options The configuration passed to the constructor. * @return object */ protected static function _instance($name, array $options = array()) { if (is_string($name) && isset(static::$_classes[$name])) { $name = static::$_classes[$name]; } return Libraries::instance(null, $name, $options); }
/** * Accepts parameters generated by the `Router` class in `Dispatcher::run()`, and produces a * callable controller object. By default, this method uses the `'controller'` path lookup * configuration in `Libraries::locate()` to return a callable object. * * @param object $request The instance of the `Request` class either passed into or generated by * `Dispatcher::run()`. * @param array $params The parameter array generated by routing the request. * @param array $options Not currently implemented. * @return object Returns a callable object which the request will be routed to. * @filter */ protected static function _callable($request, $params, $options) { $params = compact('request', 'params', 'options'); return static::_filter(__FUNCTION__, $params, function ($self, $params) { $options = array('request' => $params['request']) + $params['options']; $controller = $params['params']['controller']; try { return Libraries::instance('controllers', $controller, $options); } catch (ClassNotFoundException $e) { throw new DispatchException("Controller `{$controller}` not found.", null, $e); } }); }
/** * Retrieve instance of configured socket * * @param array $config options to be passed on to the socket * @return object */ public function &connection($config = array()) { $config += $this->_config; try { $this->connection = Libraries::instance('socket', $config['socket'], $config); } catch (ClassNotFoundException $e) { $this->connection = null; } return $this->connection; }
public static function bind($class) { $class::applyFilter('_callable', function ($self, $params, $chain) { $options = $params['options']; $name = $params['params']['controller']; if ($class = Libraries::locate('resources', $name, $options)) { if (strpos($class, 'Controller') === false) { return Libraries::instance(null, $class, $options); } } return $chain->next($self, $params, $chain); }); }
/** * Brokers access to helpers attached to this rendering context, and loads helpers on-demand if * they are not available. * * @param string $name Helper name * @param array $config * @return object */ public function helper($name, array $config = array()) { if (isset($this->_helpers[$name])) { return $this->_helpers[$name]; } try { $config += array('context' => $this); return $this->_helpers[$name] = Libraries::instance('helper', ucfirst($name), $config); } catch (ClassNotFoundException $e) { if (ob_get_length()) { ob_end_clean(); } throw new RuntimeException("Helper `{$name}` not found."); } }
public function testClassInstanceWithSubnamespace() { $testApp = Libraries::get(true, 'resources') . '/tmp/tests/test_app'; mkdir($testApp); $paths = array("/controllers", "/controllers/admin"); foreach ($paths as $path) { $namespace = str_replace('/', '\\', $path); $dotsyntax = str_replace('/', '.', trim($path, '/')); $class = 'Posts'; Libraries::add('test_app', array('path' => $testApp)); mkdir($testApp . $path, 0777, true); file_put_contents($testApp . $path . "/{$class}Controller.php", "<?php namespace test_app{$namespace};\n\n\t\t\t\tclass {$class}Controller extends \\lithium\\action\\Controller {\n\t\t\t\tpublic function index() {\n\t\t\t\t\treturn true;\n\t\t\t\t}}"); Libraries::cache(false); $expected = "test_app{$namespace}\\{$class}Controller"; $instance = Libraries::instance($dotsyntax, "Posts", array('library' => 'test_app')); $result = get_class($instance); $this->assertEqual($expected, $result, "{$path} did not work"); } $this->_cleanUp(); }
/** * Brokers access to helpers attached to this rendering context, and loads helpers on-demand if * they are not available. * * @param string $name Helper name * @param array $config * @return object */ public function helper($name, array $config = array()) { if (isset($this->_helpers[$name])) { return $this->_helpers[$name]; } try { $config += array('context' => $this); $helper = Libraries::instance('helper.mail', ucfirst($name), $config); return $this->_helpers[$name] = $helper; } catch (ClassNotFoundException $e) { throw new RuntimeException("Mail helper `{$name}` not found."); } }
public function testServiceLocateInstantiation() { $result = Libraries::instance('adapter.template.view', 'Simple'); $this->assertTrue(is_a($result, 'lithium\\template\\view\\adapter\\Simple')); $this->expectException("Class 'Foo' of type 'adapter.template.view' not found."); $result = Libraries::instance('adapter.template.view', 'Foo'); }