Skip to content

Commit

Permalink
Merge pull request #341 from phalcon/2.0.x
Browse files Browse the repository at this point in the history
2.0.1
  • Loading branch information
sergeyklay committed May 7, 2015
2 parents 9ab12d9 + ebccd2d commit f1950e9
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 38 deletions.
Original file line number Diff line number Diff line change
@@ -1,21 +1,48 @@
<?php
namespace Phalcon\CLI\Console;

/*
+------------------------------------------------------------------------+
| Phalcon Framework |
+------------------------------------------------------------------------+
| Copyright (c) 2011-2015 Phalcon Team (http://www.phalconphp.com) |
+------------------------------------------------------------------------+
| This source file is subject to the New BSD License that is bundled |
| with this package in the file docs/LICENSE.txt. |
| |
| If you did not receive a copy of the license and are unable to |
| obtain it through the world-wide-web, please send an email |
| to [email protected] so we can send you a copy immediately. |
+------------------------------------------------------------------------+
| Authors: Sebastian Arrubia <[email protected]> |
+------------------------------------------------------------------------+
*/

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 <[email protected]>
* 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();
Expand All @@ -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'];
Expand All @@ -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());

Expand All @@ -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();
Expand All @@ -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();
Expand Down Expand Up @@ -125,7 +161,7 @@ private function showHelp()
echo $helpOutput . PHP_EOL;
echo PHP_EOL . 'Usage:' . PHP_EOL;
echo PHP_EOL;
echo ' command [<task> [<action> [<param1> <param2> ... <paramN>] ] ]'. PHP_EOL;
echo "\t" , 'command [<task> [<action> [<param1> <param2> ... <paramN>] ] ]', PHP_EOL;
echo PHP_EOL;
echo PHP_EOL . 'To show task help type:' . PHP_EOL;
echo PHP_EOL;
Expand Down Expand Up @@ -157,7 +193,7 @@ private function showTaskHelp($taskTogetHelp)
echo $helpOutput . PHP_EOL;
echo PHP_EOL . 'Usage:' . PHP_EOL;
echo PHP_EOL;
echo ' command [<task> [<action> [<param1> <param2> ... <paramN>] ] ]'. PHP_EOL;
echo "\t" , 'command [<task> [<action> [<param1> <param2> ... <paramN>] ] ]', PHP_EOL;
echo PHP_EOL;
foreach ($this->documentation as $task => $doc) {
if ($taskTogetHelp != $task) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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 <task> -h
```sh
$ ./yourAppCommand <task> -h
```

or

> ./yourAppCommand <task> --help
```sh
$ ./yourAppCommand <task> --help
```

or

> ./yourAppCommand <task> help
```sh
$ ./yourAppCommand <task> 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(

Expand All @@ -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',
Expand All @@ -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
Expand Down Expand Up @@ -92,15 +107,20 @@ 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|
| :------------- |:-------------|
| 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 **<TaskName>Task.php**
Expand Down Expand Up @@ -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 [<task> [<action> [<param1> <param2> ... <paramN>] ] ]
command [<task> [<action> [<param1> <param2> ... <paramN>] ] ]
Task: ls
List directory content
The content will be displayed in the standard output
Expand Down
10 changes: 8 additions & 2 deletions Library/Phalcon/Mvc/View/Engine/Smarty.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,21 @@ 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();
}
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;
}
}

/**
Expand Down

0 comments on commit f1950e9

Please sign in to comment.