Skip to content

Commit e18a606

Browse files
committed
Add custom Silly/Application first version and an example
1 parent c0d2c94 commit e18a606

File tree

3 files changed

+74
-0
lines changed

3 files changed

+74
-0
lines changed

bootstrap.php

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
$loader = require __DIR__ . '/vendor/autoload.php';
4+
$loader->add('LockExamples', __DIR__ . '/src');
5+
6+
date_default_timezone_set("Europe/Madrid");
7+

cli.php

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
require 'bootstrap.php';
4+
5+
use Symfony\Component\Console\Output\OutputInterface;
6+
use Symfony\Component\Lock\StoreInterface;
7+
use Symfony\Component\Lock\Factory;
8+
use Symfony\Component\Lock\Store\SemaphoreStore;
9+
use Symfony\Component\Lock\Store\FlockStore;
10+
use Symfony\Component\Lock\Store\RedisStore;
11+
use Symfony\Component\Lock\Store\RetryTillSaveStore;
12+
use Monolog\Logger;
13+
use Monolog\Handler\StreamHandler;
14+
use LockExamples\Cli\Application as LockApplication;
15+
16+
$app = new LockApplication();
17+
18+
$app->addStore('flock', new FlockStore(sys_get_temp_dir()));
19+
20+
$app->command('simple:nolock [--test]', function (OutputInterface $output, Factory $factory, $input) {
21+
$output->writeln('Hello world');
22+
});
23+
24+
25+
$app->run();

src/LockExamples/Cli/Application.php

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace LockExamples\Cli;
4+
5+
use Silly\Application as SillyApplication;
6+
use Symfony\Component\Lock\StoreInterface;
7+
use Symfony\Component\Lock\Factory;
8+
9+
10+
class Application extends SillyApplication
11+
{
12+
13+
private $stores = [];
14+
15+
public function addStore($key, StoreInterface $store) {
16+
$this->stores[$key] = $store;
17+
}
18+
19+
public function command($expression, $callable, array $aliases = [])
20+
{
21+
$expressionWithStores = $expression . " [store]";
22+
23+
$command = parent::command($expressionWithStores, $callable, $aliases);
24+
25+
$command->setCode(function ($input, $output) use ($callable) {
26+
$factory = $this->getSelectedFactory($input);
27+
return call_user_func($callable, $output, $factory, $input);
28+
});
29+
30+
return $command;
31+
}
32+
33+
private function getSelectedFactory($input)
34+
{
35+
foreach ($this->stores as $key => $store) {
36+
if ($input->hasParameterOption("${key}", true)) {
37+
return new Factory($store);
38+
}
39+
}
40+
return null;
41+
}
42+
}

0 commit comments

Comments
 (0)