<?php
namespace Roothirsch\CoreBundle\Site;
use ApiPlatform\Core\EventListener\EventPriorities;
use Roothirsch\CoreBundle\Entity\User;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Finder\Finder;
use Symfony\Component\HttpKernel\KernelEvents;
use Twig\Environment;
use Twig\Loader\FilesystemLoader;
class TemplatePathInjection implements EventSubscriberInterface
{
/**
* @var SiteProvider
*/
protected $siteProvider;
/**
* @var Environment
*/
protected $twig;
/**
* @var String
*/
protected $projectDir;
public function __construct(SiteProvider $siteProvider, Environment $twig, String $projectDir) {
$this->siteProvider = $siteProvider;
$this->twig = $twig;
$this->projectDir = $projectDir;
}
/**
* Returns an array of event names this subscriber wants to listen to.
*
* The array keys are event names and the value can be:
*
* * The method name to call (priority defaults to 0)
* * An array composed of the method name to call and the priority
* * An array of arrays composed of the method names to call and respective
* priorities, or 0 if unset
*
* For instance:
*
* * array('eventName' => 'methodName')
* * array('eventName' => array('methodName', $priority))
* * array('eventName' => array(array('methodName1', $priority), array('methodName2')))
*
* @return array The event names to listen to
*/
public static function getSubscribedEvents(): array
{
return [
KernelEvents::REQUEST => ['onKernelRequest', EventPriorities::PRE_READ],
];
}
public function onKernelRequest(\Symfony\Component\HttpKernel\Event\RequestEvent $event)
{
$loader = $this->twig->getLoader();
if ($loader instanceof FilesystemLoader) {
$directories = [];
$finder = new Finder();
if(file_exists($this->projectDir."/vendor/roothirsch")){
$finder->in($this->projectDir . '/vendor/roothirsch/*/Resources')->name('views');
foreach($finder->directories() as $directory) {
$directories[] = $directory->getRealPath();
}
}
array_push($directories, $this->projectDir . '/themes/' . $this->siteProvider->getTheme());
foreach($directories as $templateDirectory) {
if (file_exists($templateDirectory)) {
$loader->prependPath($templateDirectory);
}
if (file_exists($templateDirectory . '/bundles/')) {
$finder = new Finder();
$finder->directories()->in($templateDirectory . '/bundles/');
foreach($finder as $directory) {
/** @var $directory \Symfony\Component\Finder\SplFileInfo */
$loader->prependPath($directory->getRealPath(), str_replace('Bundle', '', $directory->getBasename()));
}
}
}
}
}
}