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