Security update for Core, with self-updated composer
[yaffs-website] / web / core / modules / migrate_drupal / src / Plugin / migrate / source / Variable.php
1 <?php
2
3 namespace Drupal\migrate_drupal\Plugin\migrate\source;
4
5 use Drupal\Core\Entity\EntityManagerInterface;
6 use Drupal\Core\State\StateInterface;
7 use Drupal\migrate\Plugin\MigrationInterface;
8
9 /**
10  * Drupal variable source from database.
11  *
12  * This source class always returns a single row and as such is not a good
13  * example for any normal source class returning multiple rows.
14  *
15  * @MigrateSource(
16  *   id = "variable"
17  * )
18  */
19 class Variable extends DrupalSqlBase {
20
21   /**
22    * The variable names to fetch.
23    *
24    * @var array
25    */
26   protected $variables;
27
28   /**
29    * {@inheritdoc}
30    */
31   public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration, StateInterface $state, EntityManagerInterface $entity_manager) {
32     parent::__construct($configuration, $plugin_id, $plugin_definition, $migration, $state, $entity_manager);
33     $this->variables = $this->configuration['variables'];
34   }
35
36   /**
37    * {@inheritdoc}
38    */
39   protected function initializeIterator() {
40     return new \ArrayIterator([$this->values()]);
41   }
42
43   /**
44    * Return the values of the variables specified in the plugin configuration.
45    *
46    * @return array
47    *   An associative array where the keys are the variables specified in the
48    *   plugin configuration and the values are the values found in the source.
49    *   Only those values are returned that are actually in the database.
50    */
51   protected function values() {
52     // Create an ID field so we can record migration in the map table.
53     // Arbitrarily, use the first variable name.
54     $values['id'] = reset($this->variables);
55     return $values + array_map('unserialize', $this->prepareQuery()->execute()->fetchAllKeyed());
56   }
57
58   /**
59    * {@inheritdoc}
60    */
61   public function count($refresh = FALSE) {
62     return intval($this->query()->countQuery()->execute()->fetchField() > 0);
63   }
64
65   /**
66    * {@inheritdoc}
67    */
68   public function fields() {
69     return array_combine($this->variables, $this->variables);
70   }
71
72   /**
73    * {@inheritdoc}
74    */
75   public function query() {
76     return $this->getDatabase()
77       ->select('variable', 'v')
78       ->fields('v', ['name', 'value'])
79       ->condition('name', $this->variables, 'IN');
80   }
81
82   /**
83    * {@inheritdoc}
84    */
85   public function getIds() {
86     $ids['id']['type'] = 'string';
87     return $ids;
88   }
89
90 }