// Library Constants defined('FRONTENDER_LIBS') or define('FRONTENDER_LIBS', FRONTENDER_PATH . "/libraries"); defined('FRONTENDER_SRC') or define('FRONTENDER_SRC', FRONTENDER_LIBS . "/assetic/src/Assetic"); defined('CACHE_DIR') or define('CACHE_DIR', Libraries::get(true, 'resources') . "/tmp/cache"); /** * Load in project dependancies which include * LessPHP, Assetic and Symfony Process component */ require __DIR__ . '/libraries.php'; Dispatcher::applyFilter('run', function ($self, $params, $chain) use($options) { $assets = array('css' => array('match' => "/.css\$/", 'type' => 'text/css', 'name' => 'Stylesheet'), 'js' => array('match' => "/.js\$/", 'type' => 'text/javascript', 'name' => 'Script')); foreach (array_keys($assets) as $type) { $config = $assets[$type]; $config['cacheOnly'] = $options['cacheOnly']; if (preg_match($config['match'], $params['request']->url)) { if (readCache($params['request'], $config)) { return; } } } return $chain->next($self, $params, $chain); }); /** * Read cache files for assets * Returns CSS/Js from Cache if exists * @param object $request * @return string contents of cache file */ function readCache($request, array $options) { $http_response_code = null;
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) { return array('+1 day' => $chain->next($self, $params, $chain)); })); }); } return $chain->next($self, $params, $chain); });
* 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; });
* * Intercepts dispatching processes in order to set the effective locale by using * the locale of the request or if that is not available retrieving a locale preferred * by the client. * * @see lithium\g11n\Message * @see lithium\core\Environment */ $setLocale = function ($self, $params, $chain) { if (!$params['request']->locale()) { $params['request']->locale(Locale::preferred($params['request'])); } Environment::set(true, array('locale' => $params['request']->locale())); return $chain->next($self, $params, $chain); }; ActionDispatcher::applyFilter('_callable', $setLocale); ConsoleDispatcher::applyFilter('_callable', $setLocale); /** * Resources * * Globalization (g11n) catalog configuration. The catalog allows for obtaining and * writing globalized data. Each configuration can be adjusted through the following settings: * * - `'adapter'` _string_: The name of a supported adapter. The builtin adapters are `Memory` (a * simple adapter good for runtime data and testing), `Php`, `Gettext`, `Cldr` (for * interfacing with Unicode's common locale data repository) and `Code` (used mainly for * extracting message templates from source code). * * - `'path'` All adapters with the exception of the `Memory` adapter require a directory * which holds the data. *
Dispatcher::applyFilter('_callable', function ($self, $params, $chain) { //var_dump($params['params']); //exit(); if (isset($params['params']['library'])) { // Instead of using LITHIUM_APP_PATH,for future compatibility. $defaultAppConfig = Libraries::get(true); $appPath = $defaultAppConfig['path']; $libConfig = Libraries::get($params['params']['library']); /** * LAYOUTS AND TEMPLATES * Note the path ordering for how templates override others. * First, your overrides and then the default render paths for a library. * Second to last, it tries to grab what it can from the main application. * Last (worst case) it tries to use what's in Lithium Bootstrap. * * The last scenario is rare, if using a "default" layout, for example, * it likely exists in the main application already. If a library is * specifcially designed for Lithium Bootstrap and wishes to use * templates within li3b_core before looking in the main application, * they should be added with the proper configuration settings. */ $paths['layout'] = array($appPath . '/views/_libraries/' . $params['params']['library'] . '/layouts/{:layout}.{:type}.php', '{:library}/views/layouts/{:layout}.{:type}.php', $appPath . '/views/layouts/{:layout}.{:type}.php', $appPath . '/libraries/li3b_core/views/layouts/{:layout}.{:type}.php'); $paths['template'] = array($appPath . '/views/_libraries/' . $params['params']['library'] . '/{:controller}/{:template}.{:type}.php', '{:library}/views/{:controller}/{:template}.{:type}.php', $appPath . '/views/{:controller}/{:template}.{:type}.php', $appPath . '/libraries/li3b_core/views/{:controller}/{:layout}.{:type}.php'); /* * Condition #4 here. This will prefer Lithium Bootstrap's core layouts. * Libraries added with this configuration option were designed specifically * for use with Lithium Bootstrap and wish to use it's default design. * * Of course, there is still template fallback support in case the user * has changed up their copy of Lithium Bootstrap...But the library is * now putting the priority on the Lithium Bootstrap layouts, unless * overridden by templates in the _libraries directory of the main app. * * There is currently no need to do the same with templates since the * li3b_core library has so few view templates...And they don't even make * sense to share for any other purpose whereas layouts are definitely * something another action can take advantage of. */ if (isset($libConfig['useBootstrapLayout']) && (bool) $libConfig['useBootstrapLayout'] === true) { $paths['layout'] = array($appPath . '/views/_libraries/' . $params['params']['library'] . '/layouts/{:layout}.{:type}.php', $appPath . '/libraries/li3b_core/views/layouts/{:layout}.{:type}.php', '{:library}/views/layouts/{:layout}.{:type}.php', $appPath . '/views/layouts/{:layout}.{:type}.php'); } /** * ELEMENTS * This will allow the main application to still render it's elements * even though the View() class may be dealing with one of this library's * controllers, which would normally suggest the element comes from the library * Again, note the ordering here for how things override others. * 1. Your overrides are considered first. * 2. Elements that may come with the library are used when a library key is used. * 3. The main application is checked for the element templates (this functions as normal out of the box Lithium). * 4. Lithium Bootstrap elements. Last ditch effort to find the element. * Note: When you wish to use an element from Lithium Bootstrap, you should * pass a library key to be certain it is used. Otherwise, if you have an * element in your main application by the same name as one from Lithium * Bootstrap, you could be using that instead when you did not intend to. * All of the elements rendered from li3b_core pass a library key and * your plugins, wishing to use core li3b elements, should do the same. */ $paths['element'] = array($appPath . '/views/_libraries/' . $params['params']['library'] . '/elements/{:template}.{:type}.php', '{:library}/views/elements/{:template}.{:type}.php', $appPath . '/views/elements/{:template}.{:type}.php', $appPath . '/libraries/li3b_core/views/elements/{:template}.{:type}.php'); $params['options']['render']['paths'] = $paths; } /** * Allow the main application to use Lithium Bootstrap's admin layout template and elements. * This helps to speed up development without the need to always create libraries for everything. */ if (isset($params['params']['admin']) && $params['params']['admin'] === true && !isset($params['params']['library'])) { $defaultAppConfig = Libraries::get(true); $appPath = $defaultAppConfig['path']; $paths['layout'] = array($appPath . '/views/layouts/{:layout}.{:type}.php', $appPath . '/libraries/li3b_core/views/layouts/{:layout}.{:type}.php'); $paths['template'] = array($appPath . '/views/{:controller}/{:template}.{:type}.php', $appPath . '/libraries/li3b_core/views/{:controller}/{:template}.{:type}.php'); // Allow admin elements to be overridden for the main app looking to use the admin templates. // There is the top nav element as well as the footer...But if they aren't being overwritten, // simply use the templates that exist in li3b_core. $paths['element'] = array($appPath . '/views/elements/{:template}.{:type}.php', $appPath . '/libraries/li3b_core/views/elements/{:template}.{:type}.php'); $params['options']['render']['paths'] = $paths; } return $chain->next($self, $params, $chain); });
* $posts = Post::find('all'); * return $posts->to('json'); * }}} */ use lithium\util\Collection; Collection::formats('lithium\\net\\http\\Media'); /** * This filter is a convenience method which allows you to automatically route requests for static * assets stored within active plugins. For example, given a JavaScript file `bar.js` inside the * `li3_foo` plugin installed in an application, requests to `http://app/path/li3_foo/js/bar.js` * will be routed to `/path/to/app/libraries/plugins/li3_foo/webroot/js/bar.js` on the filesystem. * In production, it is recommended that you disable this filter in favor of symlinking each * plugin's `webroot` directory into your main application's `webroot` directory, or adding routing * rules in your web server's configuration. */ use lithium\action\Dispatcher; use lithium\action\Response; use lithium\net\http\Media; Dispatcher::applyFilter('_callable', function ($self, $params, $chain) { list($library, $asset) = explode('/', $params['request']->url, 2) + array("", ""); if ($asset && ($path = Media::webroot($library)) && file_exists($file = "{$path}/{$asset}")) { return function () use($file) { $info = pathinfo($file); $media = Media::type($info['extension']); $content = (array) $media['content']; return new Response(array('headers' => array('Content-type' => reset($content)), 'body' => file_get_contents($file))); }; } return $chain->next($self, $params, $chain); }); Media::type('js', array('application/javascript', 'text/javascript'), array('view' => 'lithium\\template\\View', 'paths' => array('template' => '{:library}/views/{:controller}/{:template}.{:type}.php', 'layout' => '{:library}/views/layouts/{:layout}.{:type}.php', 'element' => array('{:library}/views/elements/{:template}.{:type}.php', '{:library}/views/elements/{:template}.html.php'))));
Dispatcher::applyFilter('run', function ($self, $params, $chain) { if (substr($params['request']->url, 0, 17) == '/li3_perf/profile') { return $chain->next($self, $params, $chain); } Data::append('timers', array('li3_perf_start_dispatch' => microtime(true))); $result = $chain->next($self, $params, $chain); // Mark the end of li3_perf. // Note: The time it takes to render the toolbar will not be included. Data::append('timers', array('li3_perf_end' => microtime(true))); // Render the toolbar (unless it's an asset from the li3_perf library) // Why? See li3_perf\extensions\util\Asset $content_type = isset($result->headers['Content-Type']) ? $result->headers['Content-Type'] : ''; $content_type = explode(';', $content_type, 2); $content_type = array_shift($content_type); if (!isset($params['request']->params['asset_type']) && (!$content_type || $content_type == 'text/html')) { $skip = false; $li3_perf = Libraries::get('li3_perf'); if (isset($li3_perf['skip'])) { $controller = isset($params['request']->params['controller']) ? $params['request']->params['controller'] : null; $action = isset($params['request']->params['action']) ? $params['request']->params['action'] : null; $library = isset($params['request']->params['library']) ? $params['request']->params['library'] : null; // Check to see if the toolbar should be shown for this library if (isset($li3_perf['skip']['library'])) { if (in_array($library, $li3_perf['skip']['library'])) { $skip = true; } } // Check to see if the toolbar should be shown for this controller if (isset($li3_perf['skip']['controller'])) { if (in_array($controller, $li3_perf['skip']['controller'])) { $skip = true; } } // Check to see if the toolbar should be shown for this action if (isset($li3_perf['skip']['action'])) { if (in_array($action, $li3_perf['skip']['action'])) { $skip = true; } } } if ($skip || !isset($result->body[0])) { return $result; } $timers = Data::get('timers') + array('li3_perf_start' => 0, 'li3_perf_end' => 0, 'li3_perf_start_dispatch' => 0, 'li3_perf_has_route' => 0, 'li3_perf_start_call' => 0, 'li3_perf_end_call' => 0, '_filter_for_variables' => 0, '_filter_for_queries' => 0); $View = new View(array('paths' => array('template' => '{:library}/views/elements/{:template}.{:type}.php', 'layout' => '{:library}/views/layouts/{:layout}.{:type}.php'))); $toolbar = $View->render('all', array('timers' => $timers += array('dispatch_cycle' => $timers['li3_perf_end'] - $timers['li3_perf_start_dispatch'], 'routing' => $timers['li3_perf_has_route'] - $timers['li3_perf_start_dispatch'], 'call' => isset($timers['li3_perf_end_call']) && isset($timers['li3_perf_start_call']) ? $timers['li3_perf_end_call'] - $timers['li3_perf_start_call'] : 0, 'complete_load_with_li3_perf' => microtime(true) - $timers['li3_perf_start'], 'complete_load' => $timers['li3_perf_end'] - $timers['li3_perf_start'] - $timers['_filter_for_variables'] - $timers['_filter_for_queries']), 'vars' => array('request' => $params['request']->params, 'view' => Data::get('view_vars')), 'queries' => Data::get('queries')), array('library' => 'li3_perf', 'template' => 'toolbar', 'layout' => 'default')); if (preg_match('/<!--\\s*LI3_PERF_TOOLBAR\\s*-->/si', $result->body[0], $match)) { $result->body[0] = str_replace($match[0], $toolbar, $result->body[0]); } else { $result->body[0] = $toolbar . $result->body[0]; } } return $result; });
require dirname(__DIR__) . '/libraries/lessphp/lessc.inc.php'; /** * TODO: Make sure, that subfolders are possible (currently not) * */ Dispatcher::applyFilter('run', function ($self, $params, $chain) { if (strstr($params['request']->url, '.css')) { // look for a matching less file $basename = basename($params['request']->url); $css_file = "less/{$basename}"; $less_file = "less/{$basename}.less"; if (file_exists($less_file)) { header('Content-Type: text/css'); // if there's an up-to-date css file, serve it if (file_exists($css_file) && filemtime($css_file) >= filemtime($less_file)) { return file_get_contents($css_file); } try { $less = new lessc($less_file); $output = array('/**', ' * generated ' . date('r'), ' * by li3_less/lessphp', ' * ', ' * @link https://github.com/glaszig/li3_less', ' * @link http://leafo.net/lessphp', ' */'); $output = join(PHP_EOL, $output) . PHP_EOL . $less->parse(); } catch (Exception $e) { header('Content-Type: text/css', true, 500); $output = "/* less compiler exception: {$e->getMessage()} */"; } file_put_contents("less/{$basename}", $output); return $output; } } return $chain->next($self, $params, $chain); });
<?php /** * Initialize code index. */ use lithium\core\Libraries; use lithium\action\Dispatcher; use lithium\console\Dispatcher as ConsoleDispatcher; use li3_docs\extensions\docs\Code; $filter = function ($self, $params, $chain) { $indexPath = Libraries::get(true, 'path') . '/resources/docs.index.json'; if (file_exists($indexPath) && is_readable($indexPath)) { Code::index((array) json_decode(file_get_contents($indexPath), true)); } $result = $chain->next($self, $params, $chain); if (($index = Code::index()) && is_array($index) && is_writable(dirname($indexPath))) { file_put_contents($indexPath, json_encode($index)); } return $result; }; Dispatcher::applyFilter('run', $filter); ConsoleDispatcher::applyFilter('run', $filter); /** * Setup default options: * * - `'index'` _array|void_: Allows to restrict indexing to provided set of libraries. * By default all libraries registered in the application are indexed. * - `'categories'` _array|void_: Allows manually provide a set of category names. By * default categories are extracted from all indexed libraries. */ Libraries::add('li3_docs', array('bootstrap' => false) + Libraries::get('li3_docs') + array('url' => '/docs', 'index' => null, 'categories' => null));
if (!class_exists('li3_access\\security\\Access')) { Libraries::add('li3_access'); } Dispatcher::applyFilter('_callable', function ($self, $params, $chain) { // Run other filters first. This allows this one to not exactly be overwritten or excluded...But it does allow for a different login action to be used... // TODO: Perhaps allow this to be skipped... $next = $chain->next($self, $params, $chain); $request = $params['request']; $action = $request->action; $user = Auth::check('li3b_user'); // Protect all admin methods except for login and logout. if ($request->admin === true && $action != 'login' && $action != 'logout') { $action_access = Access::check('default', $user, $request, array('rules' => array('allowManagers'))); if (!empty($action_access)) { FlashMessage::write($action_access['message'], 'default'); if ($user) { header('Location: ' . Router::match($action_access['redirect'])); } else { header('Location: ' . Router::match(array('library' => 'li3b_users', 'controller' => 'users', 'action' => 'login'))); } // None shall pass. exit; } } // Sets the current user in each request for convenience. $params['request']->user = $user; return $next; // return $chain->next($self, $params, $chain); }); Access::config(array('default' => array('adapter' => 'Rules', 'filters' => array()))); // Set some basic rules to be used from anywhere // Allow access for users with a role of "administrator" or "content_editor"
<?php /** * radium: lithium application framework * * @copyright Copyright 2013, brünsicke.com GmbH (http://bruensicke.com) * @license http://opensource.org/licenses/BSD-3-Clause The BSD License */ define('RADIUM_PATH', dirname(__DIR__)); require __DIR__ . '/bootstrap/media.php'; require __DIR__ . '/bootstrap/validators.php'; use radium\extensions\storage\FlashMessage; use lithium\action\Dispatcher; Dispatcher::applyFilter('_callable', function ($self, $params, $chain) { return FlashMessage::bindTo($chain->next($self, $params, $chain)); }); // use radium\models\BaseModel; // if (!BaseModel::finder('random')) { // BaseModel::finder('random', function($self, $params, $chain){ // $amount = $self::find('count', $params['options']); // $offset = rand(0, $amount-1); // $params['options']['offset'] = $offset; // return $self::find('first', $params['options']); // }); // }
use lithium\action\Dispatcher; /** * This filter loads all application routes in all plugins, loading the default application routes * last. Change this code if plugin routes must be loaded in a specific order, or if application * routes must be loaded first (in which case the catch-all routes should be removed). If * `Dispatcher::run()` is called multiple times in the course of a single request, change the * `include`s to `include_once`. * * @see lithium\net\http\Router */ Dispatcher::applyFilter('run', function ($self, $params, $chain) { foreach (array_reverse(Libraries::get()) as $name => $config) { if ($name === 'lithium') { continue; } $file = "{$config['path']}/config/routes.php"; file_exists($file) ? include $file : null; } return $chain->next($self, $params, $chain); }); /** * Intercepts the `Dispatcher` as it finds a controller object, and passes the `'request'` parameter * to the `Environment` class to detect which environment the application is running in. * * @see lithium\action\Request * @see lithium\core\Environment */ Dispatcher::applyFilter('_callable', function ($self, $params, $chain) { Environment::set($params['request']); return $chain->next($self, $params, $chain); });
<?php use app\services\OauthWeixinUserService; use app\extensions\util\HttpUserAgentUtil; use lithium\action\Response; use app\services\UserService; use lithium\net\http\Router; use lithium\action\Dispatcher; /** * 拦截登录过滤器, 检测页面访问权限. */ Dispatcher::applyFilter('run', function ($self, $params, $chain) { $router = Router::parse($params['request']); if (empty($router->params)) { return $chain->next($self, $params, $chain); } else { $router = $router->params; } if (UserService::check_auth($router)) { // 如果是微信里的页面, 则需要注入当前的微信用户信息 if (HttpUserAgentUtil::is_weixin()) { OauthWeixinUserService::set_current_oauth_user(); } return $chain->next($self, $params, $chain); } else { $login_url = SYS_PATH . '/user/login?ref_url=' . urldecode(current_url()); return new Response(array('location' => $login_url)); } });
$a[$key] = str_replace('_', '.', substr($version, $underPosition)); } $latestVersion = max($a); $ctrl->request->params['version'] = $latestVersion; } else { if (!in_array($params['params']['action'], $methodNames)) { //version requested does not exist //TODO: return error response } } return $ctrl; }); Dispatcher::applyFilter('_call', function ($self, $params, $chain) { if (!isset($params['params']['version'])) { if (isset($params['callable']->request->params['version'])) { if (is_numeric($params['callable']->request->params['version'])) { $params['params']['version'] = $params['callable']->request->params['version']; } } } if (isset($params['params']['version'])) { if (is_numeric($params['params']['version']) && $params['params']['version'] !== 0) { $params['params']['action'] .= '_' . str_replace('.', '_', $params['params']['version']); } } $ctrl = $chain->next($self, $params, $chain); return $ctrl; }); ?> <?php
use lithium\core\Libraries; use lithium\action\Dispatcher; use li3_varnish\extensions\util\Varnish; use lithium\net\http\Media; use lithium\net\http\Router; // filter to set varnish headers Dispatcher::applyFilter('_call', function ($self, $params, $chain) { $response = $chain->next($self, $params, $chain); $cacheKey = $params['request']->params['controller'] . 'Controller::' . $params['request']->params['action']; if (isset($response->varnish) && !empty($response->varnish)) { $cache = Varnish::cache($cacheKey, true); if (is_array($response->varnish)) { $cache += $response->varnish; } } else { $cache = Varnish::cache($cacheKey); } if (!empty($cache)) { $varnishHeaders = Varnish::headers($cache); foreach ($varnishHeaders as $key => $val) { $response->headers($key, $val); } } return $response; }); // filter to set esi includes around partials Media::applyFilter('view', function ($self, $params, $chain) { $view = $chain->next($self, $params, $chain); $view->applyFilter('_step', function ($self, $params, $chain) { $content = $chain->next($self, $params, $chain); if (isset($params['options']['esi']) && $params['options']['esi'] == true) {
/** * This filter is a convenience method which allows you to automatically route requests for static * assets stored within active plugins. For example, given a JavaScript file `bar.js` inside the * `li3_foo` plugin installed in an application, requests to `http://app/path/li3_foo/js/bar.js` * will be routed to `/path/to/app/libraries/plugins/li3_foo/webroot/js/bar.js` on the filesystem. * In production, it is recommended that you disable this filter in favor of symlinking each * plugin's `webroot` directory into your main application's `webroot` directory, or adding routing * rules in your web server's configuration. */ use \lithium\action\Dispatcher; use \lithium\core\Libraries; use \lithium\net\http\Media; Dispatcher::applyFilter('_callable', function($self, $params, $chain) { list($plugin, $asset) = explode('/', $params['request']->url, 2) + array("", ""); if ($asset && $library = Libraries::get($plugin)) { $asset = "{$library['path']}/webroot/{$asset}"; if (file_exists($asset)) { return function () use ($asset) { $info = pathinfo($asset); $type = Media::type($info['extension']); header("Content-type: {$type['content']}"); return file_get_contents($asset); }; } } return $chain->next($self, $params, $chain); }); ?>
/** * If you specify `'appName'` when you add `li3_backend` it will be used as value of * `LITHIUM_APP_NAME` define. */ $LITHIUM_APP_NAME = Libraries::get('li3_backend', 'appName'); $LITHIUM_APP_NAME || ($LITHIUM_APP_NAME = 'Lithium app'); defined('LITHIUM_APP_NAME') || define('LITHIUM_APP_NAME', $LITHIUM_APP_NAME); /** * Convert `action` to `backend_action` for routes with `backend` prefix. * Eg. {{{ * /backend/home/index * `array('controller' => 'home', 'action' => 'backend_index')` * }}} */ Dispatcher::applyFilter('run', function ($self, $params, $chain) { $self::config(array('rules' => array('backend' => array('action' => 'backend_{:action}')))); return $chain->next($self, $params, $chain); }); /** * Run `backend_bootstrap` if someone access to backend */ Dispatcher::applyFilter('_callable', function ($self, $params, $chain) { if (isset($params['request']->params['backend']) && $params['request']->params['backend']) { foreach (Libraries::get(null, 'path') as $path) { if (file_exists($bootstrap = $path . '/config/backend_bootstrap.php')) { include_once $bootstrap; } } } return $chain->next($self, $params, $chain); });
Dispatcher::applyFilter('_callable', function($self, $params, $chain) { var_dump(MINERVA_APP_PATH . DIRECTORY_SEPARATOR . 'libraries' . DIRECTORY_SEPARATOR . 'common'); /* * So the problem with redirects and building requests is that since the "app" folder was changed to "minerva" * the "_base" property is not set properly. In the Request class there's a method called base() that sets it. * It basically does a string replace on "app/webroot" ... But we have "minerva/webroot" So we can change the * /minerva/webroot/index.php file and pass in an empty base key of "" to fix the issue. * I would rather set it here in the filter since that's where all the major changes are taking place. * I'd like to limit changes to a specific area to avoid complexity...But _base is protected as well as base(). * So we can't set it here. It can only be set by instantiation. * Alternatively we can write a new class (extending Request) and use that instead... * TODO: Look into that and in general a sub dispatcher that might avoid several issues and clean up this code. * * For now the index.php file has been changed, but that may cause problems elsewhere. Not sure yet. * Now all the redirects don't show a URL of site.com/minerva/blog it will be the expected site.com/blog * Both work though. */ // Don't apply this for test cases if($params['request']->params['controller'] == '\lithium\test\Controller') { return $chain->next($self, $params, $chain); } // Get the library if provided from the route params // (Note: Pages, Users, and Blocks are the only models considered, if any additional are created, the following array must change) // TODO: ...which is why... consider going back to a standard field name, it makes for less if thens....but it could create more problems for several reasons when it comes to 3rd party addons...the if thens here guarantee things to a good degree // this array is defined here and also in MinervaController.php // todo: or maybe just put elsewhere; better maintainability.. can't go back to a standard field name once distributed. this is a very critical thing $library_fields = array('page_type', 'user_type', 'block_type'); foreach($library_fields as $field) { if(in_array($field, array_keys($params['request']->params))) { $library = (isset($params['request']->params[$field])) ? $params['request']->params[$field]:null; } } // The admin flag from routes helps give control over the templates to use $admin = ((isset($params['request']->params['admin'])) && ($params['request']->params['admin'] == 1 || $params['request']->params['admin'] === true || $params['request']->params['admin'] == 'true')) ? true:false; // The layout key from the routes give us even more control, it's the final authority on where to check, but things do cascade down $layout = (isset($params['request']->params['layout'])) ? $params['request']->params['layout']:false; // Also a template key from the routes again for more control and flexibility $template = (isset($params['request']->params['template'])) ? $params['request']->params['template']:false; // DEFAULTS & MISSING TEMPLATE PAGES $params['options']['render']['paths']['layout'] = array( MINERVA_APP_PATH . DIRECTORY_SEPARATOR . 'libraries' . DIRECTORY_SEPARATOR . 'common' . DIRECTORY_SEPARATOR . 'views' . DIRECTORY_SEPARATOR . 'layouts' . DIRECTORY_SEPARATOR . '{:layout}.{:type}.php', MINERVA_APP_PATH . DIRECTORY_SEPARATOR . 'libraries' . DIRECTORY_SEPARATOR . 'common' . DIRECTORY_SEPARATOR . 'views' . DIRECTORY_SEPARATOR . '_missing' . DIRECTORY_SEPARATOR . 'missing_layout.{:type}.php' ); $params['options']['render']['paths']['template'] = array( MINERVA_APP_PATH . DIRECTORY_SEPARATOR . 'libraries' . DIRECTORY_SEPARATOR . 'common' . DIRECTORY_SEPARATOR . 'views' . DIRECTORY_SEPARATOR . '{:controller}' . DIRECTORY_SEPARATOR . '{:template}.{:type}.php', MINERVA_APP_PATH . DIRECTORY_SEPARATOR . 'libraries' . DIRECTORY_SEPARATOR . 'common' . DIRECTORY_SEPARATOR . 'views' . DIRECTORY_SEPARATOR . '_missing' . DIRECTORY_SEPARATOR . 'missing_template.{:type}.php' ); /** * DEFAULT FOR PAGE/USER/BLOCK TYPES * First by default we're going to see if this is even a controller that has a bridge model. * $library in this case is NOT the route's "library" key, if provided, it's the library name for when * bridging either a page, user, or a block. We want to use templates from that library's views folder. * This is not for admin view templates. This is for ex. /minerva/libraries/blog/views/pages/read.html.php */ if((!empty($library)) && (empty($admin))) { // Look at a common if the bridge library doesn't have the templates array_unshift($params['options']['render']['paths']['layout'], MINERVA_APP_PATH . DIRECTORY_SEPARATOR . 'libraries' . DIRECTORY_SEPARATOR . 'common' . DIRECTORY_SEPARATOR . 'views' . DIRECTORY_SEPARATOR . 'layouts' . DIRECTORY_SEPARATOR . '{:layout}.{:type}.php'); array_unshift($params['options']['render']['paths']['template'], MINERVA_APP_PATH . DIRECTORY_SEPARATOR . 'libraries' . DIRECTORY_SEPARATOR . 'common' . DIRECTORY_SEPARATOR . 'views' . DIRECTORY_SEPARATOR . '{:controller}' . DIRECTORY_SEPARATOR . '{:template}.{:type}.php'); // These will be on top of the array so it'll look first for something like: minerva/libraries/blog/views/... array_unshift($params['options']['render']['paths']['layout'], MINERVA_APP_PATH . DIRECTORY_SEPARATOR . 'libraries' . DIRECTORY_SEPARATOR . $library . DIRECTORY_SEPARATOR . 'views' . DIRECTORY_SEPARATOR . 'layouts' . DIRECTORY_SEPARATOR . '{:layout}.{:type}.php'); array_unshift($params['options']['render']['paths']['template'], MINERVA_APP_PATH . DIRECTORY_SEPARATOR . 'libraries' . DIRECTORY_SEPARATOR . $library . DIRECTORY_SEPARATOR . 'views' . DIRECTORY_SEPARATOR . '{:controller}' . DIRECTORY_SEPARATOR . '{:template}.{:type}.php'); } /** * 3RD PARTY LIBRARIES * If the route passed a "library" key then we're going to render from it's views folder. * This is likely for a 3rd party library that is stand alone, it doesn't hook into pages, users, blocks, etc. * This is so other applications can be dropped in more easily without template confusion or conflict. */ if(isset($params['request']->params['library'])) { array_unshift($params['options']['render']['paths']['layout'], MINERVA_APP_PATH . DIRECTORY_SEPARATOR . 'libraries' . DIRECTORY_SEPARATOR . $params['request']->params['library'] . DIRECTORY_SEPARATOR . 'views' . DIRECTORY_SEPARATOR . 'layouts' . DIRECTORY_SEPARATOR . '{:layout}.{:type}.php'); array_unshift($params['options']['render']['paths']['template'], MINERVA_APP_PATH . DIRECTORY_SEPARATOR . 'libraries' . DIRECTORY_SEPARATOR . $params['request']->params['library'] . DIRECTORY_SEPARATOR . 'views' . DIRECTORY_SEPARATOR . '{:controller}' . DIRECTORY_SEPARATOR . '{:template}.{:type}.php'); } /** * ADMIN TEMPLATES & CORE * If "admin" is set in the route then we will allow 3rd party templates in the common/views/_admin folder, * but default back to core. So if an alternative admin interface is desired, then templates need to be * created in common/views/_admin/... * NOTE: Admin templates are a specific setting from the routes, they are never defaulted to */ if($admin === true) { // Core (doubles as admin) array_unshift($params['options']['render']['paths']['layout'], MINERVA_APP_PATH . DIRECTORY_SEPARATOR . 'views' . DIRECTORY_SEPARATOR . 'layouts' . DIRECTORY_SEPARATOR . '{:layout}.{:type}.php'); array_unshift($params['options']['render']['paths']['template'], MINERVA_APP_PATH . DIRECTORY_SEPARATOR . 'views' . DIRECTORY_SEPARATOR . '{:controller}' . DIRECTORY_SEPARATOR . '{:template}.{:type}.php'); // Common (for when the default admin interface is desired to be changed) array_unshift($params['options']['render']['paths']['layout'], MINERVA_APP_PATH . DIRECTORY_SEPARATOR . 'libraries' . DIRECTORY_SEPARATOR . 'common' . DIRECTORY_SEPARATOR . 'views' . DIRECTORY_SEPARATOR . '_admin' . DIRECTORY_SEPARATOR . 'layouts' . DIRECTORY_SEPARATOR . '{:layout}.{:type}.php'); array_unshift($params['options']['render']['paths']['template'], MINERVA_APP_PATH . DIRECTORY_SEPARATOR . 'libraries' . DIRECTORY_SEPARATOR . 'common' . DIRECTORY_SEPARATOR . 'views' . DIRECTORY_SEPARATOR . '_admin' . DIRECTORY_SEPARATOR . '{:controller}' . DIRECTORY_SEPARATOR . '{:template}.{:type}.php'); // 3rd party libraries can also put in an _admin folder under its views folder they will override templates in common if present. They get priority. (easy portability) if(!empty($library)) { array_unshift($params['options']['render']['paths']['layout'], MINERVA_APP_PATH . DIRECTORY_SEPARATOR . 'libraries' . DIRECTORY_SEPARATOR . $library . DIRECTORY_SEPARATOR . 'views' . DIRECTORY_SEPARATOR . '_admin' . DIRECTORY_SEPARATOR . 'layouts' . DIRECTORY_SEPARATOR . '{:layout}.{:type}.php'); array_unshift($params['options']['render']['paths']['template'], MINERVA_APP_PATH . DIRECTORY_SEPARATOR . 'libraries' . DIRECTORY_SEPARATOR . $library . DIRECTORY_SEPARATOR . 'views' . DIRECTORY_SEPARATOR . '_admin' . DIRECTORY_SEPARATOR . '{:controller}' . DIRECTORY_SEPARATOR . '{:template}.{:type}.php'); } } /** * STATIC VIEWS * Special situation; "blocks" and "pages" and "menus" have "static" templates that don't require a datasource. * This is only the case for the "view" action on these controllers. First look in the common library and then * it's _admin location (admin blocks) and then default back to core. This is so when calling a block or menu * or static page, you don't have to specify it's an admin one because the router won't come into play for * menus and blocks. */ if(($params['request']->params['action'] == 'view') && ($params['request']->params['controller'] == 'blocks' || $params['request']->params['controller'] == 'pages' || $params['request']->params['controller'] == 'menus')) { // redefine the layout and template arrays, so add back the missing template templates $params['options']['render']['paths']['layout'] = array( MINERVA_APP_PATH . DIRECTORY_SEPARATOR . 'libraries' . DIRECTORY_SEPARATOR . 'common' . DIRECTORY_SEPARATOR . 'views' . DIRECTORY_SEPARATOR . 'layouts' . DIRECTORY_SEPARATOR . 'static' . DIRECTORY_SEPARATOR . '{:layout}.{:type}.php', MINERVA_APP_PATH . DIRECTORY_SEPARATOR . 'libraries' . DIRECTORY_SEPARATOR . 'common' . DIRECTORY_SEPARATOR . 'views' . DIRECTORY_SEPARATOR . 'layouts' . DIRECTORY_SEPARATOR . '{:layout}.{:type}.php', MINERVA_APP_PATH . DIRECTORY_SEPARATOR . 'libraries' . DIRECTORY_SEPARATOR . 'common' . DIRECTORY_SEPARATOR . 'views' . DIRECTORY_SEPARATOR . '_missing' . DIRECTORY_SEPARATOR . 'missing_layout.{:type}.php' ); $params['options']['render']['paths']['template'] = array( MINERVA_APP_PATH . DIRECTORY_SEPARATOR . 'libraries' . DIRECTORY_SEPARATOR . 'common' . DIRECTORY_SEPARATOR . 'views' . DIRECTORY_SEPARATOR . '{:controller}' . DIRECTORY_SEPARATOR . 'static' . DIRECTORY_SEPARATOR . '{:template}.{:type}.php', MINERVA_APP_PATH . DIRECTORY_SEPARATOR . 'libraries' . DIRECTORY_SEPARATOR . 'common' . DIRECTORY_SEPARATOR . 'views' . DIRECTORY_SEPARATOR . '_missing' . DIRECTORY_SEPARATOR . 'missing_template.{:type}.php' ); // ADMIN STATIC VIEWS // Hey, static views can be for just the admin interface as well and those will take priority. if($admin === true) { // before looking at the defaults set above for static views, look for layouts in "minerva/libraries/common/views/layouts" and then core "minerva/views/layouts" array_unshift($params['options']['render']['paths']['layout'], MINERVA_APP_PATH . DIRECTORY_SEPARATOR . 'views' . DIRECTORY_SEPARATOR . 'layouts' . DIRECTORY_SEPARATOR . '{:layout}.{:type}.php'); array_unshift($params['options']['render']['paths']['layout'], MINERVA_APP_PATH . DIRECTORY_SEPARATOR . 'libraries' . DIRECTORY_SEPARATOR . 'common' . DIRECTORY_SEPARATOR . 'views' . DIRECTORY_SEPARATOR . '_admin' . DIRECTORY_SEPARATOR . 'layouts' . DIRECTORY_SEPARATOR . '{:layout}.{:type}.php'); // but before that, look in "minerva/libraries/common/views/layouts/static" and "minerva/views/layouts/static" ... we want to give a "static" template priority array_unshift($params['options']['render']['paths']['layout'], MINERVA_APP_PATH . DIRECTORY_SEPARATOR . 'views' . DIRECTORY_SEPARATOR . 'layouts' . DIRECTORY_SEPARATOR . 'static' . DIRECTORY_SEPARATOR . '{:layout}.{:type}.php'); array_unshift($params['options']['render']['paths']['layout'], MINERVA_APP_PATH . DIRECTORY_SEPARATOR . 'libraries' . DIRECTORY_SEPARATOR . 'common' . DIRECTORY_SEPARATOR . 'views' . DIRECTORY_SEPARATOR . '_admin' . DIRECTORY_SEPARATOR . 'layouts' . DIRECTORY_SEPARATOR . 'static' . DIRECTORY_SEPARATOR . '{:layout}.{:type}.php'); // however, templates always come from a "static" folder unlike layouts array_unshift($params['options']['render']['paths']['template'], MINERVA_APP_PATH . DIRECTORY_SEPARATOR . 'views' . DIRECTORY_SEPARATOR . '{:controller}' . DIRECTORY_SEPARATOR . 'static' . DIRECTORY_SEPARATOR . '{:template}.{:type}.php'); array_unshift($params['options']['render']['paths']['template'], MINERVA_APP_PATH . DIRECTORY_SEPARATOR . 'libraries' . DIRECTORY_SEPARATOR . 'common' . DIRECTORY_SEPARATOR . 'views' . DIRECTORY_SEPARATOR . '_admin' . DIRECTORY_SEPARATOR . '{:controller}' . DIRECTORY_SEPARATOR . 'static' . DIRECTORY_SEPARATOR . '{:template}.{:type}.php'); } } /** * MANUAL OVERRIDES FROM ROUTES * Was the "layout" or "template" key set in the route? Then we're saying to change up the layout path. * This allows other libraries to share the layout template from say the "common" library right from the route. * Or more importantly, libraries that are for page, user, or block types to share layout templates since they * can't touch those controllers. NOTE: This supercedes everything (even static). It is a manual setting * in the route that is optional, but we want to obey it. */ if(!empty($layout)) { // Layouts can be borrowed from other libraries, defined like: library.layout_template $layout_pieces = explode('.', $layout); $layout_library = false; if(count($layout_pieces) > 1) { $layout_library = $layout_pieces[0]; $layout = $layout_pieces[1]; } array_unshift($params['options']['render']['paths']['layout'], MINERVA_APP_PATH . DIRECTORY_SEPARATOR . 'libraries' . DIRECTORY_SEPARATOR . $layout_library . DIRECTORY_SEPARATOR . 'views' . DIRECTORY_SEPARATOR . 'layouts' . DIRECTORY_SEPARATOR . $layout . '.{:type}.php'); } if(!empty($template)) { // Templates can be borrowed from other libraries, defined like: library.template $template_pieces = explode('.', $template); $template_library = false; if(count($template_pieces) > 1) { $template_library = $template_pieces[0]; $template = $template_pieces[1]; } array_unshift($params['options']['render']['paths']['template'], MINERVA_APP_PATH . DIRECTORY_SEPARATOR . 'libraries' . DIRECTORY_SEPARATOR . $template_library . DIRECTORY_SEPARATOR . 'views' . DIRECTORY_SEPARATOR . '{:controller}' . DIRECTORY_SEPARATOR . $template . '.{:type}.php'); } // var_dump($params['options']['render']['paths']); // <--- this is a great thing to uncomment and browse the site for reference return $chain->next($self, $params, $chain); });
/** * Here we check, if there is a library called `li3_less` * * If that is the case, we can directly work with the * less files, that is much more flexible. To get this * up and running, you need to add li3_less as a library * and load it _before_ the li3_bootstrap library like this: * * Libraries::add('li3_less'); * Libraries::add('li3_bootstrap'); * */ if (is_null($config = Libraries::get('li3_less'))) { return; // what a pity - it is not. } Dispatcher::applyFilter('run', function ($self, $params, $chain) { if (!strstr($params['request']->url, '.css')) { return $chain->next($self, $params, $chain); } // look for a matching less file $basename = basename($params['request']->url); $less_path = LI3_BOOTSTRAP_PATH . '/webroot/less'; $less_file = str_replace('.css', '.less', "{$less_path}/{$basename}"); if (!file_exists($less_file)) { return $chain->next($self, $params, $chain); } // found, so we parse this file $output = Less::file($less_file, array('cache' => true)); return new Response(array('body' => $output, 'headers' => array('Content-type' => 'text/css'))); });
StaticClockwork::getInstance()->getTimeline()->endEvent('li3_clockwork_start_call'); if (is_array($params['request']->params) && array_key_exists('controller', $params['request']->params) && array_key_exists('action', $params['request']->params)) { StaticClockwork::getInstance()->getRequest()->controller = join('::', [$params['request']->params['controller'], $params['request']->params['action']]); } // Requests should have special headers if (!stripos($params['request']->url, '__clockwork') && $result instanceof Controller) { $result->response->headers('X-Clockwork-Id', StaticClockwork::getInstance()->getRequest()->id, true); $result->response->headers('X-Clockwork-Version', Clockwork::VERSION, true); } return $result; }); Dispatcher::applyFilter('_call', function ($self, $params, $chain) { StaticClockwork::getInstance()->getTimeline()->startEvent('li3_clockwork_end_call', 'The controller action has been called and now a response will be returned.'); $result = $chain->next($self, $params, $chain); // At this point the controller action has been called and now a response will be returned. // $result here contains the response and we've been setting timers all along the way... // The next time we'll be working with the same response is under the next filter below on // run() AFTER $result = $chain->next() is called... That's the end of the dispatch cycle. // The $result = part below is actually before this filter and the filter on _callable() above. StaticClockwork::getInstance()->getTimeline()->endEvent('li3_clockwork_end_call'); return $result; }); Dispatcher::applyFilter('run', function ($self, $params, $chain) { if (stripos($params['request']->url, '__clockwork')) { return $chain->next($self, $params, $chain); } $result = $chain->next($self, $params, $chain); StaticClockwork::getInstance()->resolveRequest(); StaticClockwork::getInstance()->storeRequest(); return $result; });
<?php use lithium\action\Dispatcher; use lithium\template\View; use lithium\core\Libraries; use lithium\net\http\Router; Dispatcher::applyFilter('_callable', function ($self, $params, $chain) { $result = $chain->next($self, $params, $chain); return $result; }); Dispatcher::applyFilter('_call', function ($self, $params, $chain) { $result = $chain->next($self, $params, $chain); return $result; }); Dispatcher::applyFilter('run', function ($self, $params, $chain) { $result = $chain->next($self, $params, $chain); $li3_show = Libraries::get('li3_show'); $View = new View(array('paths' => array('template' => $li3_show['path'] . '/views/index.html.php', 'layout' => '{:library}/views/layouts/{:layout}.{:type}.php'))); $Show_SQL_View = $View->render('all', array($GLOBALS['Show_SQL'])); if (!isset($result->body[0])) { $result = $Show_SQL_View . $result; } else { $result->body[0] = $Show_SQL_View . $result->body[0]; } return $result; });
*/ use lithium\action\Dispatcher; use lithium\action\Request; use lithium\core\Environment; /** * This filter fakes the `Request` object to the correct base so that everything * works as expected without .htaccess rewrite rules. */ if (PHP_SAPI == 'cli-server') { Dispatcher::applyFilter('run', function ($self, $params, $chain) { $params['request'] = new Request(array('base' => '')); Environment::is(function ($request) { $isLocal = in_array($request->env('SERVER_NAME'), array('localhost')); switch (true) { case isset($request->env): return $request->env; case $isLocal: return 'development'; case $request->command == 'test': return 'test'; case preg_match('/^test\\//', $request->url) && $isLocal: return 'test'; case preg_match('/^test/', $request->env('HTTP_HOST')): return 'test'; default: return 'production'; } }); return $chain->next($self, $params, $chain); }); }
if ($name === 'lithium') { continue; } $file = "{$config['path']}/config/routes.php"; file_exists($file) ? include $file : null; } return $chain->next($self, $params, $chain); }); Dispatcher::applyFilter('_callable', function ($self, $params, $chain) { $ctrl = $chain->next($self, $params, $chain); $request = isset($params['request']) ? $params['request'] : null; $action = $params['params']['action']; if ($request->args) { $arguments = array(); foreach ($request->args as $value) { $param = explode(":", $value); $arguments[$param[0]] = isset($param[1]) ? $param[1] : null; } $request->args = $arguments; } if (Auth::check('default') || preg_match('|test.*|', $request->url)) { return $ctrl; } if (isset($ctrl->publicActions) && in_array($action, $ctrl->publicActions)) { return $ctrl; } return function () use($request) { Session::write('message', 'You need to login to access that page.'); return new Response(compact('request') + array('location' => 'Sessions::add')); }; });
<?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; use lithium\action\Dispatcher; use lithium\test\Controller; Dispatcher::applyFilter('run', function ($self, $params, $chain) { list($isTest, $args) = explode('/', $params['request']->url, 2) + array("", ""); $request = $params['request']; if ($isTest === "test") { $controller = new Controller(); $args = str_replace('/', '\\', $args); return $controller($request, compact('args')); } return $chain->next($self, $params, $chain); });
use lithium\action\Dispatcher; /** * This filter intercepts the `run()` method of the `Dispatcher`, and first passes the `'request'` * parameter (an instance of the `Request` object) to the `Environment` class to detect which * environment the application is running in. Then, loads all application routes in all plugins, * loading the default application routes last. * * Change this code if plugin routes must be loaded in a specific order (i.e. not the same order as * the plugins are added in your bootstrap configuration), or if application routes must be loaded * first (in which case the default catch-all routes should be removed). * * If `Dispatcher::run()` is called multiple times in the course of a single request, change the * `include`s to `include_once`. * * @see lithium\action\Request * @see lithium\core\Environment * @see lithium\net\http\Router */ Dispatcher::applyFilter('run', function ($self, $params, $chain) { Environment::set($params['request']); foreach (array_reverse(Libraries::get()) as $name => $config) { if ($name === 'lithium') { continue; } $file = "{$config['path']}/config/routes.php"; file_exists($file) ? call_user_func(function () use($file) { include $file; }) : null; } return $chain->next($self, $params, $chain); });
<?php use lithium\action\Dispatcher; use lithium\net\http\Media; Dispatcher::applyFilter('_callable', function ($self, $params, $chain) { Media::type('default', null, array('theme' => 'default', 'view' => 'li3_themes\\template\\View', 'paths' => array('layout' => '{:library}/webroot/themes/{:theme}/views/layouts/{:layout}.{:type}.php', 'template' => '{:library}/webroot/themes/{:theme}/views/{:controller}/{:template}.{:type}.php', 'element' => '{:library}/webroot/themes/{:theme}/views/elements/{:template}.{:type}.php'), 'webroot' => '{:library}/webroot/themes/{:theme}')); Media::assets('img', array('paths' => array('{:base}/themes/{:theme}/img/{:path}' => array('base', 'theme', 'path')), 'theme' => 'default')); Media::assets('js', array('paths' => array('{:base}/themes/{:theme}/js/{:path}' => array('base', 'theme', 'path')), 'theme' => 'default')); Media::assets('css', array('paths' => array('{:base}/themes/{:theme}/css/{:path}' => array('base', 'theme', 'path')), 'theme' => 'default')); return $chain->next($self, $params, $chain); });
* 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; });
<?php use li3_flash_message\extensions\storage\FlashMessage; use lithium\action\Dispatcher; Dispatcher::applyFilter('_callable', function ($self, $params, $chain) { $object = $chain->next($self, $params, $chain); if (is_a($object, 'lithium\\action\\Controller')) { return FlashMessage::bindTo($object); } return $object; });
}); /** * Integration with `Validator`. You can load locale dependent rules into the `Validator` * by specifying them manually or retrieving them with the `Catalog` class. */ foreach (array('phone', 'postalCode', 'ssn') as $name) { Validator::add($name, Catalog::read(true, "validation.{$name}", 'en_US')); } /** * Intercepts dispatching processes in order to set the effective locale by using * the locale of the request or if that is not available retrieving a locale preferred * by the client. */ ActionDispatcher::applyFilter('_callable', function ($self, $params, $chain) { $request = $params['request']; $controller = $chain->next($self, $params, $chain); if (!$request->locale) { $request->params['locale'] = Locale::preferred($request); } Environment::set(Environment::get(), array('locale' => $request->locale)); return $controller; }); ConsoleDispatcher::applyFilter('_callable', function ($self, $params, $chain) { $request = $params['request']; $command = $chain->next($self, $params, $chain); if (!$request->locale) { $request->params['locale'] = Locale::preferred($request); } Environment::set(Environment::get(), array('locale' => $request->locale)); return $command; });
<?php use lithium\data\Connections; use li3_show\extensions\data\Data; use lithium\action\Dispatcher; Dispatcher::applyFilter('_callable', function ($self, $params, $chain) { $filter_start = microtime(true); Connections::get("default")->applyFilter("read", function ($self, $params, $chain) use(&$MongoDb) { $result = $chain->next($self, $params, $chain); if (method_exists($result, 'data')) { $query = array('explain' => $result->result()->resource()->explain(), 'query' => $result->result()->resource()->info()); Data::append('queries', array($query)); // echo "<pre>"; // $GLOBALS['Show_SQL'] = $query['query']; // print (SHOW_SQL); // echo "</pre>"; } $GLOBALS['Show_SQL'] = Data::get('queries'); return $result; }); Data::append('timers', array('_filter_for_queries' => microtime(true) - $filter_start)); return $chain->next($self, $params, $chain); });