5 * Contains \Drupal\Console\Command\Debug\ConfigValidateCommand.
8 namespace Drupal\Console\Command\Debug;
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;
21 * Class ConfigValidateCommand.
23 *@package Drupal\Console\Command\Debug
25 class ConfigValidateCommand extends ContainerAwareCommand
28 use PrintConfigValidationTrait;
33 protected function configure()
36 ->setName('debug:config:validate')
37 ->setDescription($this->trans('commands.debug.config.validate.description'))
40 InputArgument::REQUIRED
44 InputArgument::REQUIRED
49 InputOption::VALUE_REQUIRED
50 )->setAliases(['dcv']);
56 protected function execute(InputInterface $input, OutputInterface $output)
59 * @var TypedConfigManagerInterface $typedConfigManager
61 $typedConfigManager = $this->get('config.typed');
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'));
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'));
77 $config = Yaml::decode(file_get_contents($configFilePath));
78 $schema = Yaml::decode(file_get_contents($configSchemaFilePath));
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);
87 return $this->printResults($this->manualCheckConfigSchema($typedConfigManager, $config, $schema[$schemaName]));
90 private function getSchemaName($configFilePath)
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);
100 private function manualCheckConfigSchema(TypedConfigManagerInterface $typed_config, $config_data, $config_schema)
102 $data_definition = $typed_config->buildDataDefinition($config_schema, $config_data);
103 $this->schema = $typed_config->create($data_definition, $config_data);
105 foreach ($config_data as $key => $value) {
106 $errors = array_merge($errors, $this->checkValue($key, $value));
108 if (empty($errors)) {