gnu-social/src/Util/GNUsocial.php

116 lines
3.8 KiB
PHP

<?php
/*
* This file is part of GNU social - https://www.gnu.org/software/social
*
* GNU social is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* GNU social is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with GNU social. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* Main GNU social entry point
*
* @package GNUsocial
* @category Framework
*
* StatusNet and GNU social 1
*
* @author Refer to CREDITS.md
* @copyright 2010 Free Software Foundation, Inc http://www.fsf.org
*
* GNU social 2
* @author Bruno Casteleiro <brunoccast@fc.up.pt>
* @author Diogo Cordeiro <diogo@fc.up.pt>
*
* GNU social 3
* @author Hugo Sales <hugo@fc.up.pt>
* @copyright 2018-2020 Free Software Foundation, Inc http://www.fsf.org
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
*/
namespace App\Util;
use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\Console\Event\ConsoleCommandEvent;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Contracts\Translation\TranslatorInterface;
class GNUsocial implements EventSubscriberInterface
{
protected ContainerInterface $container;
protected LoggerInterface $logger;
protected TranslatorInterface $translator;
/**
* Symfony dependency injection gives us access to these services
*/
public function __construct(ContainerInterface $container,
LoggerInterface $logger,
TranslatorInterface $translator)
{
$this->container = $container;
$this->logger = $logger;
$this->translator = $translator;
}
/**
* Store these services to be accessed statically and load extensions
*/
public function register(EventDispatcherInterface $event_dispatcher): void
{
Log::setLogger($this->logger);
GSEvent::setDispatcher($event_dispatcher);
I18n::setTranslator($this->translator);
ExtensionManager::loadExtensions();
}
/**
* Event very early on in the Symfony HTTP lifecycle, but after everyting is registered
* where we get access to the event dispatcher
*/
public function onKernelRequest(RequestEvent $event,
string $event_name,
EventDispatcherInterface $event_dispatcher): RequestEvent
{
$this->register($event_dispatcher);
return $event;
}
/**
* Event after everything is initialized when using the `bin/console` command
*/
public function onCommand(ConsoleCommandEvent $event,
string $event_name,
EventDispatcherInterface $event_dispatcher): ConsoleCommandEvent
{
$this->register($event_dispatcher);
return $event;
}
/**
* Tell Symfony which events we want to listen to, which Symfony detects and autowires
* due to this implementing the `EventSubscriberInterface`
*/
public static function getSubscribedEvents()
{
return [
KernelEvents::REQUEST => 'onKernelRequest',
'console.command' => 'onCommand',
];
}
}