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

Feature/service container #756

Draft
wants to merge 7 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 39 additions & 1 deletion common/oatbox/service/ServiceManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@

namespace oat\oatbox\service;

use Exception;
use oat\oatbox\Configurable;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException as SymfomyServiceNotFoundException;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
use Zend\ServiceManager\ServiceLocatorInterface;
use Zend\ServiceManager\ServiceLocatorAwareInterface;
use Psr\Container\ContainerInterface;
Expand All @@ -33,6 +38,10 @@
class ServiceManager implements ServiceLocatorInterface, ContainerInterface
{
private static $instance;
/**
* @var ContainerBuilder
*/
private $symfonyContainer;

/**
* @return \oat\oatbox\service\ServiceManager
Expand All @@ -43,9 +52,32 @@ public static function getServiceManager()
if (is_null(self::$instance)) {
self::$instance = new ServiceManager(\common_ext_ConfigDriver::singleton());
}

return self::$instance;
}

/**
* @throws Exception
*/
private function loadSymfonyConfig()
{
$this->symfonyContainer = new ContainerBuilder();

$loader = new YamlFileLoader($this->symfonyContainer, new FileLocator(CONFIG_PATH));
try {
$configFiles = $this->get('generis/serviceContainers')->getOptions();
} catch (Exception $e) {
$configFiles = [];
}

// just to make it faster. probably container extension is a better way
foreach ($configFiles as $configFile) {
$loader->load($configFile);
}
$this->symfonyContainer->set('serviceLocator', $this);
$this->symfonyContainer->compile();
}

public static function setServiceManager(ServiceManager $serviceManager)
{
self::$instance = $serviceManager;
Expand All @@ -61,6 +93,8 @@ public static function setServiceManager(ServiceManager $serviceManager)
public function __construct($configService)
{
$this->configService = $configService;

$this->loadSymfonyConfig();
}

/**
Expand All @@ -76,7 +110,11 @@ public function get($serviceKey)
{
$serviceId = $this->getServiceId($serviceKey);
if (!isset($this->services[$serviceId])) {
$this->services[$serviceId] = $this->load($serviceId, $serviceKey);
try {
$this->services[$serviceId] = $this->symfonyContainer->get($serviceKey);
} catch (SymfomyServiceNotFoundException $e) {
$this->services[$serviceId] = $this->load($serviceId, $serviceKey);
}
}
return $this->services[$serviceId];
}
Expand Down
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,10 @@
"fluent/logger": "^1.0.1",
"symfony/lock": "^3.4",
"psr/container": "^1.0.0",
"ramsey/uuid": "^3.8"
"ramsey/uuid": "^3.8",
"symfony/dependency-injection": "^5.1@dev",
"symfony/yaml": "^5.1@dev",
"symfony/config": "^5.1@dev"
},
"require-dev": {
"mikey179/vfsstream": "1.4.0",
Expand Down
5 changes: 5 additions & 0 deletions config/default/serviceContainers.conf.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

return new oat\oatbox\config\ConfigurationService(array(
'generis' => 'generis/services.yaml',
));
5 changes: 5 additions & 0 deletions config/default/services.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
services:
_defaults:
autowire: true
autoconfigure: true
public: false
2 changes: 1 addition & 1 deletion manifest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
'label' => 'Generis Core',
'description' => 'Core extension, provide the low level framework and an API to manage ontologies',
'license' => 'GPL-2.0',
'version' => '12.15.0',
'version' => '12.16.0',
'author' => 'Open Assessment Technologies, CRP Henri Tudor',
'requires' => [],
'models' => [
Expand Down
12 changes: 12 additions & 0 deletions scripts/update/Updater.php
Original file line number Diff line number Diff line change
Expand Up @@ -494,5 +494,17 @@ public function update($initialVersion)
}

$this->skip('12.12.0', '12.15.0');

if ($this->isVersion('12.15.0')) {
$serviceManager = $this->getServiceManager();
$containersConfig = 'generis/serviceContainers';
$containers = new ConfigurationService(['generis' => 'generis/services.yaml']);

copy(__DIR__ . '/../../config/default/services.yaml', CONFIG_PATH . 'generis/services.yaml');

$serviceManager->register($containersConfig, $containers);

$this->setVersion('12.16.0');
}
}
}