Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make modules act like Symfony Bundles #8342

Merged
merged 3 commits into from Nov 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions .travis.yml
Expand Up @@ -39,13 +39,16 @@ before_install:
# PrestaShop configuration
- cp tests/parameters.yml.travis app/config/parameters.yml


notifications:
hipchat: ec4e21c5eb82066ba8be5fd1afefde@1184657

script:
- composer install --prefer-dist --no-interaction --no-progress
- bash tests/check_file_syntax.sh
- bash travis-scripts/install-prestashop
- php app/console lint:twig src
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Twig is dependant from database now, since we loop into every "enabled" module templates, if any :)

- php app/console lint:twig app
- composer test
- composer test-admin
- composer phpunit-sf
Expand Down
100 changes: 99 additions & 1 deletion app/AppKernel.php
Expand Up @@ -24,8 +24,11 @@
* International Registered Trademark & Property of PrestaShop SA
*/


use Doctrine\DBAL\Configuration;
use Doctrine\DBAL\DriverManager;
use Symfony\Component\Yaml\Yaml;
use Symfony\Component\HttpKernel\Kernel;
use PrestaShopBundle\Kernel\ModuleRepository;
use Symfony\Component\Config\Loader\LoaderInterface;

class AppKernel extends Kernel
Expand Down Expand Up @@ -54,6 +57,9 @@ public function registerBundles()
$bundles[] = new Sensio\Bundle\DistributionBundle\SensioDistributionBundle();
}

/**
* @see https://symfony.com/doc/2.8/configuration/external_parameters.html#environment-variables
*/
if (extension_loaded('apc')) {
$_SERVER['SYMFONY__CACHE__DRIVER'] = 'apc';
} else {
Expand All @@ -63,8 +69,100 @@ public function registerBundles()
return $bundles;
}

/**
* @{inheritdoc}
*/
protected function getKernelParameters()
{
$kernelParameters = parent::getKernelParameters();

$activeModules = array();

if ($this->parametersFileExists()) {
try {
$this->getConnection()->connect();
$activeModules = $this->getActiveModules();
} catch (\Exception $e) {
}
}


return array_merge(
$kernelParameters,
array('kernel.active_modules' => $activeModules)
);
}

/**
* @{inheritdoc}
*/
public function registerContainerConfiguration(LoaderInterface $loader)
{
$loader->load($this->getRootDir().'/config/config_'.$this->getEnvironment().'.yml');
}

/**
* Return all active modules.
*
* @return array list of modules names.
*/
private function getActiveModules()
{
$databasePrefix = $this->getParameters()['database_prefix'];

$modulesRepository = new ModuleRepository(
$this->getConnection(),
$databasePrefix
);

return $modulesRepository->getActiveModules();
}

/**
* @return array The root parameters of PrestaShop
*/
private function getParameters()
{
if ($this->parametersFileExists()) {
$config = require($this->getParametersFile());

return $config['parameters'];
}

return array();
}

/**
* @var bool
*/
private function parametersFileExists()
{
return file_exists($this->getParametersFile());
}

/**
* @return string filepath to PrestaShop configuration parameters
*/
private function getParametersFile()
{
return $this->getRootDir().'/config/parameters.php';
}

/**
* @return \Doctrine\DBAL\Connection
*/
private function getConnection()
{
$parameters = $this->getParameters();

return DriverManager::getConnection(array(
'dbname' => $parameters['database_name'],
'user' => $parameters['database_user'],
'password' => $parameters['database_password'],
'host' => $parameters['database_host'],
'port' => $parameters['database_port'],
'charset' => 'utf8',
'driver' => 'pdo_mysql',
));
}
}
24 changes: 24 additions & 0 deletions classes/module/Module.php
Expand Up @@ -29,6 +29,7 @@
use PrestaShop\PrestaShop\Core\Module\WidgetInterface;
use PrestaShop\PrestaShop\Adapter\ServiceLocator;
use PrestaShop\PrestaShop\Core\Module\ModuleInterface;
use PrestaShop\PrestaShop\Adapter\SymfonyContainer;

abstract class ModuleCore implements ModuleInterface
{
Expand Down Expand Up @@ -179,6 +180,9 @@ abstract class ModuleCore implements ModuleInterface
/** @var bool Random session for modules perfs logs*/
public static $_log_modules_perfs_session = null;

/** @var \Symfony\Component\DependencyInjection\ContainerInterface */
private $container;

const CACHE_FILE_MODULES_LIST = '/config/xml/modules_list.xml';

const CACHE_FILE_TAB_MODULES_LIST = '/config/xml/tab_modules_list.xml';
Expand Down Expand Up @@ -3187,6 +3191,26 @@ public function isSymfonyContext()
{
return !defined('ADMIN_LEGACY_CONTEXT');
}

/**
* Access the Symfony Container if we are in Symfony Context.
* Note: in this case, we must get a container from SymfonyContainer class.
* @param string $serviceName
*
* @return Object|false if Symfony is not booted, it returns false.
*/
public function get($serviceName)
{
if ($this->isSymfonyContext()) {
if (is_null($this->container)) {
$this->container = SymfonyContainer::getInstance();
}

return $this->container->get($serviceName);
}

return false;
}
}

function ps_module_version_sort($a, $b)
Expand Down
1 change: 1 addition & 0 deletions controllers/admin/AdminLegacyLayoutController.php
Expand Up @@ -49,6 +49,7 @@ public function __construct($controllerName = '', $title = '', $headerToolbarBtn
$this->headerTabContent = $headerTabContent;
$this->enableSidebar = $enableSidebar;
$this->helpLink = $helpLink;
$this->php_self = $controllerName;
}

public function setMedia()
Expand Down
19 changes: 19 additions & 0 deletions src/Core/Addon/Module/ModuleRepository.php
Expand Up @@ -540,4 +540,23 @@ public function getInstalledModules()

return $this->getFilteredList($filters);
}

/**
* Returns installed module filepaths
* @return array
*/
public function getInstalledModulesPaths()
{
$paths = array();
$modulesFiles = Finder::create()->directories()->in(__DIR__.'/../../../../modules')->depth(0);
$installedModules = array_keys($this->getInstalledModules());

foreach ($modulesFiles as $moduleFile) {
if (in_array($moduleFile->getFilename(), $installedModules)) {
$paths[] = $moduleFile->getPathname();
}
}

return $paths;
}
}
6 changes: 3 additions & 3 deletions src/PrestaShopBundle/Controller/Admin/CommonController.php
Expand Up @@ -53,7 +53,7 @@ class CommonController extends FrameworkBundleAdminController
* {% render controller('PrestaShopBundle\\Controller\\Admin\\CommonController::paginationAction',
* {'limit': limit, 'offset': offset, 'total': product_count, 'caller_parameters': pagination_parameters}) %}
*
* @Template
* @Template("@PrestaShop/Admin/Common/pagination.html.twig")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can only override templates using "arobased" notation. This notation is the recommended one in Symfony best practices docs.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no you are able to overwrite all templates, but if you are allowed for breaking changes just do it in favor of best practices

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello Daniel, I've tried with the alternative syntax and it doesn't work Twig is unable to find the overriden template.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for a clean overwrite you should implemented some some custom Twig loader / resolver as mentioned before about the activeModules.

another solution would be to load active without container access. its also done by Shopware https://github.com/shopware/shopware/blob/5.3/engine/Shopware/Kernel.php#L452

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, for the templates I'm good thanks to a custom Twig loader as you suggested (thanks again). I'm looking at Shopware code :)

edit: what do you think @Quetzacoalt91?

* @param Request $request
* @param integer $limit
* @param integer $offset
Expand Down Expand Up @@ -149,7 +149,7 @@ public function paginationAction(Request $request, $limit = 10, $offset = 0, $to
/**
* This will allow you to retrieve an HTML code with a list of recommended modules depending on the domain.
*
* @Template
* @Template("@PrestaShop/Admin/Common/recommendedModules.html.twig")
* @param string $domain
* @param integer $limit
* @param integer $randomize
Expand Down Expand Up @@ -200,7 +200,7 @@ public function renderSidebarAction($url, $title = '', $footer = '')
{
$tools = $this->container->get('prestashop.adapter.tools');

return $this->render('PrestaShopBundle:Admin:Common/_partials/_sidebar.html.twig', [
return $this->render('@PrestaShop/Admin/Common/_partials/_sidebar.html.twig', [
'footer' => $tools->purifyHTML($footer),
'title' => $title,
'url' => urldecode($url),
Expand Down
6 changes: 3 additions & 3 deletions src/PrestaShopBundle/Controller/Admin/ProductController.php
Expand Up @@ -75,7 +75,7 @@ class ProductController extends FrameworkBundleAdminController
*
* URL example: /product/catalog/40/20/id_product/asc
*
* @Template
* @Template("@PrestaShop/Admin/Product/catalog.html.twig")
* @param Request $request
* @param integer $limit The size of the listing
* @param integer $offset The offset of the listing
Expand Down Expand Up @@ -250,7 +250,7 @@ public function catalogAction(Request $request, $limit = 10, $offset = 0, $order
* The full page that shows products list will subcall this action (from catalogAction).
* URL example: /product/list/html/40/20/id_product/asc
*
* @Template
* @Template("@PrestaShop/Admin/Product/list.html.twig")
* @param Request $request
* @param integer $limit The size of the listing
* @param integer $offset The offset of the listing
Expand Down Expand Up @@ -374,7 +374,7 @@ public function newAction()
/**
* Product form
*
* @Template
* @Template("@PrestaShop/Admin/Product/form.html.twig")
* @param int $id The product ID
* @param Request $request
* @return array|Response Template vars
Expand Down
Expand Up @@ -110,7 +110,7 @@ public function updateImagePositionAction(Request $request)
/**
* Manage form image
*
* @Template
* @Template("@PrestaShop/Admin/ProductImage/form.html.twig")
* @param $idImage
* @param Request $request
* @return array|JsonResponse|Response
Expand Down
@@ -0,0 +1,75 @@
<?php
/**
* 2007-2017 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-2017 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\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
use Symfony\Component\Config\Resource\FileExistenceResource;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Config\FileLocator;

/**
* Load services stored in installed modules.
*/
class LoadServicesFromModulesPass implements CompilerPassInterface
{
/**
* {@inheritdoc}
*/
public function process(ContainerBuilder $container)
{
$this->registerServicesFromModules($container);
}

/**
* Load all services registered in every module.
*
* @param ContainerBuilder $container
* @return void
*/
private function registerServicesFromModules(ContainerBuilder $container)
{
$installedModules = $container->getParameter('kernel.active_modules');

foreach ($this->getModulesPaths() as $modulePath) {
if (in_array($modulePath->getFilename(), $installedModules)
&& file_exists($modulePath.'/config/services.yml')
) {
$loader = new YamlFileLoader($container, new FileLocator($modulePath.'/config/'));
$loader->load('services.yml');
}
}
}

/**
* @return \Iterator
*/
private function getModulesPaths()
{
return Finder::create()->directories()->in(__DIR__.'/../../../../modules')->depth(0);
}
}