Further modules included.
[yaffs-website] / web / modules / contrib / drupalmoduleupgrader / src / Plugin / DMU / Converter / Functions / VariableGet.php
1 <?php
2
3 namespace Drupal\drupalmoduleupgrader\Plugin\DMU\Converter\Functions;
4
5 use Drupal\Component\Serialization\Yaml;
6 use Drupal\Core\Config\InstallStorage;
7 use Drupal\drupalmoduleupgrader\TargetInterface;
8 use Pharborist\Functions\FunctionCallNode;
9 use Pharborist\Objects\ClassMethodCallNode;
10 use Pharborist\Types\ScalarNode;
11
12 /**
13  * @Converter(
14  *  id = "variable_get",
15  *  description = @Translation("Replaces variable_get() calls with Configuration API.")
16  * )
17  */
18 class VariableGet extends VariableAPI {
19
20   /**
21    * Default configuration values extracted from rewritten calls to
22    * variable_get().
23    *
24    * @var mixed[]
25    */
26   private $defaults = [];
27
28   /**
29    * The schema accompanying any extracted default values.
30    *
31    * @var array
32    */
33   private $schema = [];
34
35   /**
36    * {@inheritdoc}
37    */
38   public function rewrite(FunctionCallNode $call, TargetInterface $target) {
39     if ($this->tryRewrite($call, $target)) {
40       $arguments = $call->getArguments();
41       $key = $arguments[0]->toValue();
42
43       if ($arguments[1] instanceof ScalarNode) {
44         // @TODO Couldn't convert() derive the schema from $this->defaults?
45         // That'd be preferable to having yet another state property ($schema)
46         // on this class.
47         $this->defaults[$key] = $arguments[1]->toValue();
48         $this->schema[$key]['type'] = getType($this->defaults[$key]);
49       }
50       else {
51         $comment = <<<END
52 Could not extract the default value because it is either indeterminate, or
53 not scalar. You'll need to provide a default value in
54 config/install/@module.settings.yml and config/schema/@module.schema.yml.
55 END;
56         $variables = [ '@module' => $target->id() ];
57         $this->buildFixMe($comment, $variables)->prependTo($call->getStatement());
58       }
59
60       return ClassMethodCallNode::create('\Drupal', 'config')
61         ->appendArgument($target->id() . '.settings')
62         ->appendMethodCall('get')
63         ->appendArgument(clone $arguments[0]);
64     }
65   }
66
67   /**
68    * {@inheritdoc}
69    */
70   public function convert(TargetInterface $target) {
71     parent::convert($target);
72
73     if ($this->defaults && $this->schema) {
74       $group = $target->id() . '.settings';
75
76       $this->write($target, InstallStorage::CONFIG_INSTALL_DIRECTORY . '/' . $group . '.yml', Yaml::encode($this->defaults));
77       $this->defaults = [];
78
79       $schema = [
80         $group => [
81           'type' => 'mapping',
82           'label' => (string) $this->t('Settings'),
83           'mapping' => $this->schema,
84         ],
85       ];
86       $this->write($target, InstallStorage::CONFIG_SCHEMA_DIRECTORY . '/' . $target->id() . '.schema.yml', Yaml::encode($schema));
87       $this->schema = [];
88     }
89   }
90
91 }