Skip to content

Commit

Permalink
Added annotations for action definition, Help Task
Browse files Browse the repository at this point in the history
All changes in this commit revolve around building a useful help task,
Annotation engine addition means that we can define more without the
need for hooks or functions. Although every action needs an "@action"
annotation now it creates a better, more defined environment.
  • Loading branch information
Danzabar committed Jan 26, 2015
1 parent fa46b76 commit 0796f71
Show file tree
Hide file tree
Showing 14 changed files with 143 additions and 4 deletions.
1 change: 1 addition & 0 deletions docs/Formats.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ The above format can be used by wrapping your string in `<Name></Name>`, since t
The class sets up 3 default formats that you can use, or overwrite if you wish:

<Question></Question> - Foreground Cyan
<Info></Info> - Foreground Cyan
<Comment></Comment> - Foreground Yellow
<Error></Error> - Foreground White, Background Red

Expand Down
3 changes: 3 additions & 0 deletions docs/InputOutput.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,7 @@ The output writes your strings to the console, it only has 2 methods and works i
// Write a line
$this->output->writeln("This will have a new line at the end");

// Draw a horizontal line
$this->output->hr($length, '-');

You can replace your own Output class by using the DI in the same fashion as the Input, if you are doing this though, just remember to include the format class unless you dont need/want it.
6 changes: 6 additions & 0 deletions docs/Writing Tasks.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,18 @@ As if you was using regular Phalcon CLI tools the tasks must have Task appended
* Every task should have a main method, it will be the default
* action that is called if no other is specified.
*
* We use the Annotation engine to specify which commands are actions. Make sure you include it on every action!
*
* @Action
*/
public function main()
{
$this->output->writeln("This is the main action");
}

/**
* @Action
*/
public function other()
{
$this->output->writeln("This is the other action");
Expand Down
1 change: 1 addition & 0 deletions src/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ public function start($args = Array())
$this->dispatcher->setParams($arg['params']);
}

$this->di->setShared('library', $this->library);
$this->dispatcher->setDI($this->di);

return $this->dispatcher->dispatch();
Expand Down
3 changes: 3 additions & 0 deletions src/Format/FormatCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ public function addGeneric()
// Comments
$this->add('comment', Array('foreground' => 'yellow'));

// Info
$this->add('info', Array('foreground' => 'cyan'));

// Errors
$this->add('error', Array('foreground' => 'white', 'background' => 'red'));
}
Expand Down
11 changes: 11 additions & 0 deletions src/Output/Output.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,17 @@ public function writeln($str)
fputs($this->output, Format::format($str)."\n");
}

/**
* Draws a line with a given length and character
*
* @return String
* @author Dan Cox
*/
public function hr($length, $char = '_')
{
$this->writeln(str_pad('', $length, $char));
}

/**
* Read the output
*
Expand Down
17 changes: 16 additions & 1 deletion src/Tasks/TaskLibrary.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,11 @@ public function __construct()
*/
public function add($tasks)
{
$this->library[$tasks['task']['name']] = Array('actions' => $tasks['task']['actions'], 'class' => $tasks['class']);
$this->library[$tasks['task']['name']] = Array(
'actions' => $tasks['task']['actions'],
'description' => $tasks['task']['description'],
'class' => $tasks['class']
);
}

/**
Expand All @@ -64,5 +68,16 @@ public function find($name)
throw new Exceptions\CommandNotFoundException($name);
}

/**
* Returns all the registered commands
*
* @return Array
* @author Dan Cox
*/
public function getAll()
{
return $this->library;
}


} // END class TaskLibrary
42 changes: 40 additions & 2 deletions src/Tasks/TaskPrepper.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php namespace Danzabar\CLI\Tasks;

use Danzabar\CLI\Input\InputArgument,
Phalcon\Annotations\Adapter\Memory,
Danzabar\CLI\Input\InputOption;


Expand Down Expand Up @@ -33,6 +34,20 @@ class TaskPrepper
* @var Object
*/
protected $di;

/**
* Instance of the Memory Annotation Engine
*
* @var Object
*/
protected $annotation;

/**
* Current instance of the annotation reader
*
* @var Object
*/
protected $reader;

/**
* An Array of arguments given
Expand All @@ -59,6 +74,9 @@ public function __construct($di)
// DI
$this->di = $di;

// Annotations
$this->annotation = new Memory;

// Add input class to the di
$this->di->setShared('argument', new InputArgument);
$this->di->setShared('option', new InputOption);
Expand All @@ -79,6 +97,9 @@ public function load($className)
// Create reflection
$this->reflection = new \ReflectionClass($className);

// Read annotations
$this->reader = $this->annotation->get($className);

// Save the class name
$this->className = $className;

Expand All @@ -94,13 +115,23 @@ public function load($className)
public function describe()
{
$methods = Array();
$methodAnnotations = $this->reader->getMethodsAnnotations();

foreach($this->reflection->getMethods() as $method)
{
$methods[] = $method->getName();
$name = $method->getName();

if(isset($methodAnnotations[$name]))
{
if($methodAnnotations[$name]->has('Action') || $methodAnnotations[$name]->has('action'))
{
$methods[] = $method->getName();
}
}
}

$name = $this->className;
$description = '';

if($this->reflection->hasProperty('name'))
{
Expand All @@ -109,7 +140,14 @@ public function describe()
$name = $prop->getValue($this->reflection->newInstance());
}

return Array('name' => $name, 'class' => $this->className, 'actions' => $methods);
if($this->reflection->hasProperty('description'))
{
$prop = $this->reflection->getProperty('description');
$prop->setAccessible(TRUE);
$description = $prop->getValue($this->reflection->newInstance());
}

return Array('name' => $name, 'description' => $description, 'class' => $this->className, 'actions' => $methods);
}

/**
Expand Down
26 changes: 26 additions & 0 deletions src/Tasks/Utility/Help.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,38 @@ class Help extends Task
/**
* The main action
*
* @Action
* @return void
* @author Dan Cox
*/
public function main()
{
$this->listCommands();
}

/**
* Lists out command names and descriptions
*
* @return void
* @author Dan Cox
*/
public function listCommands()
{
$commands = $this->library->getAll();

foreach($commands as $name => $details)
{
$this->output->writeln(ucwords($name) .' - '.$details['description']);
$this->output->hr(strlen($name . ' - '. $details['description']), '-');

foreach($details['actions'] as $action)
{
$this->output->writeln($action);
}

// Just for padding
$this->output->writeln('');
}
}

} // END class Help extends Task
8 changes: 8 additions & 0 deletions tests/Commands/FakeTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public function setup($action)
/**
* The main action for this command
*
* @Action
* @return void
* @author Dan Cox
*/
Expand All @@ -52,6 +53,7 @@ public function main()
/**
* Test action, no output
*
* @Action
* @return void
* @author Dan Cox
*/
Expand All @@ -62,6 +64,7 @@ public function output()
/**
* Task that tests basic question
*
* @Action
* @return void
* @author Dan Cox
*/
Expand All @@ -76,6 +79,7 @@ public function askMe()
/**
* A double question task
*
* @Action
* @return void
* @author Dan Cox
*/
Expand All @@ -95,6 +99,7 @@ public function advAsk()
/**
* Task that asks a choice question
*
* @Action
* @return void
* @author Dan Cox
*/
Expand All @@ -120,6 +125,7 @@ public function choiceQ()
/**
* Multiple Choice question
*
* @Action
* @return void
* @author Dan Cox
*/
Expand All @@ -143,6 +149,7 @@ public function multiChoice()
/**
* The confirmation test action
*
* @Action
* @return void
* @author Dan Cox
*/
Expand All @@ -164,6 +171,7 @@ public function confirmation()
/**
* A confirmation with explicit set and values changed
*
* @Action
* @return void
* @author Dan Cox
*/
Expand Down
8 changes: 7 additions & 1 deletion tests/Commands/InputTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class InputTask extends Task
/**
* Setup required arguments and options
*
* @Action
* @return void
* @author Dan Cox
*/
Expand All @@ -50,6 +51,7 @@ public function setup($action)
/**
* Setup function specifically for the main action
*
* @Action
* @return void
* @author Dan Cox
*/
Expand All @@ -62,6 +64,7 @@ public function setupMain()
/**
* The main action
*
* @Action
* @return void
* @author Dan Cox
*/
Expand All @@ -79,6 +82,7 @@ public function main()
/**
* Another action
*
* @Action
* @return void
* @author Dan Cox
*/
Expand All @@ -90,6 +94,7 @@ public function required()
/**
* Action to test validation
*
* @Action
* @return void
* @author Dan Cox
*/
Expand All @@ -106,6 +111,7 @@ public function validation()
/**
* Setup function specific to options action
*
* @Action
* @return void
* @author Dan Cox
*/
Expand All @@ -117,6 +123,7 @@ public function setupOptions()
/**
* Action to test options
*
* @Action
* @return void
* @author Dan Cox
*/
Expand All @@ -126,4 +133,3 @@ public function options()
}

} // END class InputTask extends Command

2 changes: 2 additions & 0 deletions tests/Commands/UtilityTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class UtilityTask extends Task
/**
* The main action
*
* @Action
* @return void
* @author Dan Cox
*/
Expand All @@ -39,6 +40,7 @@ public function main()
/**
* Draws a table
*
* @Action
* @return void
* @author Dan Cox
*/
Expand Down
Loading

0 comments on commit 0796f71

Please sign in to comment.