Blueprint is a PHP package for bootstrapping console applications.
Install Blueprint via Composer:
$ composer require rougin/blueprintCreate a blueprint.php file using the initialize command:
$ vendor/bin/blueprint initialize// blueprint.php
$file = array();
// Details for the console app ---
$file['name'] = 'Blueprint';
$file['version'] = '0.7.1';
// -------------------------------
// Path for the commands and templates ----
$paths = array();
$root = '%%CURRENT_DIRECTORY%%';
$paths['templates'] = $root . '/Templates';
$paths['commands'] = $root . '/Commands';
$file['paths'] = $paths;
// ----------------------------------------
// Namespace for the commands ----------
$namespace = 'Acme\Commands';
$data = array('commands' => $namespace);
$file['namespaces'] = $data;
// -------------------------------------
return $file;Note
- Replace the values specified in the
blueprint.phpfile. - Add commands and templates (if applicable) to their respective directories.
- A
blueprint.ymlfile is also supported (use--format=yml).
Before writing a command, the commands property in blueprint.php must be updated first:
// blueprint.php
// ...
-$namespace = 'Acme\Commands';
+$namespace = 'Rrsg\Commands';Then create the command (e.g., TestCommand) to its assigned directory (e.g., src/Commands):
// src/Commands/TestCommand.php
namespace Rrsg\Commands;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class TestCommand extends Command
{
protected function configure()
{
$this->setName('test');
$this->setDescription('Returns a "Test" string');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$output->writeln('<info>Test</info>');
}
}Before running the command, kindly check if its namespace is defined in Composer:
// composer.json
// ...
"autoload":
{
"psr-4":
{
"Rrsg\\": "src"
}
}
// ...$ composer dump-autoload$ vendor/bin/blueprint test
TestTo use Blueprint as a console application, the Blueprint class must be created first:
// bin/app.php
use Rougin\Blueprint\Blueprint;
// Return the root from "vendor" --------
$vendor = __DIR__ . '/../../../../';
$default = __DIR__ . '/../';
$loader = '/vendor/autoload.php';
$exists = file_exists($vendor . $loader);
$root = $exists ? $vendor : $default;
// --------------------------------------
// Load the Composer autoloader -------
require $root . '/vendor/autoload.php';
// ------------------------------------
$app = new Blueprint;The following details can now be updated after creating the class:
// bin/app.php
// ...
// Sets the directory for the defined commands ----
$app->setCommandPath(__DIR__ . '/../src/Commands');
// ------------------------------------------------
// Sets the directory for the templates. Useful -----
// for creating commands with template engines ------
$app->setTemplatePath(__DIR__ . '/../src/Templates');
// --------------------------------------------------
// Sets the name of the console application ---
$app->setName('Rrsg');
// --------------------------------------------
// Sets the version of the console application ---
$app->setVersion('0.1.0');
// -----------------------------------------------
// Sets the namespace for the "commands" path ---
$namespace = 'Rrsg\Simplest\Commands';
$app->setCommandNamespace($namespace);
// ----------------------------------------------
// Runs the console application ---
$app->run();
// --------------------------------Note
Using this approach means the blueprint.php can now be omitted. This approach is also applicable to create customized console applications without the Blueprint branding.
Once configured, the console application can now be run in the terminal:
$ php bin\app.php
Rrsg 0.1.0
Usage:
command [options] [arguments]
Options:
-h, --help Display help for the given command. When no command is given display help for the list command
-q, --quiet Do not output any message
-V, --version Display this application version
--ansi|--no-ansi Force (or disable --no-ansi) ANSI output
-n, --no-interaction Do not ask any interactive question
-v|vv|vvv, --verbose Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug
Available commands:
completion Dump the shell completion script
help Display help for a command
list List commandsBlueprint also provides an alternative Command class for creating commands with descriptive methods and less code:
// src/Commands/TestCommand.php
namespace Rrsg\Commands;
use Rougin\Blueprint\Command;
class TestCommand extends Command
{
protected $name = 'test';
protected $description = 'Returns a "Test" string';
public function execute()
{
$this->showPass('Test');
}
}Kindly see COMMAND for its available properties and methods.
Note
All of the functionalities for the Command class is derived from the Symfony's Console component.
To perform automagic resolutions in each defined commands, the addPackage can be used with the additional functionality from the Container class from Slytherin:
// src/Sample.php
namespace Rrsg;
class Sample
{
protected $name;
public function __construct($name)
{
$this->name = $name;
}
public function getName()
{
return $this->name;
}
}// src/Packages/SamplePackage.php
namespace Rrsg\Packages;
use Rrsg\Sample;
use Rougin\Slytherin\Container\ContainerInterface;
use Rougin\Slytherin\Integration\Configuration;
use Rougin\Slytherin\Integration\IntegrationInterface;
class SamplePackage implements IntegrationInterface
{
public function define(ContainerInterface $container, Configuration $config)
{
return $container->set(Sample::class, new Sample('Blueprint'));
}
}// bin/app.php
use Rrsg\Packages\SamplePackage;
use Rougin\Slytherin\Container\Container;
// ...
// Add the specified package to the container ---
$container = new Container;
$container->addPackage(new SamplePackage);
// ----------------------------------------------
// Then pass it to the console application ---
$app->setContainer($container);
// -------------------------------------------With the above-mentioned integration, for any command that uses the Sample class will get the text value as the $name property:
namespace Rrsg\Commands;
use Rrsg\Sample;
use Rougin\Blueprint\Command;
class TextCommand extends Command
{
protected $name = 'text';
protected $description = 'Shows a sample text';
protected $sample;
public function __construct(Sample $sample)
{
$this->sample = $sample;
}
public function run()
{
$this->showText('Hello, ' . $this->sample->getName() . '!');
return self::RETURN_SUCCESS;
}
}$ php bin/app.php text
Hello, Blueprint!Please see CHANGELOG for more recent changes.
See CONTRIBUTING on how to contribute.
The MIT License (MIT). Please see LICENSE for more information.