diff --git a/Library/Phalcon/CLI/Console/Extended.php b/Library/Phalcon/Cli/Console/Extended.php similarity index 68% rename from Library/Phalcon/CLI/Console/Extended.php rename to Library/Phalcon/Cli/Console/Extended.php index cac0f4550..7683027ba 100644 --- a/Library/Phalcon/CLI/Console/Extended.php +++ b/Library/Phalcon/Cli/Console/Extended.php @@ -1,21 +1,48 @@ | + +------------------------------------------------------------------------+ + */ + +namespace Phalcon\Cli\Console; + +use Phalcon\Cli\Console as ConsoleApp; +use Phalcon\Annotations\Adapter\Memory as MemoryAdapter; /** * Phalcon\CLI\Console\Extended * - * @version 0.1 - * @author Sebastian Arrubia + * Extended Console Application that uses annotations in order to create automatically a help description. * + * @package Phalcon\Cli\Console */ -class Extended extends \Phalcon\CLI\Console +class Extended extends ConsoleApp { - private $tasksDir = ''; private $documentation = array(); - - public function handle(array $arguments) + /** + * Handle the whole command-line tasks + * + * @param array $arguments Cli arguments + * + * @return mixed + * @throws \Phalcon\Cli\Console\Exception + */ + public function handle(array $arguments = null) { if (isset($arguments['task']) && in_array($arguments['task'], array('-h','--help','help'))) { $this->setTasksDir(); @@ -32,12 +59,15 @@ public function handle(array $arguments) parent::handle($arguments); } + /** + * @throws \Phalcon\Cli\Console\Exception + */ private function setTasksDir() { $config = $this->getDI()->get('config'); - if (!is_dir($config['tasksDir'])) { - throw new \Phalcon\CLI\Console\Exception("Invalid provided tasks Dir"); + if (!isset($config['tasksDir']) || !is_dir($config['tasksDir'])) { + throw new Exception("Invalid provided tasks Dir"); } $this->tasksDir = $config['tasksDir']; @@ -48,22 +78,24 @@ private function createHelp() $scannedTasksDir = array_diff(scandir($this->tasksDir), array('..', '.')); $config = $this->getDI()->get('config'); + $dispatcher = $this->getDI()->getShared('dispatcher'); + $namespace = $dispatcher->getNamespaceName(); - if (isset($config['annotationsAdapter'])) { - if ($config['annotationsAdapter'] == 'memory') { - $reader = new \Phalcon\Annotations\Adapter\Memory(); - } elseif ($config['annotationsAdapter'] == 'apc') { - $reader = new \Phalcon\Annotations\Adapter\Apc(); + if (isset($config['annotationsAdapter']) && $config['annotationsAdapter']) { + $adapter = '\Phalcon\Annotations\Adapter\\' . $config['annotationsAdapter']; + if (class_exists($adapter)) { + $reader = new $adapter(); } else { - $reader = new \Phalcon\Annotations\Adapter\Memory(); + $reader = new MemoryAdapter(); } } else { - $reader = new \Phalcon\Annotations\Adapter\Memory(); + $reader = new MemoryAdapter(); } foreach ($scannedTasksDir as $taskFile) { - $taskClass = str_replace('.php', '', $taskFile); + $taskClass = ($namespace ? $namespace . '\\' : '') . str_replace('.php', '', $taskFile); $taskName = strtolower(str_replace('Task', '', $taskClass)); + $taskName = trim($taskName, '\\'); $this->documentation[$taskName] = array('description'=>array(''), 'actions'=>array()); @@ -75,7 +107,7 @@ private function createHelp() continue; } - //Class Annotations + // Class Annotations foreach ($annotations as $annotation) { if ($annotation->getName() == 'description') { $this->documentation[$taskName]['description'] = $annotation->getArguments(); @@ -84,12 +116,16 @@ private function createHelp() $methodAnnotations = $reflector->getMethodsAnnotations(); - //Method Annotations + // Method Annotations if (!$methodAnnotations) { continue; } foreach ($methodAnnotations as $action => $collection) { + if ($collection->has('DoNotCover')) { + continue; + } + $actionName = strtolower(str_replace('Action', '', $action)); $this->documentation[$taskName]['actions'][$actionName]=array(); @@ -125,7 +161,7 @@ private function showHelp() echo $helpOutput . PHP_EOL; echo PHP_EOL . 'Usage:' . PHP_EOL; echo PHP_EOL; - echo ' command [ [ [ ... ] ] ]'. PHP_EOL; + echo "\t" , 'command [ [ [ ... ] ] ]', PHP_EOL; echo PHP_EOL; echo PHP_EOL . 'To show task help type:' . PHP_EOL; echo PHP_EOL; @@ -157,7 +193,7 @@ private function showTaskHelp($taskTogetHelp) echo $helpOutput . PHP_EOL; echo PHP_EOL . 'Usage:' . PHP_EOL; echo PHP_EOL; - echo ' command [ [ [ ... ] ] ]'. PHP_EOL; + echo "\t" , 'command [ [ [ ... ] ] ]', PHP_EOL; echo PHP_EOL; foreach ($this->documentation as $task => $doc) { if ($taskTogetHelp != $task) { diff --git a/Library/Phalcon/CLI/Console/README.md b/Library/Phalcon/Cli/Console/README.md similarity index 82% rename from Library/Phalcon/CLI/Console/README.md rename to Library/Phalcon/Cli/Console/README.md index 65b1029de..4cff1857f 100644 --- a/Library/Phalcon/CLI/Console/README.md +++ b/Library/Phalcon/Cli/Console/README.md @@ -1,36 +1,51 @@ -#Phalcon\CLI\Console\Extended -Extended "Phalcon\CLI\Console\Extended" class that uses **annotations** in order to create automatically a help description. +# Phalcon\Cli\Console\Extended + +Extended "Phalcon\Cli\Console\Extended" class that uses **annotations** in order to create automatically a help description. + +## How to use it? -##How to use it? The parameters **-h**, **--help** or **help** could be used in order to get the help documentation automatically. For instance: **To get the tasks list and their description** -> ./yourAppCommand -h +```sh +$ ./yourAppCommand -h +``` or -> ./yourAppCommand --help +```sh +$ ./yourAppCommand --help +``` or -> ./yourAppCommand help +```sh +$ ./yourAppCommand help +``` **To get the task's actions list and their description, even the parameters description** -> ./yourAppCommand -h +```sh +$ ./yourAppCommand -h +``` or -> ./yourAppCommand --help +```sh +$ ./yourAppCommand --help +``` or -> ./yourAppCommand help +```sh +$ ./yourAppCommand help +``` -##How to set it up? +## How to set it up? Firstly you need to add some important values into the config. + ```php $config = new \Phalcon\Config(array( @@ -45,7 +60,6 @@ $config = new \Phalcon\Config(array( /** * annotationsAdapter is the choosen adapter to read annotations. - * Available adapters: memory | apc * Adapter by default: memory */ 'annotationsAdapter' => 'memory', @@ -64,6 +78,7 @@ $di->set("config",function () use ($config) { ``` Well, it's time to create an instance of Extended Console Class to handle the calls + ```php $console = new \Phalcon\CLI\Console\Extended(); //Seting the above DI @@ -92,7 +107,9 @@ try { exit(255); } ``` -##How to add task description? + +## How to add task description? + The annotation tags that have been used to add tasks description are described below: | Annotation| Description| @@ -100,7 +117,10 @@ The annotation tags that have been used to add tasks description are described b | description | Is the Task or Action description. Could be one line or multiples lines| | param | Is the parameter documentation of the Action. The "param " annotation have 3 variables Name, Type and description| -###Task Example +Also is available instruction for hiding action from help. Just use `@DoNotCover` in method annotation. + +### Task Example + Assume that we have developed a task, to list a directory's content. So the file of the task must be located within the tasks folder. **For instance: /path/to/your/project/tasks/LsTask.php** Pay attention to the file name. This must be named as **Task.php** @@ -131,15 +151,29 @@ class LsTask extends \Phalcon\CLI\Task $unitSize = $params[1]; //Code to iterate a directory and show the content } + + /** + * @DoNotCover + */ + public function secretAction() + { + echo 'Secret list:'.PHP_EOL; + // ... + } } ``` + So the above example should looks like: -> ./yourAppCommand ls --help ``` +$ ./yourAppCommand ls --help + My Console App 1.0 + Usage: - command [ [ [ ... ] ] ] + command [ [ [ ... ] ] ] + + Task: ls List directory content The content will be displayed in the standard output diff --git a/Library/Phalcon/Mvc/View/Engine/Smarty.php b/Library/Phalcon/Mvc/View/Engine/Smarty.php index 6699d5d31..25e65338c 100644 --- a/Library/Phalcon/Mvc/View/Engine/Smarty.php +++ b/Library/Phalcon/Mvc/View/Engine/Smarty.php @@ -43,7 +43,7 @@ public function __construct($view, DiInterface $di = null) * @param array $params * @param boolean $mustClean */ - public function render($path, $params, $mustClean = null) + public function render($path, $params, $mustClean = false) { if (!isset($params['content'])) { $params['content'] = $this->_view->getContent(); @@ -51,7 +51,13 @@ public function render($path, $params, $mustClean = null) foreach ($params as $key => $value) { $this->smarty->assign($key, $value); } - $this->_view->setContent($this->smarty->fetch($path)); + + $content = $this->smarty->fetch($path); + if ($mustClean) { + $this->_view->setContent($content); + } else { + echo $content; + } } /**