Skip to content

Commit

Permalink
Merge pull request #9153 from mickaelandrieu/play-with-admin-controllers
Browse files Browse the repository at this point in the history
Be able to declare modern controllers in modules
  • Loading branch information
Quetzacoalt91 committed Jun 25, 2018
2 parents 90a34fc + 3e90278 commit 6251e07
Show file tree
Hide file tree
Showing 27 changed files with 894 additions and 38 deletions.
1 change: 1 addition & 0 deletions app/config/config.yml
Expand Up @@ -59,6 +59,7 @@ twig:
'%admin_page%/TwigTemplateForm': Twig
'%admin_page%/Configure/AdvancedParameters': AdvancedParameters
'%admin_page%/Configure/ShopParameters': ShopParameters
'%kernel.root_dir%/../modules': Modules
globals:
webpack_server: false

Expand Down
7 changes: 7 additions & 0 deletions app/config/routing.yml
@@ -1,3 +1,10 @@
app:
# The main bundle is PrestaShopCoreBundle which will load other dependencies.
resource: "@PrestaShopBundle/Resources/config/routing.yml"

app_modules:
# Declare routing.yml file in modules/module-name/config folder.
# v1: only YAML format is supported for now.
resource: .
type: module
prefix: /modules
6 changes: 3 additions & 3 deletions src/PrestaShopBundle/Resources/config/services.yml
@@ -1,5 +1,5 @@
imports:
- { resource: services/bundle_services.yml }
- { resource: services/core_services.yml }
- { resource: services/adapter_services.yml }
- { resource: services/bundle/*.yml }
- { resource: services/core/*.yml }
- { resource: services/adapter/*.yml }
- { resource: services/alias.yml }

This file was deleted.

@@ -0,0 +1,6 @@
services:
prestashop.bundle.routing.module_route_loader:
class: 'PrestaShopBundle\Routing\YamlModuleLoader'
arguments:
- '@=service("prestashop.module_kernel.repository").getActiveModulesPaths()'
tags: [routing.loader]
13 changes: 0 additions & 13 deletions src/PrestaShopBundle/Resources/config/services/bundle_services.yml

This file was deleted.

10 changes: 0 additions & 10 deletions src/PrestaShopBundle/Resources/config/services/core_services.yml

This file was deleted.

84 changes: 84 additions & 0 deletions src/PrestaShopBundle/Routing/YamlModuleLoader.php
@@ -0,0 +1,84 @@
<?php
/**
* 2007-2018 PrestaShop
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/OSL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
* @author PrestaShop SA <contact@prestashop.com>
* @copyright 2007-2018 PrestaShop SA
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
namespace PrestaShopBundle\Routing;

use Symfony\Component\Config\Loader\Loader;
use Symfony\Component\Routing\RouteCollection;
use RuntimeException;

/**
* This class is responsible of loading routes of enabled modules.
*/
class YamlModuleLoader extends Loader
{
/**
* @var array $activeModules The list of activated modules.
*/
private $activeModulesPaths;

/**
* @var bool $isLoaded We load the route collection only once per request.
*/
private $isLoaded = false;

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

/**
* {@inheritdoc}
*/
public function load($resource, $type = null)
{
if (true === $this->isLoaded) {
throw new RuntimeException('Do not add the "module" loader twice.');
}

$routes = new RouteCollection();

foreach ($this->activeModulesPaths as $modulePath) {
$routingFile = $modulePath.'/config/routes.yml';
if (file_exists($routingFile)) {
$loadedRoutes = $this->import($routingFile, 'yaml');

$routes->addCollection($loadedRoutes);
}
}

$this->isLoaded = true;

return $routes;
}

/**
* {@inheritdoc}
*/
public function supports($resource, $type = null)
{
return 'module' === $type;
}
}
Expand Up @@ -57,8 +57,8 @@ protected function setUp()
$this->moduleManager = $this->moduleManagerBuilder->build();

$this->moduleNames= [
'pscsx3241',
'pscsx32412',
'pscsx3241',
'pscsx32412',
];
}

Expand Down
85 changes: 85 additions & 0 deletions tests/PrestaShopBundle/Routing/YamlRoutesInModuleTest.php
@@ -0,0 +1,85 @@
<?php
/**
* 2007-2018 PrestaShop
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/OSL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
* @author PrestaShop SA <contact@prestashop.com>
* @copyright 2007-2018 PrestaShop SA
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/

namespace Tests\PrestaShopBundle\Routing;

use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\Routing\Route;
use Symfony\Component\DependencyInjection\Container;
use Tests\TestCase\Module as HelperModule;


class YamlRoutesInModuleTest extends KernelTestCase
{
/**
* @var Container
*/
private $container;

/**
* @var Module
*/
private $module;

/**
* {@inheritdoc}
*/
public function setUp()
{
$kernel = self::createKernel();
$kernel->boot();

$this->container = $kernel->getContainer();

HelperModule::addModule('demo');

$this->module = $this->container->get('prestashop.core.admin.module.repository')->getModule('demo');
$this->module->onInstall();
$this->container->get('prestashop.adapter.cache_clearer')->clearAllCaches();
}

/**
* {@inheritdoc}
*/
public function tearDown()
{
HelperModule::removeModule('demo');
$this->module->onUninstall();
}

public function testRoutesAreRegistered()
{
$router = $this->container->get('router');
$route = $router->getRouteCollection()->get('demo_admin_demo');

self::assertInstanceOf(Route::class, $route);

self::assertEquals('/modules/demo/demo', $route->getPath());
self::assertEquals([
'_controller' => 'PsTest\Controller\Admin\DemoController::demoAction'
], $route->getDefaults());
}
}
3 changes: 3 additions & 0 deletions tests/resources/module/demo/README.md
@@ -0,0 +1,3 @@
# Demo module

Demo module, files in vendor folder automatically generated by Composer.
13 changes: 13 additions & 0 deletions tests/resources/module/demo/composer.json
@@ -0,0 +1,13 @@
{
"name": "prestashop/ademo",
"description": "A module for testing purposes",
"autoload": {
"psr-4": {
"PsTest\\": "src/"
},
"exclude-from-classmap": []
},
"license": "MIT",
"type": "prestashop-module",
"require": {}
}
12 changes: 12 additions & 0 deletions tests/resources/module/demo/config.xml
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8" ?>
<module>
<name>demo</name>
<displayName><![CDATA[Module demo]]></displayName>
<version><![CDATA[1]]></version>
<description><![CDATA[A module to test Module routing feature]]></description>
<author><![CDATA[demo]]></author>
<tab><![CDATA[core_feature]]></tab>
<is_configurable>0</is_configurable>
<need_instance>0</need_instance>
<limited_countries></limited_countries>
</module>
5 changes: 5 additions & 0 deletions tests/resources/module/demo/config/routes.yml
@@ -0,0 +1,5 @@
demo_admin_demo:
path: demo/demo
methods: [GET]
defaults:
_controller: 'PsTest\Controller\Admin\DemoController::demoAction'
44 changes: 44 additions & 0 deletions tests/resources/module/demo/demo.php
@@ -0,0 +1,44 @@
<?php
/**
* 2007-2018 PrestaShop
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/OSL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
* @author PrestaShop SA <contact@prestashop.com>
* @copyright 2007-2018 PrestaShop SA
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/

if (!defined('_PS_VERSION_')) {
exit;
}

class demo extends Module
{
public function __construct()
{
$this->name = 'demo';
$this->tab = 'front_office_features';
$this->version = 1.0;
$this->author = 'Šarūnas Jonušas';
$this->need_instance = 0;
parent::__construct();
$this->displayName = 'Awesome module';
$this->description = 'A module to test Module modern routing';
}
}
Binary file added tests/resources/module/demo/logo.gif
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@@ -0,0 +1,37 @@
<?php
/**
* 2007-2018 PrestaShop
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/OSL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
* @author PrestaShop SA <contact@prestashop.com>
* @copyright 2007-2018 PrestaShop SA
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/

namespace PsTest\Controller\Admin;

use PrestaShopBundle\Controller\Admin\FrameworkBundleAdminController;

class DemoController extends FrameworkBundleAdminController
{
public function demoAction()
{
return $this->render('@Modules/demo/templates/admin/demo.html.twig');
}
}
7 changes: 7 additions & 0 deletions tests/resources/module/demo/templates/admin/demo.html.twig
@@ -0,0 +1,7 @@
{% extends '@PrestaShop/Admin/layout.html.twig' %}

{% block content %}
<h1>Modern module Controller</h1>

<p>Awesome !</p>
{% endblock %}
7 changes: 7 additions & 0 deletions tests/resources/module/demo/vendor/autoload.php
@@ -0,0 +1,7 @@
<?php

// autoload.php @generated by Composer

require_once __DIR__ . '/composer/autoload_real.php';

return ComposerAutoloaderInit77be78716c0a12958e9a5ba226453e15::getLoader();

0 comments on commit 6251e07

Please sign in to comment.