refactory of the program classes and MindCommand class to make it easier, safer and less repetitive the creation of new programs
Esse commit está contido em:
@@ -11,9 +11,100 @@
|
||||
*/
|
||||
class MindCommand extends Symfony\Component\Console\Command\Command
|
||||
{
|
||||
private $restrict= true;
|
||||
private $fileName= null;
|
||||
private $restrict = true;
|
||||
private $fileName = null;
|
||||
private $requiredArguments = Array();
|
||||
private $optionalArguments = Array();
|
||||
private $requiredOptions = Array();
|
||||
private $optionalOptions = Array();
|
||||
private $commandFlags = Array();
|
||||
|
||||
public function init()
|
||||
{
|
||||
//$this->configure();
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
public function configure()
|
||||
{
|
||||
$this->setName($this->commandName);
|
||||
|
||||
$definition= Array();
|
||||
$definition= array_merge($this->requiredArguments,
|
||||
$this->requiredOptions,
|
||||
$this->optionalArguments,
|
||||
$this->optionalOptions,
|
||||
$this->commandFlags);
|
||||
|
||||
$this->setDefinition($definition);
|
||||
}
|
||||
|
||||
public function addRequiredArgument($argName, $agDescription='')
|
||||
{
|
||||
$this->requiredArguments[]= new InputArgument($argName,
|
||||
InputArgument::REQUIRED,
|
||||
$agDescription);
|
||||
return $this;
|
||||
}
|
||||
public function addOptionalArgument($argName, $agDescription='')
|
||||
{
|
||||
$this->optionalArguments[]= new InputArgument($argName,
|
||||
InputArgument::OPTIONAL,
|
||||
$agDescription);
|
||||
return $this;
|
||||
}
|
||||
public function addRequiredOption($name, $shortCut=null, $description='', $default=null)
|
||||
{
|
||||
$this->requiredOptions[]= new InputOption($name,
|
||||
$shortCut,
|
||||
InputOption::PARAMETER_REQUIRED,
|
||||
$description,
|
||||
$default);
|
||||
return $this;
|
||||
}
|
||||
public function addOptionalOption($name, $shortCut=null, $description='', $default=null)
|
||||
{
|
||||
$this->optionalOptions[]= new InputOption($name,
|
||||
$shortCut,
|
||||
InputOption::PARAMETER_OPTIONAL,
|
||||
$description,
|
||||
$default);
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function addFlag($name, $shortCut=null, $description='')
|
||||
{
|
||||
$this->commandFlags[]= new InputOption($name,
|
||||
$shortCut,
|
||||
InputOption::PARAMETER_NONE,
|
||||
$description);
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setCommandName($commandName)
|
||||
{
|
||||
$this->commandName= $commandName;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function description($description)
|
||||
{
|
||||
$this->description= $description;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function help($helpContent)
|
||||
{
|
||||
$this->helpContent= $helpContent;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setAction($action)
|
||||
{
|
||||
$this->commandAction= $action;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Specifies the name of the file, included with the program
|
||||
* @param String $fName
|
||||
@@ -24,7 +115,7 @@ class MindCommand extends Symfony\Component\Console\Command\Command
|
||||
$this->fileName= $fName;
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Gets the name of the file which the program is refered to
|
||||
* @method getFileName
|
||||
@@ -106,12 +197,15 @@ class MindCommand extends Symfony\Component\Console\Command\Command
|
||||
public function execute(Console\Input\InputInterface $input,
|
||||
Console\Output\OutputInterface $output)
|
||||
{
|
||||
$this->runPlugins('before');
|
||||
return $this->verifyCredentials();
|
||||
}
|
||||
|
||||
public function __() {
|
||||
$this->runPlugins('after');
|
||||
if(!$this->verifyCredentials())
|
||||
return false;
|
||||
|
||||
foreach($input->getArguments() as $k=>$arg)
|
||||
{
|
||||
$this->$k= $arg;
|
||||
}
|
||||
|
||||
$this->runAction();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -125,8 +219,15 @@ class MindCommand extends Symfony\Component\Console\Command\Command
|
||||
GLOBAL $_REQ;
|
||||
if($_REQ['env'] =='http')
|
||||
{
|
||||
$this->runPlugins('before');
|
||||
return $this->verifyCredentials();
|
||||
if(!$this->verifyCredentials())
|
||||
return false;
|
||||
|
||||
foreach($_REQ['data'] as $k=>$arg)
|
||||
{
|
||||
$this->$k= $arg;
|
||||
}
|
||||
|
||||
$this->runAction();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -180,6 +281,19 @@ class MindCommand extends Symfony\Component\Console\Command\Command
|
||||
* each program::runAction command blocks
|
||||
*/
|
||||
public function runAction(){
|
||||
$this->runPlugins('after');
|
||||
$this->runPlugins('before');
|
||||
|
||||
// yea, I know it looks crazy!
|
||||
if(is_string($this->commandAction))
|
||||
$this->{$this->commandAction}();
|
||||
else
|
||||
call_user_func($this->commandAction, $this);
|
||||
|
||||
$this->runPlugins('after');
|
||||
}
|
||||
|
||||
public function __set($what, $value)
|
||||
{
|
||||
$this->$what= $value;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
<?php
|
||||
/**
|
||||
* This file is part of theWebMind.org project
|
||||
*/
|
||||
use Symfony\Component\Console\Input\InputArgument,
|
||||
Symfony\Component\Console\Input\InputOption,
|
||||
Symfony\Component\Console;
|
||||
|
||||
/**
|
||||
* This class represents a model for programs
|
||||
*
|
||||
* @author Felipe Nascimento de Moura <felipenmoura@gmail.com>
|
||||
*/
|
||||
class modeloTeste extends MindCommand implements program
|
||||
{
|
||||
/*
|
||||
* The properties you will use as argument MUST be declared, and public
|
||||
*/
|
||||
public $firstArgument= '';
|
||||
|
||||
public function executableFunction()
|
||||
{
|
||||
// in this example, we will simply show a message using one argument
|
||||
echo " This command has just been executed!\n";
|
||||
echo " With argument: ".$this->firstArgument."\n\n";
|
||||
}
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
/**
|
||||
* You can use the following structure to set the program behavior
|
||||
*/
|
||||
$this->setCommandName('modeloteste')
|
||||
->setDescription("This is a model command, only")
|
||||
->setRestrict(false)
|
||||
->setHelp("A longer text, explaining the command")
|
||||
->setAction(function($class){
|
||||
$class->executableFunction();
|
||||
});
|
||||
/**
|
||||
* Or the following...
|
||||
*/
|
||||
/*
|
||||
$this->setCommandName('modeloteste')
|
||||
->setDescription("This is a model command, only")
|
||||
->setRestrict(false)
|
||||
->setHelp("A longer text, explaining the command")
|
||||
->setAction('executableFunction');
|
||||
*/
|
||||
|
||||
/**
|
||||
* The next commands shows you how to set the signature of you program, such as
|
||||
* parameters, options or flags.
|
||||
* Your class will receive a property for each parameter, which can be accessed
|
||||
* by its argument name(in this example, 'firstArgument'.
|
||||
*/
|
||||
$this->addRequiredArgument('firstArgument', 'first, and required argument');
|
||||
//$this->addOptionalArgument('secondArgument', 'This is the second and optional argument');
|
||||
//$this->addRequiredOption('user', '-u', 'The user who will be passed for any reason', 'root');
|
||||
//$this->addOptionalOption('detailed', '-d', 'Should perform its action detailed?', null);
|
||||
//$this->addFlag('silent', '-s', 'Executes the command quietly');
|
||||
|
||||
// after all the definition, you MUST initiate your program.
|
||||
$this->init();
|
||||
}
|
||||
}
|
||||
@@ -47,7 +47,8 @@
|
||||
new Analyze(),
|
||||
new SetUse(),
|
||||
new dqb(),
|
||||
new Generate()
|
||||
new Generate(),
|
||||
new modeloTeste()
|
||||
));
|
||||
|
||||
if($_REQ['env']=='shell')
|
||||
|
||||
Referência em uma Nova Issue
Bloquear um usuário