Skip to content

Commit

Permalink
Directly inject twig (#469)
Browse files Browse the repository at this point in the history
* dep on twig
* Properly registered deps
* Moved config up and adjusted paths
  • Loading branch information
helios-ag authored Jan 8, 2022
1 parent b693ece commit 43c785d
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 29 deletions.
14 changes: 7 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,17 @@
"require": {
"php": "^7.4 || ^8.0",
"ext-json": "*",
"symfony/framework-bundle": "^4.4 || ^5.0 || ^5.2 || ^6.0",
"symfony/twig-bundle": "^4.4 || ^5.0 || ^5.2 || ^6.0",
"symfony/form": "^4.4 || ^5.0 || ^5.2 || ^6.0",
"symfony/asset": "^4.4 || ^5.0 || ^5.2 || ^6.0",
"symfony/framework-bundle": "^4.4 || ^5.0 || ^5.2",
"symfony/twig-bundle": "^4.4 || ^5.0 || ^5.2",
"symfony/form": "^4.4 || ^5.0 || ^5.2",
"symfony/asset": "^4.4 || ^5.0 || ^5.2",
"studio-42/elfinder": "~2.1"
},
"require-dev" : {
"symfony/phpunit-bridge": "^4.1 || ^5.0 || ^5.2 || ^6.0",
"symfony/phpunit-bridge": "^4.1 || ^5.0 || ^5.2",
"matthiasnoback/symfony-config-test": "^4.0",
"matthiasnoback/symfony-dependency-injection-test": "^4.1",
"symfony/finder": "^4.4 || ^5.0 || ^5.2 || ^6.0",
"symfony/finder": "^4.4 || ^5.0 || ^5.2",
"php-coveralls/php-coveralls": "^2.0"
},
"suggest": {
Expand All @@ -50,7 +50,7 @@
},
"extra": {
"branch-alias": {
"dev-master": "11.0-dev"
"dev-master": "10.0-dev"
}
}
}
File renamed without changes.
8 changes: 4 additions & 4 deletions src/Resources/config/elfinder.xml → config/elfinder.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,18 @@
<argument type="service" id="request_stack"/>
<argument type="service" id="service_container"/>
</service>
<service id="fm_elfinder.loader.default" class="%fm_elfinder.loader%">
<service id="fm_elfinder.loader.default" class="%fm_elfinder.loader%" autowire="true">
<argument type="service" id="fm_elfinder.configurator"/>
</service>
<service id="FM\ElfinderBundle\Loader\ElFinderLoader" alias="fm_elfinder.loader.default"/>
<service id="twig.extension.fm_elfinder_init" class="FM\ElfinderBundle\Twig\Extension\FMElfinderExtension">
<argument type="service" id="twig"/>
<tag name="twig.extension" alias="fm_elfinder_init" />
</service>
<service id="FM\ElfinderBundle\Controller\ElFinderController">
<tag name="controller.service_arguments" />
<call method="setContainer">
<argument type="service" id="service_container"/>
</call>
<argument type="service" id="twig" />
<argument></argument>
</service>
</services>
</container>
File renamed without changes.
28 changes: 17 additions & 11 deletions src/Controller/ElFinderController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Exception;
use FM\ElfinderBundle\Loader\ElFinderLoader;
use FM\ElfinderBundle\Session\ElFinderSession;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\JsonResponse;
Expand All @@ -16,18 +15,27 @@
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use FM\ElfinderBundle\Event\ElFinderPreExecutionEvent;
use FM\ElfinderBundle\Event\ElFinderPostExecutionEvent;
use Symfony\Component\HttpKernel\HttpKernel;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Twig\Environment;

class ElFinderController extends AbstractController
class ElFinderController
{
private Environment $twig;

public function __construct(Environment $twig, array $params)
{
$this->twig = $twig;
$this->params = $params;
}

/**
* Renders Elfinder.
*
* @throws Exception
*/
public function show(Request $request, string $instance, string $homeFolder): Response
{
$efParameters = $this->container->getParameter('fm_elfinder');
$efParameters = $this->params;

if (empty($efParameters['instances'][$instance])) {
throw new NotFoundHttpException('Instance not found');
Expand All @@ -41,7 +49,7 @@ public function show(Request $request, string $instance, string $homeFolder): Re
$assetsPath = $efParameters['assets_path'];
$result = $this->selectEditor($parameters, $instance, $homeFolder, $assetsPath, $request->get('id'));

return $this->render($result['template'], $result['params']);
return new Response($this->twig->render($result['template'], $result['params']));
}

/**
Expand Down Expand Up @@ -187,17 +195,15 @@ private function selectEditor(array $parameters, string $instance, string $homeF
}
}

public function load(SessionInterface $session, EventDispatcherInterface $eventDispatcher, Request $request, string $instance, string $homeFolder): JsonResponse
public function load(SessionInterface $session, ElFinderLoader $loader, HttpKernelInterface $httpKernel, EventDispatcherInterface $eventDispatcher, Request $request, string $instance, string $homeFolder): JsonResponse
{
$efParameters = $this->container->getParameter('fm_elfinder');
$loader = $this->get('fm_elfinder.loader');
$efParameters = $this->params;
$loader->initBridge($instance, $efParameters); // builds up the Bridge object for the loader with the given instance

if ($loader instanceof ElFinderLoader) {
$loader->setSession(new ElFinderSession($session));
}
/** @var HttpKernel $httpKernel */
$httpKernel = $this->get('http_kernel');

$preExecutionEvent = new ElFinderPreExecutionEvent($request, $httpKernel, $instance, $homeFolder);
$eventDispatcher->dispatch($preExecutionEvent);

Expand All @@ -213,7 +219,7 @@ public function load(SessionInterface $session, EventDispatcherInterface $eventD
public function mainJS()
{
return new Response(
$this->renderView('@FMElfinder/Elfinder/helper/main.js.twig'),
$this->twig->render('@FMElfinder/Elfinder/helper/main.js.twig'),
200,
[
'Content-type' => 'text/javascript',
Expand Down
5 changes: 1 addition & 4 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;

/**
* Class Configuration.
*/
class Configuration implements ConfigurationInterface
final class Configuration implements ConfigurationInterface
{
/**
* {@inheritdoc}
Expand Down
10 changes: 7 additions & 3 deletions src/DependencyInjection/FMElfinderExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,29 @@

namespace FM\ElfinderBundle\DependencyInjection;

use FM\ElfinderBundle\Controller\ElFinderController;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\Extension\Extension;
use Symfony\Component\DependencyInjection\Loader;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;

final class FMElfinderExtension extends Extension
{
/**
* {@inheritdoc}
*/
public function load(array $configs, ContainerBuilder $container)
public function load(array $configs, ContainerBuilder $container): void
{
$config = $this->processConfiguration(new Configuration(), $configs);

$loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../../config'));
$loader->load('elfinder.xml');
$loader->load('form.xml');
$loader->load('command.xml');
$container->setParameter('fm_elfinder', $config);

$repo = $container->getDefinition(ElFinderController::class);
$repo->replaceArgument(1, $config);
$container->setAlias('fm_elfinder.configurator', $config['configuration_provider']);
$container->setAlias('fm_elfinder.loader', $config['loader']);
$container->getAlias('fm_elfinder.loader')->setPublic(true);
Expand Down

0 comments on commit 43c785d

Please sign in to comment.