Skip to content

Instantly share code, notes, and snippets.

@ercanozkaya
Created October 24, 2011 14:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ercanozkaya/1309230 to your computer and use it in GitHub Desktop.
Save ercanozkaya/1309230 to your computer and use it in GitHub Desktop.
Command Line Dispatcher
<?php
/**
* @category Nooku
* @package Nooku_Server
* @subpackage Default
* @copyright Copyright (C) 2011 Timble CVBA and Contributors. (http://www.timble.net).
* @license GNU GPLv3 <http://www.gnu.org/licenses/gpl.html>
* @link http://www.nooku.org
*/
/**
* Special Dispatcher for CLI
*
* @author Ercan Ozkaya <http://nooku.assembla.com/profile/ercanozkaya>
* @category Nooku
* @package Nooku_Server
* @subpackage Default
*/
class ComDefaultDispatcherCli extends KDispatcherAbstract
{
protected $_request_data;
public function __construct(KConfig $config)
{
parent::__construct($config);
if ($config->request_data) {
$this->_request_data = KConfig::unbox($config->request_data);
}
if ($config->credentials) {
if (!$this->_login(KConfig::unbox($config->credentials))) {
$this->error('Error: Login failed');
exit(1);
}
}
}
protected function _initialize(KConfig $config)
{
$options = getopt('a::u::p::h::', array('url::', 'help::'));
/*
* Handle URL (or controller) and query string
*/
if (empty($options['url'])) {
return $this->_help();
} else {
$url = $this->getService('koowa:http.url', array('url' => $options['url']));
$query = $url->query;
$option = '';
if (isset($query['option'])) {
$option = substr($query['option'], 4);
unset($query['option']);
}
$name = isset($query['view']) ? $query['view'] : $option;
$name = KInflector::singularize($name);
$app = strpos($url->path, 'administrator/') !== false ? 'admin': 'site';
$controller = sprintf('com://%s/%s.controller.%s', $app, $option, $name);
$query = $url->query;
}
/*
* Handle request data
*/
stream_set_blocking(STDIN, 0);
$request_data = $this->read(); // this is the piped input to the script, if exists
stream_set_blocking(STDIN, 1);
$data = array();
parse_str($request_data, $data);
/*
* Handle action
*/
if (empty($options['a'])) {
$query['action'] = empty($request_data) ? 'get' : 'post';
}
else {
$query['action'] = $options['a'];
}
/*
* Handle user credentials
*/
$credentials = false;
if (!empty($options['u']) && !empty($options['p'])) {
$credentials = array(
'username' => $options['u'],
'password' => $options['p']
);
}
$config->append(array(
'controller' => $controller,
'request' => $query,
'request_data' => $data,
'credentials' => $credentials
));
parent::_initialize($config);
}
protected function _help()
{
$caller = $_SERVER['_'];
$script_name = $caller == $_SERVER['argv'][0] ? null : ' '.$_SERVER['argv'][0];
$tpl = sprintf("Usage: %s%s --url [-a] [-u] [-p]\n
--url : URL to parse. You can use administrator in the path for admin side
-a : Controller action. Leave blank for the default values
-u : User name to log in
-p : Password for the user name
-h : Print this message and exit (also --help)\n", $caller, $script_name);
$this->write($tpl);
exit(0);
}
protected function _login($credentials)
{
return JFactory::getApplication()->login($credentials) === true;
}
protected function _actionDispatch(KCommandContext $context)
{
$action = $this->_request->action;
if($action != 'get') {
$context->data = $this->_request_data;
}
if ($action == 'delete') {
$this->unregisterCallback('after.dispatch' , array($this, 'forward'));
}
// _checkToken method checks for KRequest::method which returns null for cli so this is required
KRequest::set('request._token', JUtility::getToken());
$result = $this->getController()->execute($action, $context);
return $result;
}
public function _actionForward(KCommandContext $context)
{
$context->result = $this->getController()->execute('display', $context);
return $context->result;
}
/**
* Overloaded to catch all Exceptions occurring in the class
* @see KControllerAbstract::execute()
*/
public function execute($action, KCommandContext $context)
{
try {
return parent::execute($action, $context);
}
catch (Exception $e) {
$this->error(get_class($e).': '.($e->getCode() ? $e->getCode().' ' : '').$e->getMessage());
exit(1);
}
}
public function read()
{
return trim(fgets(STDIN));
}
public function write($message, $end = "\n")
{
fwrite(STDOUT, $message.$end);
return $this;
}
public function error($message, $end = "\n")
{
fwrite(STDERR, $message.$end);
return $this;
}
}
@ercanozkaya
Copy link
Author

ADD

echo "title=testing" | php index.php --url="administrator/?option=com_articles&view=article&format=json" -uadmin -padmin

EDIT

echo "title=testing edit" | php index.php --url="administrator/?option=com_articles&view=article&id=74&format=json" -uadmin -padmin

DELETE

php index.php --url="administrator/?option=com_articles&view=article&id=74&format=json" -uadmin -padmin -adelete

READ

php index.php --url="administrator/?option=com_articles&view=article&id=65&format=json"

php index.php --url="administrator/?option=com_articles&view=articles&limit=2&format=json"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment