-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConsole.php
More file actions
146 lines (127 loc) · 5.63 KB
/
Console.php
File metadata and controls
146 lines (127 loc) · 5.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<?php declare(strict_types=1);
/**
* @package Triangle Console Component
* @link https://github.com/Triangle-org/Console
*
* @author Ivan Zorin <creator@localzet.com>
* @copyright Copyright (c) 2023-2024 Triangle Framework Team
* @license https://www.gnu.org/licenses/agpl-3.0 GNU Affero General Public License v3.0
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* For any questions, please contact <triangle@localzet.com>
*/
namespace Triangle;
use Composer\InstalledVersions;
use ErrorException;
use Exception;
use localzet\Console\Util;
use Phar;
use ReflectionClass;
use ReflectionException;
use RuntimeException;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Triangle\Engine\Config;
use Triangle\Engine\Plugin;
/**
*
*/
class Console extends \localzet\Console
{
/**
* @param array $config
* @param bool $installInternalCommands
* @throws ErrorException
*/
public function __construct(protected array $config = [], bool $installInternalCommands = true)
{
if (!InstalledVersions::isInstalled('triangle/engine')) {
throw new RuntimeException('Triangle\\Console не может работать без Triangle\\Engine. Для запуска вне среды Triangle используйте `localzet/console`.');
}
if (Config::isLoaded()) {
$base_path = defined('BASE_PATH') ? BASE_PATH : (InstalledVersions::getRootPackage()['install_path'] ?? null);
$this->config += config('console', ['build' => [
'input_dir' => $base_path,
'output_dir' => $base_path . DIRECTORY_SEPARATOR . 'build',
'exclude_pattern' => '#^(?!.*(composer.json|/.github/|/.idea/|/.git/|/.setting/|/runtime/|/vendor-bin/|/build/))(.*)$#',
'exclude_files' => ['.env', 'LICENSE', 'composer.json', 'composer.lock', 'triangle.phar', 'triangle'],
'phar_alias' => 'triangle',
'phar_filename' => 'triangle.phar',
'phar_stub' => 'master',
'signature_algorithm' => Phar::SHA256,
'private_key_file' => '',
'php_version' => 8.3,
'php_ini' => 'memory_limit = 256M',
'bin_filename' => 'triangle',
]]);
$this->loadFromConfig(config('command', []));
// Грузим команды из /app/command/*.php
$command_path = Util::guessPath(app_path(), 'command', true);
$command_path && $this->installCommands($command_path);
// Грузим команды из /plugin/{plugin}/app/command/*.php
Plugin::app_reduce(function ($plugin, $config) {
$this->loadFromConfig($config['command'] ?? []);
$plugin_path = config('app.plugin_alias', 'plugin');
$base_path = "$plugin_path/app/$plugin";
$command_str = Util::guessPath(base_path($base_path), 'command');
if ($command_str) {
$path = "$base_path/$command_str";
$this->installCommands(base_path($path), str_replace('/', "\\", $path));
}
});
// Грузим команды из /config/plugin/{vendor}/{plugin}/command.php
Plugin::plugin_reduce(function ($vendor, $plugins, $plugin, $config) {
$this->loadFromConfig($config['command'] ?? []);
});
}
$this->config['name'] = 'Triangle Console';
$this->config['version'] = InstalledVersions::getPrettyVersion('triangle/console');
parent::__construct($this->config, $installInternalCommands);
}
/**
* @throws ErrorException
* @throws Exception
*/
public static function runAll(
array $config = [],
bool $internal = true,
?InputInterface $input = null,
?OutputInterface $output = null
): void
{
$self = new self($config, $internal);
$self->run($input, $output);
}
/**
* @param array $config
* @throws ReflectionException
*/
protected function loadFromConfig(array $config): void
{
foreach ($config as $class_name) {
$reflection = new ReflectionClass($class_name);
if ($reflection->isAbstract()) continue;
$properties = $reflection->getStaticProperties();
$name = $properties['defaultName'] ?? null;
if (!$name) throw new RuntimeException("У команды $class_name нет defaultName");
$this->add(new $class_name(config('console', [])));
}
}
public function installInternalCommands(): void
{
parent::installInternalCommands();
$this->installCommands(rtrim(InstalledVersions::getInstallPath('triangle/console'), '/') . '/src/Commands', 'Triangle\\Console\\Commands');
}
}