Nimic
Nimic provides a good starting point for a php console application. Its purpose is to provide a base for your code with access to a (cacheable) dependency injection container, a console application ready to carry (symfony) commands, an event dispatcher and a monolog instance and phpunit. It's basically a facade built upon symfony components., (*1)
Installation
You can clone the github repo, but the recommended method is through composer. Require flo / nimic in your composer.json., (*2)
Basic usage
Inside your app put this in somefile.php, (*3)
/**
* composer autoloader:
*/
require 'vendor/autoload.php';
$kernel = new \Flo\Nimic\Kernel\NimiKernel; //extend this kernel!
/**
* This is a Symfony2 container
*/
$container = $kernel->getContainer();
/**
* with some predefined services, like this (Console Component) application
*/
$app = $container->get('app');
/**
* on which you should add your own commands:
*/
$app->add(new MyCommand);
/**
* before running it
*/
$app->run();
Create your commands like this., (*4)
Adding a new service
You have to, (*5)
-
create your extension class
- using the extension, add the command service definition to the container.
- (optional) If the service is a command, an event listener or subscriber then you should tag it with command, listener or subscriber. See this for events.
In order to register your extension with the container, you'll have to [override][8] NimiKernel::getExtensions().
This method should return an array of your ExtensionInterface instances, and it's quite possible that you'll need only one extension., (*6)
class YourCustomKernel extends \Flo\Nimic\Kernel\NimiKernel
{
...
/**
* @return array Array of your own extensions
*/
protected function getExtensions()
{
return [
new YourExtension()
];
}
...
}
And then continue with the basic usage example, but instead of, (*7)
$kernel = new \Flo\Nimic\Kernel\NimiKernel;
do, (*8)
$kernel = new YourCustomKernel;
Again, using the extension, you can add (or override) any container service, not just Command classes., (*9)
example.php
#!/usr/bin/env php
<?php
/**
* If you use composer you should use its autoloader:
*/
$loader = require 'vendor/autoload.php';
/**
* In case you need to inject custom services into the container you'll have to:
* override NimiKernel
* create an extension
* add its instance to the array returner by YourCustomKernel::getExtensions (so that the extension will be registered before the container gets compiled)
* make the extension load your services xml or yaml or whatever
*/
$kernel = new Flo\Nimic\Kernel\NimiKernel;
/**
* If the kernel returns a writable cache dir ( YourCustomKernel::getCacheDir ) then the container is cached
*/
$container = $kernel->getContainer();
/**
* This is the main entry point for your console application
* http://symfony.com/doc/current/components/console/index.html
*/
$app = $container->get('app');
/**
* Here you can add commands using $app->add()
* The other method of adding commands is through the custom extension, by defining command services tagges "command"
*/
$app->run();
Testing Commands
See this., (*10)