Skip to content

Commit

Permalink
Adds shop aware services for modules
Browse files Browse the repository at this point in the history
  • Loading branch information
mtkoltan committed Nov 8, 2018
1 parent ba13c6b commit 6956418
Show file tree
Hide file tree
Showing 32 changed files with 1,621 additions and 18 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Expand Up @@ -39,3 +39,7 @@

# DI cache
/source/Internal/Application/containercache.php

# DI for modules and packages
/source/project.yaml
/tests/Integration/Internal/ProjectDIConfig/Dao/project.yaml
26 changes: 26 additions & 0 deletions source/Core/Module/ModuleInstaller.php
Expand Up @@ -17,6 +17,9 @@
use OxidEsales\Eshop\Core\Module\ModuleVariablesLocator as EshopModuleVariablesLocator;
use OxidEsales\Eshop\Core\Module\Module as EshopModule;
use OxidEsales\Eshop\Core\Module\ModuleSmartyPluginDirectoryValidator as EshopModuleSmartyPluginDirectoryValidator;
use OxidEsales\EshopCommunity\Internal\Application\ContainerFactory;
use OxidEsales\EshopCommunity\Internal\ProjectDIConfig\Service\ShopActivationServiceInterface;
use OxidEsales\Facts\Facts;

/**
* Modules installer class.
Expand Down Expand Up @@ -122,6 +125,8 @@ public function activate(\OxidEsales\Eshop\Core\Module\Module $module)
}
}

$this->activateShopAwareServices($module);

$this->resetCache();

$this->_callEvent('onActivate', $moduleId);
Expand All @@ -132,6 +137,26 @@ public function activate(\OxidEsales\Eshop\Core\Module\Module $module)
return $result;
}

/**
* @param EshopModule $module
*/
private function activateShopAwareServices(\OxidEsales\Eshop\Core\Module\Module $module)
{
/** @var ShopActivationServiceInterface $shopActivationService */
$shopActivationService = ContainerFactory::getInstance()->getContainer()->get(ShopActivationServiceInterface::class);
$shopActivationService->activateServicesForShops($module->getModuleFullPath(), [Registry::getConfig()->getShopId()]);
}

/**
* @param EshopModule $module
*/
private function deactivateShopAwareServices(\OxidEsales\Eshop\Core\Module\Module $module)
{
/** @var ShopActivationServiceInterface $shopActivationService */
$shopActivationService = ContainerFactory::getInstance()->getContainer()->get(ShopActivationServiceInterface::class);
$shopActivationService->deactivateServicesForShops($module->getModuleFullPath(), [Registry::getConfig()->getShopId()]);
}

/**
* Deactivate extension by adding disable module class information to disabled module array
*
Expand All @@ -156,6 +181,7 @@ public function deactivate(\OxidEsales\Eshop\Core\Module\Module $module)
$this->deleteModuleControllers($moduleId);
$this->deleteModuleSmartyPluginDirectories($moduleId);

$this->deactivateShopAwareServices($module);

$this->resetCache();

Expand Down
29 changes: 23 additions & 6 deletions source/Internal/Application/ContainerBuilder.php
Expand Up @@ -9,6 +9,10 @@
namespace OxidEsales\EshopCommunity\Internal\Application;

use OxidEsales\EshopCommunity\Core\Registry;
use OxidEsales\EshopCommunity\Internal\ProjectDIConfig\Dao\ProjectYamlDao;
use OxidEsales\EshopCommunity\Internal\ProjectDIConfig\Dao\ProjectYamlDaoInterface;
use OxidEsales\EshopCommunity\Internal\ProjectDIConfig\Service\ProjectYamlImportService;
use OxidEsales\EshopCommunity\Internal\Utility\Context;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
Expand Down Expand Up @@ -52,20 +56,33 @@ private function loadServiceFiles(SymfonyContainerBuilder $symfonyContainer)
}

/**
* Loads a 'project.yaml' file if it can be found in the shop directory
* Loads a 'project.yaml' file if it can be found in the shop directory.
*
* @param SymfonyContainerBuilder $symfonyContainer
*
* @return void
*/
private function loadProjectServices(SymfonyContainerBuilder $symfonyContainer)
{

try {
$loader = new YamlFileLoader($symfonyContainer, new FileLocator($this->getShopSourcePath()));
$loader->load('project.yaml');
} catch (\Exception $e) {
// pass
if (! file_exists($this->getShopSourcePath() .
DIRECTORY_SEPARATOR . ProjectYamlDaoInterface::PROJECT_FILE_NAME)) {
return;
}
$this->cleanupProjectYaml();
$loader = new YamlFileLoader($symfonyContainer, new FileLocator($this->getShopSourcePath()));
$loader->load(ProjectYamlDaoInterface::PROJECT_FILE_NAME);
}

/**
* Removes imports from modules that have deleted on the file system.
*/
private function cleanupProjectYaml()
{
$context = new Context(Registry::getConfig());
$projectYamlDao = new ProjectYamlDao($context);
$yamlImportService = new ProjectYamlImportService($projectYamlDao);
$yamlImportService->removeNonExistingImports();
}

/**
Expand Down
6 changes: 3 additions & 3 deletions source/Internal/Application/ContainerFactory.php
Expand Up @@ -9,6 +9,7 @@
namespace OxidEsales\EshopCommunity\Internal\Application;

use OxidEsales\Eshop\Core\Registry;
use OxidEsales\EshopCommunity\Internal\Utility\Context;
use Psr\Container\ContainerInterface;
use Symfony\Component\DependencyInjection\Dumper\PhpDumper;

Expand Down Expand Up @@ -102,9 +103,8 @@ private function saveContainerToCache($cachefile)
*/
private function getCacheFilePath()
{
$compileDir = Registry::getConfig()->getConfigParam('sCompileDir');

return $compileDir . '/containercache.php';
$context = new Context(Registry::getConfig());
return $context->getContainerCacheFile();
}

/**
Expand Down
@@ -0,0 +1,19 @@
<?php declare(strict_types=1);

/**
* Copyright © OXID eSales AG. All rights reserved.
* See LICENSE file for license details.
*/

namespace OxidEsales\EshopCommunity\Internal\Application\Events;

use OxidEsales\EshopCommunity\Internal\Utility\ContextInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

/**
* Class ShopAwareEventSubscriber
*/
abstract class AbstractShopAwareEventSubscriber implements EventSubscriberInterface, ShopAwareInterface
{
use ShopAwareServiceTrait;
}
Expand Up @@ -29,7 +29,7 @@ protected function doDispatch($listeners, $eventName, Event $event)
}
if (is_array($listener) &&
is_object($listener[0]) &&
in_array(ShopAwareEventSubscriberInterface::class, class_implements($listener[0])) &&
in_array(ShopAwareInterface::class, class_implements($listener[0])) &&
! $listener[0]->isActive()) {
continue;
}
Expand Down
Expand Up @@ -13,7 +13,7 @@
/**
* Interface ShopAwareEventSubscriberInterface
*/
interface ShopAwareEventSubscriberInterface
interface ShopAwareInterface
{
/**
* This method is used by the DI container
Expand Down
@@ -1,5 +1,4 @@
<?php
declare(strict_types=1);
<?php declare(strict_types=1);

/**
* Copyright © OXID eSales AG. All rights reserved.
Expand All @@ -14,7 +13,7 @@
/**
* Class ShopAwareEventSubscriber
*/
abstract class ShopAwareEventSubscriber implements EventSubscriberInterface, ShopAwareEventSubscriberInterface
trait ShopAwareServiceTrait
{
/**
* @var ContextInterface
Expand Down
1 change: 1 addition & 0 deletions source/Internal/Application/services.yaml
Expand Up @@ -6,3 +6,4 @@ imports:
- { resource: ../Review/services.yaml }
- { resource: ../Form/services.yaml }
- { resource: ../Adapter/services.yaml }
- { resource: ../ProjectDIConfig/services.yaml }
77 changes: 77 additions & 0 deletions source/Internal/ProjectDIConfig/Dao/ProjectYamlDao.php
@@ -0,0 +1,77 @@
<?php declare(strict_types=1);

/**
* Copyright © OXID eSales AG. All rights reserved.
* See LICENSE file for license details.
*/

namespace OxidEsales\EshopCommunity\Internal\ProjectDIConfig\Dao;

use OxidEsales\EshopCommunity\Internal\ProjectDIConfig\DataObject\DIConfigWrapper;
use OxidEsales\EshopCommunity\Internal\Utility\ContextInterface;
use Symfony\Component\Yaml\Yaml;

/**
* @internal
*/
class ProjectYamlDao implements ProjectYamlDaoInterface
{
/**
* @var ContextInterface $context
*/
private $context;

/**
* ProjectYamlDao constructor.
*
* @param ContextInterface $context
*/
public function __construct(ContextInterface $context)
{
$this->context = $context;
}

/**
* @return DIConfigWrapper
*/
public function loadProjectConfigFile(): DIConfigWrapper
{
return $this->loadDIConfigFile($this->getProjectFileName());
}

/**
* @param DIConfigWrapper $config
*/
public function saveProjectConfigFile(DIConfigWrapper $config)
{
file_put_contents($this->getProjectFileName(), Yaml::dump($config->getConfigAsArray(), 3, 2));
if (file_exists($this->context->getContainerCacheFile())) {
unlink($this->context->getContainerCacheFile());
}
}

/**
* @param string $path
*
* @return array
*/
public function loadDIConfigFile(string $path): DIConfigWrapper
{
$yamlArray = null;
if (file_exists($path)) {
$yamlArray = Yaml::parse(file_get_contents($path));
}
if (is_null($yamlArray)) {
$yamlArray = [];
}
return new DIConfigWrapper($yamlArray);
}

/**
* @return string
*/
private function getProjectFileName(): string
{
return $this->context->getShopDir() . DIRECTORY_SEPARATOR . ProjectYamlDaoInterface::PROJECT_FILE_NAME;
}
}
36 changes: 36 additions & 0 deletions source/Internal/ProjectDIConfig/Dao/ProjectYamlDaoInterface.php
@@ -0,0 +1,36 @@
<?php declare(strict_types=1);

/**
* Copyright © OXID eSales AG. All rights reserved.
* See LICENSE file for license details.
*/

namespace OxidEsales\EshopCommunity\Internal\ProjectDIConfig\Dao;

use OxidEsales\EshopCommunity\Internal\ProjectDIConfig\DataObject\DIConfigWrapper;

/**
* @internal
*/
interface ProjectYamlDaoInterface
{

const PROJECT_FILE_NAME = 'project.yaml';

/**
* @param string $path
*
* @return DIConfigWrapper
*/
public function loadDIConfigFile(string $path): DIConfigWrapper;

/**
* @return DIConfigWrapper
*/
public function loadProjectConfigFile(): DIConfigWrapper;

/**
* @param DIConfigWrapper $config
*/
public function saveProjectConfigFile(DIConfigWrapper $config);
}
78 changes: 78 additions & 0 deletions source/Internal/ProjectDIConfig/DataObject/DICallWrapper.php
@@ -0,0 +1,78 @@
<?php
declare(strict_types=1);

/**
* Copyright © OXID eSales AG. All rights reserved.
* See LICENSE file for license details.
*/

namespace OxidEsales\EshopCommunity\Internal\ProjectDIConfig\DataObject;

/**
* @internal
*/
class DICallWrapper
{

const METHOD_KEY = 'method';
const PARAMETER_KEY = 'arguments';

private $callArray;

/**
* DICallWrapper constructor.
*
* @param array $callArray
*/
public function __construct(array $callArray = [])
{
if (! $callArray) {
$this->callArray = ['method' => '', 'arguments' => []];
} else {
$this->callArray = $callArray;
}
}

/**
* @return string
*/
public function getMethodName(): string
{
return $this->callArray[$this::METHOD_KEY];
}

/**
* @param string $methodName
*/
public function setMethodName(string $methodName)
{
$this->callArray[$this::METHOD_KEY] = $methodName;
}

/**
* @param int $index
* @param mixed $parameter
*/
public function setParameter(int $index, $parameter)
{
$this->callArray[$this::PARAMETER_KEY][$index] = $parameter;
}

/**
* @param int $index
*
* @return mixed
*/
public function getParameter(int $index)
{
return $this->callArray[$this::PARAMETER_KEY][$index];
}

/**
* @return array
*/
public function getCallAsArray(): array
{
return $this->callArray;
}
}

0 comments on commit 6956418

Please sign in to comment.