1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Saxulum\Console\Provider; |
4
|
|
|
|
5
|
|
|
use Pimple\Container; |
6
|
|
|
use Pimple\ServiceProviderInterface; |
7
|
|
|
use Saxulum\ClassFinder\ClassFinder; |
8
|
|
|
use Saxulum\Console\Console\ConsoleApplication; |
9
|
|
|
use Symfony\Component\Finder\Finder; |
10
|
|
|
use Symfony\Component\Finder\SplFileInfo; |
11
|
|
|
|
12
|
|
|
class ConsoleProvider implements ServiceProviderInterface |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @param Container $container |
16
|
|
|
*/ |
17
|
|
|
public function register(Container $container) |
18
|
|
|
{ |
19
|
|
|
$container['console.cache'] = null; |
20
|
|
|
|
21
|
|
|
$container['console.command.paths'] = function () { |
22
|
|
|
$paths = array(); |
23
|
|
|
|
24
|
|
|
return $paths; |
25
|
|
|
}; |
26
|
|
|
|
27
|
|
|
$container['console.commands'] = function () use ($container) { |
28
|
|
|
$commands = array(); |
29
|
|
|
|
30
|
|
|
return $commands; |
31
|
|
|
}; |
32
|
|
|
|
33
|
|
|
$container['console'] = function () use ($container) { |
34
|
|
|
$console = new ConsoleApplication($container); |
35
|
|
|
|
36
|
|
|
// automatically detect dispatcher (e.g. in Silex applications) |
37
|
|
|
if (isset($container['dispatcher']) && !is_null($container['dispatcher'])) { |
38
|
|
|
$console->setDispatcher($container['dispatcher']); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
foreach ($container['console.commands'] as $command) { |
42
|
|
|
$console->add($command); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
if (!is_null($container['console.cache'])) { |
46
|
|
|
if (!is_null($container['console.cache']) && !is_dir($container['console.cache'])) { |
47
|
|
|
mkdir($container['console.cache'], 0777, true); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$cacheFile = $container['console.cache'] . '/saxulum-console.php'; |
51
|
|
|
if ($container['debug'] || !file_exists($cacheFile)) { |
52
|
|
|
file_put_contents( |
53
|
|
|
$cacheFile, |
54
|
|
|
'<?php return ' . var_export($container['console.command.search'](), true) . ';' |
55
|
|
|
); |
56
|
|
|
} |
57
|
|
|
$commandsMap = require($cacheFile); |
58
|
|
|
} else { |
59
|
|
|
$commandsMap = $container['console.command.search'](); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
foreach ($commandsMap as $commandClass) { |
63
|
|
|
$command = new $commandClass; |
64
|
|
|
$console->add($command); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return $console; |
68
|
|
|
}; |
69
|
|
|
|
70
|
|
|
$container['console.command.search'] = $container->protect(function () use ($container) { |
71
|
|
|
$commandsMap = array(); |
72
|
|
|
foreach ($container['console.command.paths'] as $path) { |
73
|
|
|
foreach (Finder::create()->files()->name('*.php')->in($path) as $file) { |
74
|
|
|
/** @var SplFileInfo $file */ |
75
|
|
|
$classes = ClassFinder::findClasses($file->getContents()); |
76
|
|
|
foreach ($classes as $class) { |
77
|
|
|
$reflectionClass = new \ReflectionClass($class); |
78
|
|
|
if($reflectionClass->isSubclassOf('Saxulum\Console\Command\AbstractPimpleCommand') && |
79
|
|
|
$reflectionClass->isInstantiable()) { |
80
|
|
|
$commandsMap[] = $class; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return $commandsMap; |
87
|
|
|
}); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|