class EventLogSubscriber

Defines an event subscriber to test logging during events in Package Manager.

Hierarchy

  • class \Drupal\package_manager_test_event_logger\EventSubscriber\EventLogSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of EventLogSubscriber

1 file declares its use of EventLogSubscriber
TemplateProjectTestBase.php in core/modules/package_manager/tests/src/Build/TemplateProjectTestBase.php
1 string reference to 'EventLogSubscriber'
package_manager_test_event_logger.services.yml in core/modules/package_manager/tests/modules/package_manager_test_event_logger/package_manager_test_event_logger.services.yml
core/modules/package_manager/tests/modules/package_manager_test_event_logger/package_manager_test_event_logger.services.yml
1 service uses EventLogSubscriber
package_manager_test_event_logger.subscriber in core/modules/package_manager/tests/modules/package_manager_test_event_logger/package_manager_test_event_logger.services.yml
Drupal\package_manager_test_event_logger\EventSubscriber\EventLogSubscriber

File

core/modules/package_manager/tests/modules/package_manager_test_event_logger/src/EventSubscriber/EventLogSubscriber.php, line 21

Namespace

Drupal\package_manager_test_event_logger\EventSubscriber
View source
final class EventLogSubscriber implements EventSubscriberInterface {
    
    /**
     * The name of the log file to write to.
     *
     * @var string
     */
    public const LOG_FILE_NAME = 'package_manager_test_event.log';
    
    /**
     * Excludes the log file from Package Manager operations.
     *
     * @param \Drupal\package_manager\Event\CollectPathsToExcludeEvent $event
     *   The event being handled.
     */
    public function excludeLogFile(CollectPathsToExcludeEvent $event) : void {
        $event->addPathsRelativeToProjectRoot([
            self::LOG_FILE_NAME,
        ]);
    }
    
    /**
     * Logs all events in the stage life cycle.
     *
     * @param \Drupal\package_manager\Event\StageEvent $event
     *   The event object.
     */
    public function logEventInfo(StageEvent $event) : void {
        $log_file = \Drupal::service(PathLocator::class)->getProjectRoot() . '/' . self::LOG_FILE_NAME;
        if (file_exists($log_file)) {
            $log_data = file_get_contents($log_file);
            $log_data = json_decode($log_data, TRUE, flags: JSON_THROW_ON_ERROR);
        }
        else {
            $log_data = [];
        }
        $log_data[] = [
            'event' => $event::class,
            'stage' => $event->stage::class,
        ];
        file_put_contents($log_file, json_encode($log_data, JSON_UNESCAPED_SLASHES));
    }
    
    /**
     * {@inheritdoc}
     */
    public static function getSubscribedEvents() : array {
        // This subscriber should run before every other validator, because the
        // purpose of this subscriber is to log all dispatched events.
        // @see \Drupal\package_manager\Validator\BaseRequirementsFulfilledValidator
        // @see \Drupal\package_manager\Validator\BaseRequirementValidatorTrait
        // @see \Drupal\package_manager\Validator\EnvironmentSupportValidator
        return [
            CollectPathsToExcludeEvent::class => [
                'excludeLogFile',
            ],
            PreCreateEvent::class => [
                'logEventInfo',
                PHP_INT_MAX,
            ],
            PostCreateEvent::class => [
                'logEventInfo',
                PHP_INT_MAX,
            ],
            PreRequireEvent::class => [
                'logEventInfo',
                PHP_INT_MAX,
            ],
            PostRequireEvent::class => [
                'logEventInfo',
                PHP_INT_MAX,
            ],
            PreApplyEvent::class => [
                'logEventInfo',
                PHP_INT_MAX,
            ],
            PostApplyEvent::class => [
                'logEventInfo',
                PHP_INT_MAX,
            ],
        ];
    }

}

Members

Title Sort descending Modifiers Object type Summary
EventLogSubscriber::excludeLogFile public function Excludes the log file from Package Manager operations.
EventLogSubscriber::getSubscribedEvents public static function
EventLogSubscriber::logEventInfo public function Logs all events in the stage life cycle.
EventLogSubscriber::LOG_FILE_NAME public constant The name of the log file to write to.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.