Skip to content

Commit

Permalink
Merge pull request #117 from pantheon-systems/cms-399-psr14-bridge
Browse files Browse the repository at this point in the history
Add Psr14Bridge class in search_api_pantheon.
  • Loading branch information
stovak authored Nov 9, 2021
2 parents 418a9f8 + 6d963e1 commit 6b653dd
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Services/SolariumClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use Solarium\Core\Query\QueryInterface;
use Solarium\Core\Query\Result\ResultInterface;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Drupal\search_api_solr\Solarium\EventDispatcher\Psr14Bridge;
use Drupal\search_api_pantheon\Solarium\EventDispatcher\Psr14Bridge;
use Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher;

/**
Expand Down
35 changes: 35 additions & 0 deletions src/Solarium/EventDispatcher/EventProxy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Drupal\search_api_pantheon\Solarium\EventDispatcher;

use Symfony\Component\EventDispatcher\Event;

/**
* A proxy for events defined by symfony contracts to be used with older Drupal.
*/
class EventProxy extends Event {
/**
* @var \Symfony\Contracts\EventDispatcher\Event
*/
protected $event;

public function __construct($event) {
$this->event = $event;
}

public function isPropagationStopped() {
return $this->event->isPropagationStopped();
}

public function stopPropagation() {
$this->event->stopPropagation();
}

/**
* Proxies all method calls to the original event.
*/
public function __call($method, $arguments) {
return $this->event->{$method}(...$arguments);
}

}
34 changes: 34 additions & 0 deletions src/Solarium/EventDispatcher/Psr14Bridge.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Drupal\search_api_pantheon\Solarium\EventDispatcher;

use Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher;
use Psr\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\Event;

/**
* A helper to decorate the legacy EventDispatcherInterface::dispatch().
*/
final class Psr14Bridge extends ContainerAwareEventDispatcher implements EventDispatcherInterface {

/**
* @var \Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher
*/
protected $dispatcher;

public function __construct(ContainerAwareEventDispatcher $eventDispatcher) {
$this->dispatcher = $eventDispatcher;
}

public function dispatch($event, Event $null = NULL) {
if (\is_object($event)) {
return $this->dispatcher->dispatch(\get_class($event), new EventProxy($event));
}
return $this->dispatcher->dispatch($event, $null);
}

public function addListener($event_name, $listener, $priority = 0) {
$this->dispatcher->addListener($event_name, $listener, $priority);
}

}

0 comments on commit 6b653dd

Please sign in to comment.