本文整理匯總了PHP中lithium\action\Dispatcher::applyFilter方法的典型用法代碼示例。如果您正苦於以下問題:PHP Dispatcher::applyFilter方法的具體用法?PHP Dispatcher::applyFilter怎麽用?PHP Dispatcher::applyFilter使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類lithium\action\Dispatcher
的用法示例。
在下文中一共展示了Dispatcher::applyFilter方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的PHP代碼示例。
示例1: readCache
// 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;
示例2: function
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);
});
示例3: array
* 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;
});
示例4: function
*
* 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.
*
示例5: function
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);
});
示例6: function
* $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'))));
示例7: function
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;
});
示例8: dirname
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);
});
示例9: function
<?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));
示例10: function
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"
示例11: dirname
<?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']);
// });
// }
示例12: function
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);
});
示例13: function
<?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));
}
});
示例14: substr
$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
示例15: function
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) {