Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / vendor / drupal / console / src / Command / Debug / ConfigValidateCommand.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Console\Command\Debug\ConfigValidateCommand.
6  */
7
8 namespace Drupal\Console\Command\Debug;
9
10 use Drupal\Core\Config\Schema\SchemaCheckTrait;
11 use Symfony\Component\Console\Input\InputInterface;
12 use Symfony\Component\Console\Input\InputOption;
13 use Symfony\Component\Console\Output\OutputInterface;
14 use Drupal\Console\Core\Command\ContainerAwareCommand;
15 use Symfony\Component\Console\Input\InputArgument;
16 use Drupal\Core\Config\TypedConfigManagerInterface;
17 use Drupal\Core\Serialization\Yaml;
18 use Drupal\Console\Command\Config\PrintConfigValidationTrait;
19
20 /**
21  * Class ConfigValidateCommand.
22  *
23  *@package Drupal\Console\Command\Debug
24  */
25 class ConfigValidateCommand extends ContainerAwareCommand
26 {
27     use SchemaCheckTrait;
28     use PrintConfigValidationTrait;
29
30     /**
31      * {@inheritdoc}
32      */
33     protected function configure()
34     {
35         $this
36             ->setName('debug:config:validate')
37             ->setDescription($this->trans('commands.debug.config.validate.description'))
38             ->addArgument(
39                 'filepath',
40                 InputArgument::REQUIRED
41             )
42             ->addArgument(
43                 'schema-filepath',
44                 InputArgument::REQUIRED
45             )
46             ->addOption(
47                 'schema-name',
48                 'sch',
49                 InputOption::VALUE_REQUIRED
50             )->setAliases(['dcv']);
51     }
52
53     /**
54    * {@inheritdoc}
55    */
56     protected function execute(InputInterface $input, OutputInterface $output)
57     {
58         /**
59          * @var TypedConfigManagerInterface $typedConfigManager
60          */
61         $typedConfigManager = $this->get('config.typed');
62
63         //Validate config file path
64         $configFilePath = $input->getArgument('filepath');
65         if (!file_exists($configFilePath)) {
66             $this->getIo()->info($this->trans('commands.debug.config.validate.messages.noConfFile'));
67             return 1;
68         }
69
70         //Validate schema path
71         $configSchemaFilePath = $input->getArgument('schema-filepath');
72         if (!file_exists($configSchemaFilePath)) {
73             $this->getIo()->info($this->trans('commands.debug.config.validate.messages.noConfSchema'));
74             return 1;
75         }
76
77         $config = Yaml::decode(file_get_contents($configFilePath));
78         $schema = Yaml::decode(file_get_contents($configSchemaFilePath));
79
80         //Get the schema name and check it exists in the schema array
81         $schemaName = $this->getSchemaName($configFilePath);
82         if (!array_key_exists($schemaName, $schema)) {
83             $this->getIo()->warning($this->trans('commands.debug.config.validate.messages.noSchemaName') . $schemaName);
84             return 1;
85         }
86
87         return $this->printResults($this->manualCheckConfigSchema($typedConfigManager, $config, $schema[$schemaName]));
88     }
89
90     private function getSchemaName($configFilePath)
91     {
92         $schemaName = $this->getIo()->getInput()->getOption('schema-name');
93         if ($schemaName === null) {
94             $schema_name = end(explode('/', $configFilePath));
95             $schemaName = substr($schema_name, 0, -4);
96         }
97         return $schemaName;
98     }
99
100     private function manualCheckConfigSchema(TypedConfigManagerInterface $typed_config, $config_data, $config_schema)
101     {
102         $data_definition = $typed_config->buildDataDefinition($config_schema, $config_data);
103         $this->schema = $typed_config->create($data_definition, $config_data);
104         $errors = [];
105         foreach ($config_data as $key => $value) {
106             $errors = array_merge($errors, $this->checkValue($key, $value));
107         }
108         if (empty($errors)) {
109             return true;
110         }
111
112         return $errors;
113     }
114 }