1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* ValidateDirectoryCommand class file |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace Graviton\JsonSchemaBundle\Command; |
7
|
|
|
|
8
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
9
|
|
|
use Symfony\Component\Console\Input\InputOption; |
10
|
|
|
use Symfony\Component\Finder\Finder; |
11
|
|
|
use Symfony\Component\Finder\SplFileInfo; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Validate JSON definition in directory |
15
|
|
|
* |
16
|
|
|
* @author List of contributors <https://github.com/libgraviton/graviton/graphs/contributors> |
17
|
|
|
* @license https://opensource.org/licenses/MIT MIT License |
18
|
|
|
* @link http://swisscom.ch |
19
|
|
|
*/ |
20
|
|
|
class ValidateDirectoryCommand extends AbstractValidateCommand |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
private $defaultPath; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Set default scan path |
29
|
|
|
* |
30
|
|
|
* @param string $path Path |
31
|
|
|
* @return void |
32
|
|
|
*/ |
33
|
|
|
public function setDefaultPath($path) |
34
|
|
|
{ |
35
|
|
|
$this->defaultPath = $path; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Configures the current command. |
40
|
|
|
* |
41
|
|
|
* @return void |
42
|
|
|
*/ |
43
|
|
|
protected function configure() |
44
|
|
|
{ |
45
|
|
|
parent::configure(); |
46
|
|
|
|
47
|
|
|
if ($this->getName() === null) { |
48
|
|
|
$this->setName('graviton:validate:definition:directory'); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
$this |
52
|
|
|
->setDescription('Validate a JSON definition') |
53
|
|
|
->addOption( |
54
|
|
|
'path', |
55
|
|
|
'', |
56
|
|
|
InputOption::VALUE_OPTIONAL, |
57
|
|
|
'Path to the json definition.' |
58
|
|
|
); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Load JSON definitions |
63
|
|
|
* |
64
|
|
|
* @param InputInterface $input Command input |
65
|
|
|
* @return SplFileInfo[] |
66
|
|
|
*/ |
67
|
|
|
protected function loadDefinitions(InputInterface $input) |
68
|
|
|
{ |
69
|
|
|
$path = $input->getOption('path'); |
70
|
|
|
if ($path === null) { |
71
|
|
|
$path = $this->defaultPath; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
// if we are vendorized we will search all vendor paths |
75
|
|
|
if (strpos($path, 'vendor/graviton/graviton')) { |
76
|
|
|
$path .= '/../../'; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$finder = (new Finder()) |
80
|
|
|
->files() |
81
|
|
|
->in($path) |
82
|
|
|
->name('*.json') |
83
|
|
|
->notName('_*') |
84
|
|
|
->path('/(^|\/)resources\/definition($|\/)/i') |
85
|
|
|
->notPath('/(^|\/)Tests($|\/)/i'); |
86
|
|
|
return iterator_to_array($finder); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|