-
-
Notifications
You must be signed in to change notification settings - Fork 339
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #341 from phalcon/2.0.x
2.0.1
- Loading branch information
Showing
3 changed files
with
114 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
|
@@ -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 [<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; | ||
|
@@ -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) { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters