For example, in a queuing application, you can define a class type called 'job':
Libraries::paths(array('job' => '{:library}\extensions\job\{:name}'));
Then, any classes you add to the extensions/job directory in your application will be
automatically detected when calling Libraries::locate('job'). Additionally, any matching
classes in the extensions/job directory of any plugin or vendor library you add to your
application will also be detected.
Supposing you wanted to have the option of further organizing jobs by class type (some jobs
are related to updating caches, others to sending notifications, etc.), you can specify
multiple paths per class type, with varying levels of specificity:
Libraries::paths(array('job' => array(
'{:library}\extensions\job\{:class}\{:name}',
'{:library}\extensions\job\{:name}'
)));
This allows you to, for example, have two different classes called Cleanup. One may be
located in app\extensions\job\Cleanup, while the other is in
app\extensions\job\cache\Cleanup. Calling: Libraries::locate('job'); will find
both classes, while Libraries::locate('job.cache'); will only find the second. You can
also find individual jobs by name: Libraries::locate('job', 'Cleanup');
See Libraries::locate() for more information on using built-in and user-defined paths to
look up classes.
In addition to adding custom class types, paths() allows you to redefine the naming and
organization of existing types. For example, if you wished to reference your model classes
as app\models\PostModel instead of app\models\Post, you can do the following:
Libraries::paths(array('models' => '{:library}\models\{:name}Model'));
Note, however, that this is a destructive, not an additive operation, and will
replace any existing paths defined for that type. If you wish to add a search path
for an existing type, you must do the following:
$existing = Libraries::paths('controllers');
Libraries::paths(array('controller' => array_merge(
array('{:library}\extensions\controllers\{:name}Controller'), (array) $existing
)));
public function testPathTemplateWithGlobBrace() { Libraries::paths(array('analysis' => array('{:library}\\analysis\\*{Docblock,Debugger}'))); $analysis = list($docblock, $debugger) = Libraries::locate('analysis', null, array('recursive' => false, 'format' => false)); $this->assertCount(2, $analysis); $this->assertPattern('/Docblock\\.php/', $docblock); $this->assertPattern('/Debugger\\.php/', $debugger); }
/** * Exercise initialization. * * @return void */ public function _init() { parent::_init(); Libraries::paths(array('exercises' => '{:library}\\extensions\\exercises\\{:name}')); $exercises = Libraries::locate('exercises'); foreach ($exercises as $exercise) { $this->_exercises[$this->exerciseName($exercise)] = $exercise; } }
public function setUp() { Libraries::paths(array('models' => '{:library}\\tests\\mocks\\data\\model\\{:name}Model')); $this->classes = array('response' => 'lithium\\tests\\mocks\\console\\MockResponse'); $this->_backup['cwd'] = getcwd(); $this->_backup['_SERVER'] = $_SERVER; $_SERVER['argv'] = array(); $this->collection = $this->getCollection('li3_ensureindex\\tests\\mocks\\data\\model\\MockModel'); $this->collection->deleteIndexes(); }
public function testPathTemplate() { $expected = array('{:app}/libraries/{:name}', '{:root}/libraries/{:name}'); $result = Libraries::paths('libraries'); $this->assertEqual($expected, $result); $this->assertNull(Libraries::locate('authAdapter', 'Form')); $paths = Libraries::paths(); $test = array('authAdapter' => array('lithium\\security\\auth\\adapter\\{:name}')); Libraries::paths($test); $this->assertEqual($paths + $test, Libraries::paths()); $class = Libraries::locate('authAdapter', 'Form'); $expected = 'lithium\\security\\auth\\adapter\\Form'; $this->assertEqual($expected, $class); }
public function import() { Libraries::paths(array('neons' => array('{:library}\\data\\{:class}\\{:name}.neon'))); $libraries = Libraries::get(null, 'name'); $data = array(); $namespaces = true; foreach ($libraries as $library) { $files = Libraries::locate('neons', null, compact('namespaces', 'library')); if (!empty($files)) { $data[$library] = $files; } } return compact('data'); }
<?php use lithium\core\Libraries; $path = Libraries::get(true, 'path') . '/webroot/themes/default'; if (!is_dir($path)) { mkdir($path); rename(Libraries::get(true, 'path') . '/views', $path . '/views'); } $existing = Libraries::paths('helper'); Libraries::paths(array('helper' => array_merge(array('{:library}\\extensions\\helper\\{:class}\\{:name}', '{:library}\\template\\helper\\{:class}\\{:name}' => array('libraries' => 'li3_themes')), (array) $existing))); require __DIR__ . '/bootstrap/media.php'; require __DIR__ . '/bootstrap/errors.php';
/** * Loads entities and records from file-based structure * * Trys to implement a similar method to load datasets not from database, but rather from * a file that holds all relevant model data and is laid into the filesystem of each library. * This allows for easy default-data to be loaded without using a database as backend. * * Put your files into `{:library}\data\{:class}\{:name}.neon` and let the content be found * with loading just the id or slug of that file. * * Attention: This feature is considered experimental and should be used with care. It might * not work as expected. Also, not all features are implemented. * * If nothing is found, it just returns null or an empty array to ensure a falsey value. * * @param string|array $model fully namespaced model class, e.g. `radium\models\Contents` * can also be an array, in which case `key` and `source` must be given * according to the internal structure of `Model::meta()`. * @param string $type The find type, which is looked up in `Model::$_finders`. By default it * accepts `all`, `first`, `list` and `count`. Later two are not implement, yet. * @param array $options Options for the query. By default, accepts: * - `conditions`: The conditional query elements, e.g. * `'conditions' => array('published' => true)` * - `fields`: The fields that should be retrieved. When set to `null`, defaults to * all fields. * - `order`: The order in which the data will be returned, e.g. `'order' => 'ASC'`. * - `limit`: The maximum number of records to return. * - `page`: For pagination of data. * @return mixed returns null or an empty array if nothing is found * If `$type` is `first` returns null or the correct entity with given data * If `$type` is `all` returns null or a DocumentSet object with loaded entities */ public static function find($model, $type, array $options = array()) { $defaults = array('conditions' => null, 'fields' => null); $options += $defaults; $paths = self::$_paths; Libraries::paths($paths); $meta = is_array($model) ? $model : $model::meta(); $locate = sprintf('neons.%s', $meta['source']); $data = Libraries::locate($locate, null, array('namespaces' => true)); $files = new Collection(compact('data')); unset($data); $files->each(function ($file) { return str_replace('\\', '/', $file) . 'neon'; }); extract($options); if (isset($conditions['slug'])) { $field = 'slug'; } if (isset($conditions[$meta['key']])) { $field = $meta['key']; } if (!isset($field)) { $field = 'all'; // var_dump($field); // return array(); } $value = $conditions[$field]; switch (true) { case is_string($value): $pattern = sprintf('/%s/', $value); break; case isset($value['like']): $pattern = $value['like']; break; } if (isset($pattern)) { $filter = function ($file) use($pattern) { return (bool) preg_match($pattern, $file); }; } if (isset($filter)) { $files = $files->find($filter); } if (isset($order)) { // TODO: add sort } if ($type == 'count') { return count($files); } if ($type == 'list') { // TODO: implement me } if ($type == 'first' && count($files)) { $data = self::file($files->first()); $data[$field] = $value; if ($model === 'radium\\models\\Configurations') { $data['value'] = Neon::encode($data['value']); } return $model::create($data); } // we found one (!) file with name 'all.neon', this is the entire set if ($type == 'all' && count($files) == 1 && substr($files->first(), -strlen('all.neon')) === 'all.neon') { $rows = self::file($files->first()); $data = array(); foreach ($rows as $row) { $data[] = $model::create($row); } if (is_array($model)) { return new Collection(compact('data')); } $model = $meta['class']; return new DocumentSet(compact('data', 'model')); } if ($type == 'all' && count($files)) { $data = array(); foreach ($files as $file) { $current = self::file($file); if (is_array($model)) { $filename = File::name($file); $data[$filename] = $current; continue; } if ($model === 'radium\\models\\Configurations') { $current['value'] = Neon::encode($current['value']); } $data[] = $model::create($current); } if (is_array($model)) { return new Collection(compact('data')); } $model = $meta['class']; return new DocumentSet(compact('data', 'model')); } return false; }
<?php use lithium\core\Libraries; /** * Register paths for mail template helpers. */ $existing = Libraries::paths('helper'); Libraries::paths(array('helper' => array_merge(array('{:library}\\extensions\\helper\\{:class}\\{:name}', '{:library}\\template\\helper\\{:class}\\{:name}' => array('libraries' => 'li3_mailer')), (array) $existing))); /** * Add paths for delivery transport adapters from this library (plugin). */ $existing = Libraries::paths('adapter'); $key = '{:library}\\{:namespace}\\{:class}\\adapter\\{:name}'; $existing[$key]['libraries'] = array_merge((array) $existing[$key]['libraries'], (array) 'li3_mailer'); Libraries::paths(array('adapter' => $existing)); /* * Ensure the mail template resources path exists. */ $path = Libraries::get(true, 'resources') . '/tmp/cache/mails'; if (!is_dir($path)) { mkdir($path); } /** * Load the file that configures the delivery system. */ require __DIR__ . '/bootstrap/delivery.php';
<?php /** * Lithium: the most rad php framework * * @copyright Copyright 2010, Union of RAD (http://union-of-rad.org) * @license http://opensource.org/licenses/bsd-license.php The BSD License */ use lithium\core\Libraries; /** * This adds the `'behavior'` type to the list of recognized class types. You can look up the * behaviors available to your application by running `Libraries::locate('behavior')`. */ Libraries::paths(array( 'behavior' => array('{:library}\extensions\data\behavior\{:name}') )); ?>
public function testServiceLocateAll() { $result = Libraries::locate('tests'); $this->assertTrue(count($result) > 30); $expected = array('lithium\\template\\view\\adapter\\File', 'lithium\\template\\view\\adapter\\Simple'); $result = Libraries::locate('adapter.template.view'); $this->assertEqual($expected, $result); $result = Libraries::locate('test.filter'); $this->assertTrue(count($result) >= 4); $this->assertTrue(in_array('lithium\\test\\filter\\Affected', $result)); $this->assertTrue(in_array('lithium\\test\\filter\\Complexity', $result)); $this->assertTrue(in_array('lithium\\test\\filter\\Coverage', $result)); $this->assertTrue(in_array('lithium\\test\\filter\\Profiler', $result)); foreach (Libraries::paths() as $type => $paths) { if (count($paths) <= 1 || $type == 'libraries') { continue; } $this->assertTrue(count(Libraries::locate($type)) > 1); } }
<?php use lithium\core\Libraries; Libraries::paths(array('resources' => array('{:library}\\controllers\\{:namespace}\\{:class}\\{:name}'))); Libraries::add("Mockery", array('path' => dirname(__DIR__) . '/libraries/mockery/library', 'prefix' => ''));
<?php /** * Lithium: the most rad php framework * * @copyright Copyright 2011, Union of RAD (http://union-of-rad.org) * @license http://opensource.org/licenses/bsd-license.php The BSD License */ use lithium\core\Libraries; use lithium\g11n\Multibyte; Libraries::paths(array('rules' => array('{:library}\\extensions\\qa\\rules\\{:class}\\{:name}', '{:library}\\qa\\rules\\{:class}\\{:name}' => array('libraries' => 'li3_quality')))); Multibyte::config(array('li3_quality' => array('adapter' => 'Mbstring')));
<?php use lithium\core\Libraries; define('LI3_RESQUE_PATH', dirname(__DIR__)); define('LI3_RESQUE_LIB_PATH', LI3_RESQUE_PATH . '/libraries/Resque'); // load up third party lib // Libraries::add('resque', array( // 'path' => LI3_RESQUE_LIB_PATH, // 'prefix' => false, // 'transform' => function($class, $config) { // $class = str_replace("_", "/", $class); // return "{$config['path']}/{$class}{$config['suffix']}"; // } // )); if (Libraries::paths('job') === null) { Libraries::paths(array('job' => '{:library}\\extensions\\job\\{:name}')); } if (file_exists(__DIR__ . '/../vendor/autoload.php')) { require __DIR__ . '/../vendor/autoload.php'; }
<?php use lithium\core\Libraries; // Adapters nested under security/access/adapters instead // of under extensions until core has the paths changed. Libraries::paths(array('adapter' => array_merge(Libraries::paths('adapter'), array('{:library}\\{:namespace}\\{:class}\\adapter\\{:name}'))));
/** * @deprecated */ public function testPathTemplateWithGlobBrace() { error_reporting(($original = error_reporting()) & ~E_USER_DEPRECATED); Libraries::paths(array('analysis' => array('{:library}\\analysis\\*{Docblock,Debugger}'))); $analysis = list($docblock, $debugger) = Libraries::locate('analysis', null, array('recursive' => false, 'format' => false)); $this->assertCount(2, $analysis); $this->assertPattern('/Docblock\\.php/', $docblock); $this->assertPattern('/Debugger\\.php/', $debugger); error_reporting($original); }
<?php /** * Lithium: the most rad php framework * * @copyright Copyright 2010, Union of RAD (http://union-of-rad.org) * @license http://opensource.org/licenses/bsd-license.php The BSD License */ use lithium\core\Libraries; /** * This adds the `'behavior'` type to the list of recognized class types. You can look up the * behaviors available to your application by running `Libraries::locate('behavior')`. */ Libraries::paths(['behavior' => ['{:library}\\data\\model\\behavior\\{:name}', '{:library}\\extensions\\data\\behavior\\{:name}']]);
public function tearDown() { Libraries::paths($this->_backup); MockPosts::reset(); }