allanmessias / phelp Goto Github PK
View Code? Open in Web Editor NEWA command line PHP helper for you
A command line PHP helper for you
I should model a command for a better representation.
A command must have:
1 - A name;
2 - A executable way (Controller or Callback);
3 - It's description;
4 - It's flags (To be implemented);
5 - It's manual (How to be executed);
The base Command is "help" and if called with another Command, it should show the latter's manual.
Well, looks like I'm building a framework for myself without even knowing and forgot about what this application needs to do. It's a simple task actually.
It only needs to:
1 - Create a php file with the given name;
2 - Asks if it's a class, if it is, create like this:
<?php
class GivenName
{
}
3 - If it's an interface or a trait, do it as well, following this pattern.
4 - Insert the namespace automatically
The motivation for this project it's just because I'm really tired of writing this stuff in PHP and I really want to automate this stuff, because I don't want to rely on another text editor, like PHPStorm, that does this out of the box.
I've downloaded the Symfony Command Line Application framework and saw a couple of things that could help me out with the Phelp application design, concerning to Command object
To do this, I'm using this code as example:
$application = new Application();
$application->register('generate-admin')
->addArgument('username', InputArgument::REQUIRED)
->setCode(function (InputInterface $input, OutputInterface $output): int {
$output->writeln("Teste");
return Command::SUCCESS;
});
$application->run();
So, this code does the some things.
1 - Instantiante the application and register the command "generate-admin"
2 - Then, the Application will call it's "add" method to create a new Command instance with the given name
3 - Inside Command's constructor, it's definition property will be assigned as a InputDefinition object, which will later contain all the definitions to the given input, such as: arguments, options, negations and shortcuts
4 - Then, it will set the name of the command and the description
5 - When all is set up, the "add" method will resume as follow:
return [new HelpCommand(), new ListCommand(), new CompleteCommand(), new DumpCompletionCommand()];
The Command model in the framework is as it follows:
// see https://tldp.org/LDP/abs/html/exitcodes.html
public const SUCCESS = 0;
public const FAILURE = 1;
public const INVALID = 2;
/**
* @var string|null The default command name
*
* @deprecated since Symfony 6.1, use the AsCommand attribute instead
*/
protected static $defaultName;
/**
* @var string|null The default command description
*
* @deprecated since Symfony 6.1, use the AsCommand attribute instead
*/
protected static $defaultDescription;
private ?Application $application = null;
private ?string $name = null;
private ?string $processTitle = null;
private array $aliases = [];
private InputDefinition $definition;
private bool $hidden = false;
private string $help = '';
private string $description = '';
private ?InputDefinition $fullDefinition = null;
private bool $ignoreValidationErrors = false;
private ?\Closure $code = null;
private array $synopsis = [];
private array $usages = [];
private ?HelperSet $helperSet = null;
public static function getDefaultName(): ?string
{
$class = static::class;
if ($attribute = (new \ReflectionClass($class))->getAttributes(AsCommand::class)) {
return $attribute[0]->newInstance()->name;
}
$r = new \ReflectionProperty($class, 'defaultName');
if ($class !== $r->class || null === static::$defaultName) {
return null;
}
trigger_deprecation('symfony/console', '6.1', 'Relying on the static property "$defaultName" for setting a command name is deprecated. Add the "%s" attribute to the "%s" class instead.', AsCommand::class, static::class);
return static::$defaultName;
}
public static function getDefaultDescription(): ?string
{
$class = static::class;
if ($attribute = (new \ReflectionClass($class))->getAttributes(AsCommand::class)) {
return $attribute[0]->newInstance()->description;
}
$r = new \ReflectionProperty($class, 'defaultDescription');
if ($class !== $r->class || null === static::$defaultDescription) {
return null;
}
trigger_deprecation('symfony/console', '6.1', 'Relying on the static property "$defaultDescription" for setting a command description is deprecated. Add the "%s" attribute to the "%s" class instead.', AsCommand::class, static::class);
return static::$defaultDescription;
}
/**
* @param string|null $name The name of the command; passing null means it must be set in configure()
*
* @throws LogicException When the command name is empty
*/
public function __construct(string $name = null)
{
$this->definition = new InputDefinition();
if (null === $name && null !== $name = static::getDefaultName()) {
$aliases = explode('|', $name);
if ('' === $name = array_shift($aliases)) {
$this->setHidden(true);
$name = array_shift($aliases);
}
$this->setAliases($aliases);
}
if (null !== $name) {
$this->setName($name);
}
if ('' === $this->description) {
$this->setDescription(static::getDefaultDescription() ?? '');
}
$this->configure();
}
For better understanding, Commands should be stored in a list and the application should rely on this list for execute the command it's being called.
A CommandList should have:
1 - A list of commands
2 - Every command must be like this: ["name_of_command" => CommandObject]
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.