forked from TYPO3/coding-standards
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApplication.php
More file actions
119 lines (97 loc) · 3.16 KB
/
Copy pathApplication.php
File metadata and controls
119 lines (97 loc) · 3.16 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
<?php
declare(strict_types=1);
/*
* This file is part of the TYPO3 project.
*
* (c) 2019-2026 Benni Mack
* Simon Gilli
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/
namespace TYPO3\CodingStandards\Console;
use Symfony\Component\Console\Application as BaseApplication;
use Symfony\Component\Console\Input\InputDefinition;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Filesystem\Filesystem;
use TYPO3\CodingStandards\Console\Command\SetupCommand;
use TYPO3\CodingStandards\Console\Command\UpdateCommand;
/**
* @internal
*/
final class Application extends BaseApplication
{
/**
* @var string
*/
public const VERSION = '0.8.0';
/**
* getcwd() equivalent which always returns a string.
*
* @throws \RuntimeException
*/
private static function getCwd(bool $allowEmpty = false): string
{
$cwd = getcwd();
// @codeCoverageIgnoreStart
// fallback to realpath('') just in case this works but odds are it would break as well if we are in a case
// where getcwd fails
if ($cwd === false) {
$cwd = realpath('');
}
// crappy state, assume '' and hopefully relative paths allow things to continue
if ($cwd === false) {
if ($allowEmpty) {
return '';
}
throw new \RuntimeException('Could not determine the current working directory');
}
// @codeCoverageIgnoreEnd
return $cwd;
}
public static function getProjectDir(): string
{
return self::getCwd(true);
}
/**
* @throws \RuntimeException
*/
public static function getTargetDir(InputInterface $input): string
{
/** @var string|null $targetDir */
$targetDir = $input->getParameterOption(['--target-dir', '-d'], null, true);
if ($targetDir === null) {
$targetDir = self::getProjectDir();
}
if (!(new Filesystem())->isAbsolutePath($targetDir)) {
$targetDir = self::getProjectDir() . '/' . $targetDir;
}
if (!is_dir($targetDir)) {
throw new \RuntimeException(sprintf('Invalid target directory specified, %s does not exist.', $targetDir));
}
return $targetDir;
}
public function __construct()
{
parent::__construct('TYPO3 Coding Standards', self::VERSION);
// in alphabetical order
$this->add(new SetupCommand());
$this->add(new UpdateCommand());
//$this->setDefaultCommand('setup', false);
}
protected function getDefaultInputDefinition(): InputDefinition
{
$inputDefinition = parent::getDefaultInputDefinition();
$inputDefinition->addOption(new InputOption(
'--target-dir',
'-d',
InputOption::VALUE_REQUIRED,
'If specified, use the given directory as target directory',
self::getProjectDir()
));
return $inputDefinition;
}
}