X-Git-Url: http://www.aleph1.co.uk/gitweb/?p=yaffs-website;a=blobdiff_plain;f=web%2Fmodules%2Fcontrib%2Fdrupalmoduleupgrader%2Fsrc%2FIndexerBase.php;fp=web%2Fmodules%2Fcontrib%2Fdrupalmoduleupgrader%2Fsrc%2FIndexerBase.php;h=24cb4d49492dfb4265f5a38abcddd8050bf520a8;hp=0000000000000000000000000000000000000000;hb=8acec36f19c470dfcda1ae2336826a782f41874c;hpb=e0411c4e83ba0d079034db83c3f7f55be24a0e35 diff --git a/web/modules/contrib/drupalmoduleupgrader/src/IndexerBase.php b/web/modules/contrib/drupalmoduleupgrader/src/IndexerBase.php new file mode 100644 index 000000000..24cb4d494 --- /dev/null +++ b/web/modules/contrib/drupalmoduleupgrader/src/IndexerBase.php @@ -0,0 +1,199 @@ +db = $db; + + if ($target) { + $this->bind($target); + } + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { + return new static( + $configuration, + $plugin_id, + $plugin_definition, + $container->get('database') + ); + } + + /** + * {@inheritdoc} + */ + public function bind(TargetInterface $module) { + $this->target = $module; + $this->table = $module->id() . '__' . $this->getPluginId(); + + $schema = $this->db->schema(); + if ($schema->tableExists($this->table)) { + $this->clear(); + } + else { + $schema->createTable($this->table, [ 'fields' => $this->getFields() ]); + } + $this->build(); + } + + /** + * {@inheritdoc} + */ + public function build() { + /** @var \Symfony\Component\Finder\SplFileInfo $file */ + foreach ($this->target->getFinder() as $file) { + $this->addFile($file->getPathname()); + } + } + + /** + * {@inheritdoc} + */ + public function clear() { + $this->db->truncate($this->table)->execute(); + } + + /** + * {@inheritdoc} + */ + public function destroy() { + $this->db->schema()->dropTable($this->table); + } + + /** + * {@inheritdoc} + */ + public function has($identifier) { + return (boolean) $this->getQuery() + ->condition('id', $identifier) + ->countQuery() + ->execute() + ->fetchField(); + } + + /** + * {@inheritdoc} + */ + public function hasAny(array $identifiers) { + return $this->has($identifiers); + } + + /** + * {@inheritdoc} + */ + public function hasAll(array $identifiers) { + $count = $this->getQuery() + ->condition('id', $identifiers) + ->countQuery() + ->execute() + ->fetchField(); + + return ($count == sizeof(array_unique($identifiers))); + } + + /** + * {@inheritdoc} + */ + public function add(NodeInterface $node) { + $this->db + ->insert($this->table) + ->fields([ + 'id' => (string) $node->getName(), + 'file' => $node->getFilename(), + ]) + ->execute(); + } + + /** + * {@inheritdoc} + */ + public function deleteFile($path) { + $this->db + ->delete($this->table) + ->condition('file', $path) + ->execute(); + } + + /** + * {@inheritdoc} + */ + public function delete($identifier) { + $this->db + ->delete($this->table) + ->condition('id', $identifier) + ->execute(); + } + + /** + * {@inheritdoc} + */ + public function getMultiple(array $identifiers) { + return new NodeCollection(array_filter(array_map([ $this, 'get' ], $identifiers))); + } + + /** + * {@inheritdoc} + */ + public function getAll() { + return $this->getMultiple($this->getQuery(['id'])->distinct()->execute()->fetchCol()); + } + + /** + * {@inheritdoc} + */ + public function getFields() { + return [ + 'id' => [ + 'type' => 'varchar', + 'length' => 255, + 'not null' => TRUE, + ], + 'file' => [ + 'type' => 'varchar', + 'length' => 255, + 'not null' => TRUE, + ], + ]; + } + + /** + * {@inheritdoc} + */ + public function getQuery(array $fields = []) { + return $this->db + ->select($this->table) + ->fields($this->table, $fields); + } + +}