1. Go to this page and download the library: Download clancats/container library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
clancats / container example snippets
class Human
{
public $name;
public function setName(string $name) {
$this->name = $name;
}
}
class SpaceShip
{
protected $captain; // every ship needs a captain!
public function __construct(Human $captain) {
$this->captain = $captain;
}
public function ayeAye() {
return 'aye aye captain ' . $this->captain->name;
}
}
@malcolm: Human
- setName('Reynolds')
@firefly: SpaceShip(@malcolm)
// for the consistency of the example I leave this here
// but I strongly recommend to autolaod your classes with composer.
ate('AppContainer', function($builder)
{
// create a new container file namespace and parse our `app.ctn` file.
$namespace = new \ClanCats\Container\ContainerNamespace();
$namespace->parse(__DIR__ . '/app.ctn');
// import the namespace data into the builder
$builder->importNamespace($namespace);
});
# Parameters can be defined erverywhere
:pipeline.prefix: 'myapp.'
// you can define aliases to services
@pipeline.queue: @queue.redis
@pipeline.storage: @db.repo.pipeline.mysql
// add function calls that will be run directly after construction of the service
@pipeline: Pipeline\PipelineManager(@pipeline.queue, @pipeline.storage, @pipeline.executor)
- setPrefix(:pipeline.prefix)
- bind(@pipeline_handler.image.downloader)
- bind(@pipeline_handler.image.process)
@pipeline_handler.image.downloader: PipelineHandler\Images\DownloadHandler(@client.curl)
@pipeline_handler.image.process: PipelineHandler\Images\ProcessHandler(@image.processor, {
'temp_dir': '/tmp/',
'backend': 'imagick'
})
$dispatcher = \FastRoute\cachedDispatcher(function(RouteCollector $r) use($container)
{
foreach($container->serviceNamesWithMetaData('route') as $serviceName => $routeMetaData)
{
// an action can have multiple routes handle all of them
foreach($routeMetaData as $routeData)
{
$r->addRoute($routeData[0], $routeData[1], $serviceName);
}
}
}, [
'cacheFile' => PATH_CACHE . '/RouterCache.php',
'cacheDisabled' => $container->getParameter('env') === 'dev',
]);
@signal.exception.http404: App\ExceptionHandler\NotFoundExceptionHandler
= on: 'http.exception', call: 'onHTTPException'
@signal.exception.http400: App\ExceptionHandler\BadRequestExceptionHandler
= on: 'http.exception', call: 'onHTTPException'
@signal.exception.http401: App\ExceptionHandler\UnauthorizedAccessExceptionHandler
= on: 'http.exception', call: 'onHTTPException'
@signal.bootstrap_handler: App\Bootstrap
= on: 'bootstrap.pre', call: 'onBootstrapPre'
= on: 'bootstrap.post', call: 'onBootstrapPost'
foreach($container->serviceNamesWithMetaData('on') as $serviceName => $signalHandlerMetaData)
{
// a action can have multiple routes handle all of them
foreach($signalHandlerMetaData as $singalHandler)
{
if (!is_string($singalHandler[0] ?? false)) {
throw new RegisterHandlerException('The signal handler event key must be a string.');
}
if (!isset($singalHandler['call']) || !is_string($singalHandler['call'])) {
throw new RegisterHandlerException('You must define the name of the function you would like to call.');
}
$priority = $singalHandler['priority'] ?? 0;
// register the signal handler
$eventdispatcher->register($singalHandler[0], function(Signal $signal) use($container, $singalHandler, $serviceName)
{
$container->get($serviceName)->{$singalHandler['call']}($signal);
}, $priority);
}
}