3 namespace Drupal\drupalmoduleupgrader;
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;
13 * Base class for indexers.
15 abstract class IndexerBase extends CorePluginBase implements IndexerInterface, ContainerFactoryPluginInterface {
18 * @var \Drupal\Core\Database\Connection
23 * @var TargetInterface
32 public function __construct(array $configuration, $plugin_id, $plugin_definition, DatabaseConnection $db, TargetInterface $target = NULL) {
33 parent::__construct($configuration, $plugin_id, $plugin_definition);
44 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
49 $container->get('database')
56 public function bind(TargetInterface $module) {
57 $this->target = $module;
58 $this->table = $module->id() . '__' . $this->getPluginId();
60 $schema = $this->db->schema();
61 if ($schema->tableExists($this->table)) {
65 $schema->createTable($this->table, [ 'fields' => $this->getFields() ]);
73 public function build() {
74 /** @var \Symfony\Component\Finder\SplFileInfo $file */
75 foreach ($this->target->getFinder() as $file) {
76 $this->addFile($file->getPathname());
83 public function clear() {
84 $this->db->truncate($this->table)->execute();
90 public function destroy() {
91 $this->db->schema()->dropTable($this->table);
97 public function has($identifier) {
98 return (boolean) $this->getQuery()
99 ->condition('id', $identifier)
108 public function hasAny(array $identifiers) {
109 return $this->has($identifiers);
115 public function hasAll(array $identifiers) {
116 $count = $this->getQuery()
117 ->condition('id', $identifiers)
122 return ($count == sizeof(array_unique($identifiers)));
128 public function add(NodeInterface $node) {
130 ->insert($this->table)
132 'id' => (string) $node->getName(),
133 'file' => $node->getFilename(),
141 public function deleteFile($path) {
143 ->delete($this->table)
144 ->condition('file', $path)
151 public function delete($identifier) {
153 ->delete($this->table)
154 ->condition('id', $identifier)
161 public function getMultiple(array $identifiers) {
162 return new NodeCollection(array_filter(array_map([ $this, 'get' ], $identifiers)));
168 public function getAll() {
169 return $this->getMultiple($this->getQuery(['id'])->distinct()->execute()->fetchCol());
175 public function getFields() {
193 public function getQuery(array $fields = []) {
195 ->select($this->table)
196 ->fields($this->table, $fields);