class ContentHasherEventSubscriber in Tome 8
Event subscriber that keeps the content hash table up to date.
@internal
Hierarchy
- class \Drupal\tome_sync\EventSubscriber\ContentHasherEventSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface
Expanded class hierarchy of ContentHasherEventSubscriber
1 string reference to 'ContentHasherEventSubscriber'
- tome_sync.services.yml in modules/
tome_sync/ tome_sync.services.yml - modules/tome_sync/tome_sync.services.yml
1 service uses ContentHasherEventSubscriber
File
- modules/
tome_sync/ src/ EventSubscriber/ ContentHasherEventSubscriber.php, line 17
Namespace
Drupal\tome_sync\EventSubscriberView source
class ContentHasherEventSubscriber implements EventSubscriberInterface {
/**
* The content hasher.
*
* @var \Drupal\tome_sync\ContentHasherInterface
*/
protected $contentHasher;
/**
* The target content storage.
*
* @var \Drupal\Core\Config\FileStorage
*/
protected $contentStorage;
/**
* Creates a ContentHasherEventSubscriber object.
*
* @param \Drupal\tome_sync\ContentHasherInterface $content_hasher
* The content hasher.
* @param \Drupal\Core\Config\FileStorage $content_storage
* The target content storage.
*/
public function __construct(ContentHasherInterface $content_hasher, FileStorage $content_storage) {
$this->contentHasher = $content_hasher;
$this->contentStorage = $content_storage;
}
/**
* Maintains a hash of imported content to support partial imports.
*
* @param \Drupal\tome_sync\Event\ContentCrudEvent $event
* The content CRUD event.
*/
public function writeHash(ContentCrudEvent $event) {
$entity = $event
->getContent();
$content_name = TomeSyncHelper::getContentName($entity);
$file_path = $this->contentStorage
->getFilePath($content_name);
if (file_exists($file_path)) {
$encoded_content = file_get_contents($file_path);
$this->contentHasher
->writeHash($encoded_content, $content_name);
}
}
/**
* Maintains a hash of exported content to support partial imports.
*
* @param \Drupal\tome_sync\Event\ContentCrudEvent $event
* The content CRUD event.
*/
public function writeSourceHash(ContentCrudEvent $event) {
$entity = $event
->getContent();
$content_name = TomeSyncHelper::getContentName($entity);
$file_path = $this->contentStorage
->getFilePath($content_name);
if (file_exists($file_path)) {
$encoded_content = file_get_contents($file_path);
$this->contentHasher
->writeHash($encoded_content, $content_name);
}
}
/**
* Maintains a hash of imported content to support partial imports.
*
* @param \Drupal\tome_sync\Event\ContentCrudEvent $event
* The content CRUD event.
*/
public function deleteHash(ContentCrudEvent $event) {
$entity = $event
->getContent();
$content_name = TomeSyncHelper::getContentName($entity);
$this->contentHasher
->deleteHash($content_name);
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
$events[TomeSyncEvents::IMPORT_CONTENT][] = [
'writeHash',
];
$events[TomeSyncEvents::EXPORT_CONTENT][] = [
'writeSourceHash',
];
$events[TomeSyncEvents::DELETE_CONTENT][] = [
'deleteHash',
];
return $events;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ContentHasherEventSubscriber:: |
protected | property | The content hasher. | |
ContentHasherEventSubscriber:: |
protected | property | The target content storage. | |
ContentHasherEventSubscriber:: |
public | function | Maintains a hash of imported content to support partial imports. | |
ContentHasherEventSubscriber:: |
public static | function | ||
ContentHasherEventSubscriber:: |
public | function | Maintains a hash of imported content to support partial imports. | |
ContentHasherEventSubscriber:: |
public | function | Maintains a hash of exported content to support partial imports. | |
ContentHasherEventSubscriber:: |
public | function | Creates a ContentHasherEventSubscriber object. |