|
1
|
|
|
<?php namespace Limoncello\Commands\Traits; |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Copyright 2015-2018 [email protected] |
|
5
|
|
|
* |
|
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
7
|
|
|
* you may not use this file except in compliance with the License. |
|
8
|
|
|
* You may obtain a copy of the License at |
|
9
|
|
|
* |
|
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
|
11
|
|
|
* |
|
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
|
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
|
14
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
15
|
|
|
* See the License for the specific language governing permissions and |
|
16
|
|
|
* limitations under the License. |
|
17
|
|
|
*/ |
|
18
|
|
|
|
|
19
|
|
|
use Composer\Composer; |
|
20
|
|
|
use Limoncello\Commands\CommandConstants; |
|
21
|
|
|
use Limoncello\Commands\Exceptions\ConfigurationException; |
|
22
|
|
|
use Limoncello\Commands\Wrappers\ConsoleIoWrapper; |
|
23
|
|
|
use Limoncello\Contracts\Commands\IoInterface; |
|
24
|
|
|
use Limoncello\Contracts\Core\ApplicationInterface; |
|
25
|
|
|
use Psr\Container\ContainerInterface; |
|
26
|
|
|
use ReflectionClass; |
|
27
|
|
|
use ReflectionException; |
|
28
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
29
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @package Limoncello\Commands |
|
33
|
|
|
*/ |
|
34
|
|
|
trait CommandTrait |
|
35
|
|
|
{ |
|
36
|
|
|
/** |
|
37
|
|
|
* @param InputInterface $input |
|
38
|
|
|
* @param OutputInterface $output |
|
39
|
|
|
* |
|
40
|
|
|
* @return IoInterface |
|
41
|
|
|
*/ |
|
42
|
10 |
|
protected function wrapIo(InputInterface $input, OutputInterface $output): IoInterface |
|
43
|
|
|
{ |
|
44
|
10 |
|
return new ConsoleIoWrapper($input, $output); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @param Composer $composer |
|
49
|
|
|
* |
|
50
|
|
|
* @return ContainerInterface |
|
51
|
|
|
* |
|
52
|
|
|
* @throws ReflectionException |
|
53
|
|
|
*/ |
|
54
|
2 |
|
protected function createContainer(Composer $composer): ContainerInterface |
|
55
|
|
|
{ |
|
56
|
|
|
// use application auto loader otherwise no app classes will be visible for us |
|
57
|
2 |
|
$autoLoaderPath = $this->getAutoloadPath($composer); |
|
58
|
2 |
|
if (file_exists($autoLoaderPath) === true) { |
|
59
|
|
|
/** @noinspection PhpIncludeInspection */ |
|
60
|
2 |
|
require_once $autoLoaderPath; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
2 |
|
$extra = $this->getExtra($composer); |
|
64
|
2 |
|
$appKey = CommandConstants::COMPOSER_JSON__EXTRA__APPLICATION; |
|
65
|
2 |
|
$classKey = CommandConstants::COMPOSER_JSON__EXTRA__APPLICATION__CLASS; |
|
66
|
2 |
|
$appClass = $extra[$appKey][$classKey] ?? CommandConstants::DEFAULT_APPLICATION_CLASS_NAME; |
|
67
|
2 |
|
if ($this->isValidApplicationClass($appClass) === false) { |
|
68
|
1 |
|
$settingsPath = "extra->$appKey->$classKey"; |
|
69
|
1 |
|
throw new ConfigurationException( |
|
70
|
1 |
|
"Invalid application class specified '$appClass'. Check your settings at composer.json $settingsPath." |
|
71
|
|
|
); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
1 |
|
$container = $this->createApplication($appClass)->createContainer(); |
|
75
|
|
|
|
|
76
|
1 |
|
return $container; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @param Composer $composer |
|
81
|
|
|
* |
|
82
|
|
|
* @return string |
|
83
|
|
|
*/ |
|
84
|
2 |
|
protected function getAutoloadPath(Composer $composer): string |
|
85
|
|
|
{ |
|
86
|
2 |
|
return $composer->getConfig()->get('vendor-dir') . DIRECTORY_SEPARATOR . 'autoload.php'; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @param Composer $composer |
|
91
|
|
|
* |
|
92
|
|
|
* @return array |
|
93
|
|
|
*/ |
|
94
|
2 |
|
protected function getExtra(Composer $composer): array |
|
95
|
|
|
{ |
|
96
|
2 |
|
return $composer->getPackage()->getExtra(); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @param string $className |
|
101
|
|
|
* |
|
102
|
|
|
* @return bool |
|
103
|
|
|
* |
|
104
|
|
|
* @throws ReflectionException |
|
105
|
|
|
*/ |
|
106
|
2 |
|
protected function isValidApplicationClass(string $className): bool |
|
107
|
|
|
{ |
|
108
|
2 |
|
$reflectionClass = new ReflectionClass($className); |
|
109
|
|
|
|
|
110
|
|
|
return |
|
111
|
2 |
|
$reflectionClass->isInstantiable() === true && |
|
112
|
2 |
|
$reflectionClass->implementsInterface(ApplicationInterface::class) === true; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* @param string $className |
|
117
|
|
|
* |
|
118
|
|
|
* @return ApplicationInterface |
|
119
|
|
|
*/ |
|
120
|
1 |
|
protected function createApplication(string $className): ApplicationInterface |
|
121
|
|
|
{ |
|
122
|
1 |
|
return new $className(); |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|