The console dispatcher is responsible for accepting requests from scripts called from the command line, and executing the appropriate Command class(es). The run() method accepts an instance of lithium\console\Request, which encapsulates the console environment and any command-line parameters passed to the script. Dispatcher then invokes lithium\console\Router to determine the correct Command class to invoke, and which method should be called.
Inheritance: extends lithium\core\StaticObject
示例#1
0
 public function testRunWithCamelizingAction()
 {
     $result = Dispatcher::run(new Request(array('args' => array('\\lithium\\tests\\mocks\\console\\command\\MockCommandHelp', 'sample-task-with-optional-args'))));
     $this->assertTrue($result);
     $result = Dispatcher::run(new Request(array('args' => array('\\lithium\\tests\\mocks\\console\\command\\MockCommandHelp', 'sample_task_with_optional_args'))));
     $this->assertTrue($result);
 }
示例#2
0
文件: g11n.php 项目: EHER/chegamos
});
/**
 * 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

/**
 * 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\console\Dispatcher;
Dispatcher::applyFilter('_call', function ($self, $params, $chain) {
    $params['callable']->response->styles(array('heading' => '\\033[1;30;46m'));
    return $chain->next($self, $params, $chain);
});
示例#4
0
 public function testEnvironmentIsSet()
 {
     $expected = 'production';
     $response = Dispatcher::run(new Request(array('args' => array('lithium\\tests\\mocks\\console\\MockDispatcherCommand', 'testEnv', '--env=production'))));
     $result = $response->environment;
     $this->assertEqual($expected, $result);
 }
示例#5
0
<?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\console\Dispatcher;
use lithium\core\Environment;
/**
 * This filter sets the environment based on the current request. By default, `$request->env`, for
 * example in the command `li3 help --env=production`, is used to determine the environment.
 *
 */
Dispatcher::applyFilter('run', function ($self, $params, $chain) {
    Environment::set($params['request']);
    return $chain->next($self, $params, $chain);
});
/**
 * This filter will convert {:heading} to the specified color codes. This is useful for colorizing
 * output and creating different sections.
 *
 */
// Dispatcher::applyFilter('_call', function($self, $params, $chain) {
// 	$params['callable']->response->styles(array(
// 		'heading' => '\033[1;30;46m'
// 	));
// 	return $chain->next($self, $params, $chain);
// });
示例#6
0
 * 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.
 *
 *   - `'scope'` If you plan on using scoping i.e. for accessing plugin data separately you
示例#7
0
use lithium\core\Environment;
use lithium\core\Libraries;
/**
 * This filter sets the environment based on the current request. By default, `$request->env`, for
 * example in the command `li3 help --env=production`, is used to determine the environment.
 *
 * Routes are also loaded, to facilitate URL generation from within the console environment.
 *
 */
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);
});
/**
 * This filter will convert {:heading} to the specified color codes. This is useful for colorizing
 * output and creating different sections.
 *
 */
// Dispatcher::applyFilter('_call', function($self, $params, $chain) {
// 	$params['callable']->response->styles(array(
// 		'heading' => '\033[1;30;46m'
// 	));
示例#8
0
<?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));
 public function testInvalidCommand()
 {
     $expected = (object) array('status' => "Command `\\this\\command\\is\\fake` not found\n");
     $result = Dispatcher::run(new Request(array('args' => array('\\this\\command\\is\\fake', 'testAction'))));
     $this->assertEqual($expected, $result);
 }
示例#10
0
 * Determine if we're in an application context by moving up the directory tree looking for
 * a `config` directory with a `bootstrap.php` file in it.  If no application context is found,
 * just boot up the core framework.
 */
$library = dirname(dirname(__DIR__));
$app = null;
$working = getcwd() ?: __DIR__;
while (!$app && $working) {
    if (file_exists($working . '/config/bootstrap.php')) {
        $app = $working;
    } elseif (file_exists($working . '/app/config/bootstrap.php')) {
        $app = $working . '/app';
    } else {
        $working = ($parent = dirname($working)) != $working ? $parent : false;
    }
}
if ($app) {
    include $app . '/config/bootstrap.php';
} else {
    define('LITHIUM_LIBRARY_PATH', $library);
    define('LITHIUM_APP_PATH', dirname($library) . '/app');
    if (!(include LITHIUM_LIBRARY_PATH . '/lithium/core/Libraries.php')) {
        $message = "Lithium core could not be found.  Check the value of `LITHIUM_LIBRARY_PATH` ";
        $message .= "in `config/bootstrap.php`. It should point to the directory containing your ";
        $message .= "`/libraries` directory.";
        trigger_error($message, E_USER_ERROR);
    }
    Libraries::add('lithium');
}
exit(Dispatcher::run()->status);
示例#11
0
<?php

use lithium\action\Dispatcher;
use lithium\console\Dispatcher as ConsoleDispatcher;
use li3_redis\storage\Redis;
/**
 * Apply filter to Dispatcher, to initialize Redis configuration
 */
Dispatcher::applyFilter('_call', function ($self, $params, $chain) {
    Redis::config();
    return $chain->next($self, $params, $chain);
});
ConsoleDispatcher::applyFilter('_call', function ($self, $params, $chain) {
    Redis::config();
    return $chain->next($self, $params, $chain);
});