Returns or sets the the class path cache used for mapping class names to file paths, or
locating classes using Libraries::locate().
public static cache ( array $cache = null ) : array | ||
$cache | array | An array of keys and values to use when pre-populating the cache. Keys are either class names (which match to file paths as values), or dot-separated lookup paths used by `locate()` (which matches to either a single class or an array of classes). If `false`, the cache is cleared. |
return | array | Returns an array of cached class lookups, formatted per the description for `$cache`. |
public function setUp() { Libraries::cache(false); $this->classes = array('response' => 'lithium\\tests\\mocks\\console\\MockResponse'); $this->_backup['cwd'] = getcwd(); $this->_backup['_SERVER'] = $_SERVER; $_SERVER['argv'] = array(); chdir(LITHIUM_LIBRARY_PATH . '/lithium'); $this->request = new Request(array('input' => fopen('php://temp', 'w+'))); $this->request->params = array('library' => 'build_test'); }
public function setUp() { Libraries::cache(false); $this->classes = array('response' => 'lithium\\tests\\mocks\\console\\MockResponse'); $this->_backup['cwd'] = getcwd(); $this->_backup['_SERVER'] = $_SERVER; $_SERVER['argv'] = array(); Libraries::add('create_test', array('path' => $this->_testPath . '/create_test')); $this->request = new Request(array('input' => fopen('php://temp', 'w+'))); $this->request->params = array('library' => 'create_test'); }
* 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 */ /** * This file creates a default cache configuration using the most optimized adapter available, and * uses it to provide default caching for high-overhead operations. */ use lithium\storage\Cache; use lithium\core\Libraries; use lithium\action\Dispatcher; use lithium\storage\cache\adapter\Apc; /** * If APC is not available and the cache directory is not writeable, bail out. */ if (!($apcEnabled = Apc::enabled() && !is_writable(LITHIUM_APP_PATH . '/resources/tmp/cache'))) { return; } Cache::config(array('default' => array('adapter' => '\\lithium\\storage\\cache\\adapter\\' . ($apcEnabled ? 'Apc' : 'File')))); Dispatcher::applyFilter('run', function ($self, $params, $chain) { if ($cache = Cache::read('default', 'core.libraries')) { $cache = (array) unserialize($cache) + Libraries::cache(); Libraries::cache($cache); } $result = $chain->next($self, $params, $chain); if ($cache != Libraries::cache()) { Cache::write('default', 'core.libraries', serialize(Libraries::cache()), '+1 day'); } return $result; });
* removed post-install, and the cache should be configured with the adapter you plan to use. */ $cachePath = Libraries::get(true, 'resources') . '/tmp/cache'; if (!($apcEnabled = Apc::enabled()) && !is_writable($cachePath)) { return; } /** * This configures the default cache, based on whether ot not APC user caching is enabled. If it is * not, file caching will be used. Most of this code is for getting you up and running only, and * should be replaced with a hard-coded configuration, based on the cache(s) you plan to use. */ $default = array('adapter' => 'File', 'strategies' => array('Serializer')); if ($apcEnabled) { $default = array('adapter' => 'Apc'); } Cache::config(compact('default')); /** * Caches paths for auto-loaded and service-located classes. */ Dispatcher::applyFilter('run', function ($self, $params, $chain) { $key = md5(LITHIUM_APP_PATH) . '.core.libraries'; if ($cache = Cache::read('default', $key)) { $cache = (array) $cache + Libraries::cache(); Libraries::cache($cache); } $result = $chain->next($self, $params, $chain); if ($cache != Libraries::cache()) { Cache::write('default', $key, Libraries::cache(), '+1 day'); } return $result; });
* 2. Cache describe calls on all connections that use a `Database` based adapter. * * @see lithium\core\Environment * @see lithium\core\Libraries */ if (!Environment::is('production')) { return; } Dispatcher::applyFilter('run', function ($self, $params, $chain) { $cacheKey = 'core.libraries'; if ($cached = Cache::read('default', $cacheKey)) { $cached = (array) $cached + Libraries::cache(); Libraries::cache($cached); } $result = $chain->next($self, $params, $chain); if ($cached != ($data = Libraries::cache())) { Cache::write('default', $cacheKey, $data, '+1 day'); } return $result; }); Dispatcher::applyFilter('run', function ($self, $params, $chain) { foreach (Connections::get() as $name) { if (!($connection = Connections::get($name)) instanceof Database) { continue; } $connection->applyFilter('describe', function ($self, $params, $chain) use($name) { if ($params['fields']) { return $chain->next($self, $params, $chain); } $cacheKey = "data.connections.{$name}.sources.{$params['entity']}.schema"; return Cache::read('default', $cacheKey, array('write' => function () use($self, $params, $chain) {
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(); }
public function testRunGroupForTestAppModel() { $testApp = Libraries::get(true, 'resources') . '/tmp/tests/test_app'; mkdir($testApp); Libraries::add('test_app', array('path' => $testApp)); mkdir($testApp . '/tests/cases/models', 0777, true); file_put_contents($testApp . '/tests/cases/models/UserTest.php', "<?php namespace test_app\\tests\\cases\\models;\n\n\t\t\tclass UserTest extends \\lithium\\test\\Unit { public function testMe() {\n\t\t\t\t\$this->assertTrue(true);\n\t\t\t}}"); Libraries::cache(false); $group = new Group(array('data' => array('\\test_app\\tests\\cases'))); $expected = array('test_app\\tests\\cases\\models\\UserTest'); $result = $group->to('array'); $this->assertEqual($expected, $result); $expected = 'pass'; $result = $group->tests()->run(); $this->assertEqual($expected, $result[0][0]['result']); Libraries::cache(false); $this->_cleanUp(); }
public function testCaseSensitivePathLookups() { Libraries::cache(false); $library = Libraries::get('lithium'); $base = $library['path'] . '/'; $expected = $base . 'template/view.php'; $result = Libraries::path('\\lithium\\template\\view'); $this->assertEqual($expected, $result); $result = Libraries::path('lithium\\template\\view'); $this->assertEqual($expected, $result); $expected = $base . 'template/View.php'; $result = Libraries::path('\\lithium\\template\\View'); $this->assertEqual($expected, $result); $result = Libraries::path('lithium\\template\\View'); $this->assertEqual($expected, $result); $expected = $base . 'template/view'; $result = Libraries::path('\\lithium\\template\\view', array('dirs' => true)); $this->assertEqual($expected, $result); $result = Libraries::path('lithium\\template\\view', array('dirs' => true)); $this->assertEqual($expected, $result); }
use lithium\storage\Cache; use lithium\core\Libraries; use lithium\action\Dispatcher; use lithium\storage\cache\adapter\Apc; if (PHP_SAPI === 'cli') { return; } /** * If APC is not available and the cache directory is not writeable, bail out. This block should be * removed post-install, and the cache should be configured with the adapter you plan to use. */ if (!($apcEnabled = Apc::enabled()) && !is_writable(LITHIUM_APP_PATH . '/resources/tmp/cache')) { return; } if ($apcEnabled) { $default = array('adapter' => 'lithium\\storage\\cache\\adapter\\Apc'); } else { $default = array('adapter' => 'lithium\\storage\\cache\\adapter\\File', 'strategies' => array('Serializer')); } Cache::config(compact('default')); Dispatcher::applyFilter('run', function ($self, $params, $chain) { if ($cache = Cache::read('default', 'core.libraries')) { $cache = (array) $cache + Libraries::cache(); Libraries::cache($cache); } $result = $chain->next($self, $params, $chain); if ($cache != Libraries::cache()) { Cache::write('default', 'core.libraries', Libraries::cache(), '+1 day'); } return $result; });