24cb4d49492dfb4265f5a38abcddd8050bf520a8
[yaffs-website] / web / modules / contrib / drupalmoduleupgrader / src / IndexerBase.php
1 <?php
2
3 namespace Drupal\drupalmoduleupgrader;
4
5 use Drupal\Core\Database\Connection as DatabaseConnection;
6 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
7 use Drupal\Core\Plugin\PluginBase as CorePluginBase;
8 use Pharborist\NodeCollection;
9 use Pharborist\NodeInterface;
10 use Symfony\Component\DependencyInjection\ContainerInterface;
11
12 /**
13  * Base class for indexers.
14  */
15 abstract class IndexerBase extends CorePluginBase implements IndexerInterface, ContainerFactoryPluginInterface {
16
17   /**
18    * @var \Drupal\Core\Database\Connection
19    */
20   protected $db;
21
22   /**
23    * @var TargetInterface
24    */
25   protected $target;
26
27   /**
28    * @var string
29    */
30   protected $table;
31
32   public function __construct(array $configuration, $plugin_id, $plugin_definition, DatabaseConnection $db, TargetInterface $target = NULL) {
33     parent::__construct($configuration, $plugin_id, $plugin_definition);
34     $this->db = $db;
35
36     if ($target) {
37       $this->bind($target);
38     }
39   }
40
41   /**
42    * {@inheritdoc}
43    */
44   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
45     return new static(
46       $configuration,
47       $plugin_id,
48       $plugin_definition,
49       $container->get('database')
50     );
51   }
52
53   /**
54    * {@inheritdoc}
55    */
56   public function bind(TargetInterface $module) {
57     $this->target = $module;
58     $this->table = $module->id() . '__' . $this->getPluginId();
59
60     $schema = $this->db->schema();
61     if ($schema->tableExists($this->table)) {
62       $this->clear();
63     }
64     else {
65       $schema->createTable($this->table, [ 'fields' => $this->getFields() ]);
66     }
67     $this->build();
68   }
69
70   /**
71    * {@inheritdoc}
72    */
73   public function build() {
74     /** @var \Symfony\Component\Finder\SplFileInfo $file */
75     foreach ($this->target->getFinder() as $file) {
76       $this->addFile($file->getPathname());
77     }
78   }
79
80   /**
81    * {@inheritdoc}
82    */
83   public function clear() {
84     $this->db->truncate($this->table)->execute();
85   }
86
87   /**
88    * {@inheritdoc}
89    */
90   public function destroy() {
91     $this->db->schema()->dropTable($this->table);
92   }
93
94   /**
95    * {@inheritdoc}
96    */
97   public function has($identifier) {
98     return (boolean) $this->getQuery()
99       ->condition('id', $identifier)
100       ->countQuery()
101       ->execute()
102       ->fetchField();
103   }
104
105   /**
106    * {@inheritdoc}
107    */
108   public function hasAny(array $identifiers) {
109     return $this->has($identifiers);
110   }
111
112   /**
113    * {@inheritdoc}
114    */
115   public function hasAll(array $identifiers) {
116     $count = $this->getQuery()
117       ->condition('id', $identifiers)
118       ->countQuery()
119       ->execute()
120       ->fetchField();
121
122     return ($count == sizeof(array_unique($identifiers)));
123   }
124
125   /**
126    * {@inheritdoc}
127    */
128   public function add(NodeInterface $node) {
129     $this->db
130       ->insert($this->table)
131       ->fields([
132         'id' => (string) $node->getName(),
133         'file' => $node->getFilename(),
134       ])
135       ->execute();
136   }
137
138   /**
139    * {@inheritdoc}
140    */
141   public function deleteFile($path) {
142     $this->db
143       ->delete($this->table)
144       ->condition('file', $path)
145       ->execute();
146   }
147
148   /**
149    * {@inheritdoc}
150    */
151   public function delete($identifier) {
152     $this->db
153       ->delete($this->table)
154       ->condition('id', $identifier)
155       ->execute();
156   }
157
158   /**
159    * {@inheritdoc}
160    */
161   public function getMultiple(array $identifiers) {
162     return new NodeCollection(array_filter(array_map([ $this, 'get' ], $identifiers)));
163   }
164
165   /**
166    * {@inheritdoc}
167    */
168   public function getAll() {
169     return $this->getMultiple($this->getQuery(['id'])->distinct()->execute()->fetchCol());
170   }
171
172   /**
173    * {@inheritdoc}
174    */
175   public function getFields() {
176     return [
177       'id' => [
178         'type' => 'varchar',
179         'length' => 255,
180         'not null' => TRUE,
181       ],
182       'file' => [
183         'type' => 'varchar',
184         'length' => 255,
185         'not null' => TRUE,
186       ],
187     ];
188   }
189
190   /**
191    * {@inheritdoc}
192    */
193   public function getQuery(array $fields = []) {
194     return $this->db
195       ->select($this->table)
196       ->fields($this->table, $fields);
197   }
198
199 }