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

Allow recursive configurable services #2523

Draft
wants to merge 3 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
58 changes: 19 additions & 39 deletions install/class.Setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use oat\oatbox\service\ConfigurableService;
use oat\oatbox\service\ServiceManager;
use Zend\ServiceManager\ServiceLocatorAwareInterface;
use oat\tao\install\utils\seed\SeedParser;

class tao_install_Setup implements Action
{
Expand Down Expand Up @@ -71,6 +72,8 @@ public function __invoke($params)
}

$filePath = $params[0];
$parser = new SeedParser();
$seed = $parser->fromFile($filePath);

if (!file_exists($filePath)) {
throw new \ErrorException('Unable to find ' . $filePath);
Expand Down Expand Up @@ -213,35 +216,20 @@ public function __invoke($params)
}

$serviceManager = $installator->getServiceManager();

if (!isset($parameters['configuration']['generis']['persistences'])) {
throw new InvalidArgumentException('Your config should have a \'persistence\' key under \'generis\'');
}
$persistences = $parameters['configuration']['generis']['persistences'];
if (isset($persistences['default'])) {
$parameters['configuration']['generis']['persistences'] = $this->wrapPersistenceConfig($persistences);
} elseif (!isset($persistences['type'])) {
throw new InvalidArgumentException('Your config should have a \'default\' key under \'persistences\'');
foreach($seed->getServices() as $serviceId => $service) {
$serviceManager->register($serviceId, $service);
}

foreach ($parameters['configuration'] as $extension => $configs) {
foreach ($configs as $key => $config) {
if (isset($config['type']) && $config['type'] === 'configurableService') {
$className = $config['class'];
$params = $config['options'];
if (is_a($className, \oat\oatbox\service\ConfigurableService::class, true)) {
if (is_a($className, \oat\tao\model\service\InjectionAwareService::class, true)) {
$service = new $className(...$this->prepareParameters($className, $params, $serviceManager));
} else {
$service = new $className($params);
}
$serviceManager->register($extension . '/' . $key, $service);
} else {
$this->logWarning('The class : ' . $className . ' can not be set as a Configurable Service');
$this->logWarning('Make sure your configuration is correct and all required libraries are installed');
}
}
if (!$serviceManager->has(PersistenceManager::SERVICE_ID)) {
if (!isset($parameters['configuration']['generis']['persistences'])) {
throw new InvalidArgumentException('Your config should have a \'persistence\' key under \'generis\'');
}
$persistences = $parameters['configuration']['generis']['persistences'];
if (!isset($persistences['default'])) {
throw new InvalidArgumentException('Your config should have a \'default\' key under \'persistences\'');
}
$persistenceManager = $this->wrapPersistenceConfig($persistences);
$serviceManager->register(PersistenceManager::SERVICE_ID, $persistenceManager);
}

// mod rewrite cannot be detected in CLI Mode.
Expand Down Expand Up @@ -346,23 +334,15 @@ private function resolveParameter(ReflectionParameter $parameter, $paramValue, S
* Transforms the seed persistence configuration into command line parameters
* and then back into a persistence configuration to ensure backwards compatibility
* with the previous process
* @param array $persistences
* @return array
*/
private function wrapPersistenceConfig($persistences)
private function wrapPersistenceConfig(array $persistences): PersistenceManager
{
$installParams = $this->getCommandLineParameters($persistences['default']);

$dbalConfigCreator = new tao_install_utils_DbalConfigCreator();
$persistences['default'] = $dbalConfigCreator->createDbalConfig($installParams);

return [
'type' => 'configurableService',
'class' => PersistenceManager::class,
'options' => [
'persistences' => $persistences,
],
];
$persistenceConfig = $dbalConfigCreator->createDbalConfig($installParams);
$persistenceManager = new PersistenceManager();
$persistenceManager->registerPersistence('default', $persistenceConfig);
return $persistenceManager;
}

private function getCommandLineParameters(array $defaultPersistenceConfig): array
Expand Down
25 changes: 25 additions & 0 deletions install/utils/seed/InvalidSeedException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

/*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; under version 2
* of the License (non-upgradable).
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Copyright (c) 2019 (original work) Open Assessment Technologies SA;
*
*/
namespace oat\tao\install\utils\seed;

class InvalidSeedException extends \Exception
{
}
104 changes: 104 additions & 0 deletions install/utils/seed/Seed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php

/*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; under version 2
* of the License (non-upgradable).
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Copyright (c) 2019 (original work) Open Assessment Technologies SA;
*
*/
namespace oat\tao\install\utils\seed;

use oat\generis\model\GenerisRdf;

class Seed
{
private $extensionsToInstall;
private $services;
private $options;
private $userData;
private $postInstallScripts = [];

public function __construct(SeedOptions $options, $extensions, $services, $user, $postInstallScripts = [])
{
$this->options = $options;
$this->extensionsToInstall = $extensions;
$this->services = $services;
$this->userData = $user;
$this->postInstallScripts = $postInstallScripts;
}

public function getRootUrl(): string
{
return $this->options->getRootUrl();
}

public function getLocalNamespace(): string
{
return $this->options->getLocalNamespace();
}

/**
* @return string language code, by default 'en-US'
*/
public function getDefaultLanguage(): string
{
return $this->options->getDefaultLanguage();
}

public function getDefaultTimezone(): string
{
return $this->options->getDefaultTimezone();
}

public function getInstanceName(): string
{
return $this->options->getInstanceName();
}

public function useDebugMode(): bool
{
return $this->options->useDebugMode();
}

public function installSamples(): bool
{
return $this->options->installSamples();
}

public function getExtensionsToInstall(): array
{
return $this->extensionsToInstall;
}

public function getUserProperties()
{
return [
GenerisRdf::PROPERTY_USER_FIRSTNAME,
GenerisRdf::PROPERTY_USER_LASTNAME,
GenerisRdf::PROPERTY_USER_LOGIN,
GenerisRdf::PROPERTY_USER_PASSWORD
];
}

public function getServices(): array
{
return $this->services;
}

public function getPostInstallScripts(): array
{
return $this->postInstallScripts;
}
}
86 changes: 86 additions & 0 deletions install/utils/seed/SeedOptions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php

/*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; under version 2
* of the License (non-upgradable).
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Copyright (c) 2019 (original work) Open Assessment Technologies SA;
*
*/
namespace oat\tao\install\utils\seed;

class SeedOptions
{
private $rootUrl;
private $localNamespace;
private $options;

const OPTION_LANGUAGE = 'lang';
const OPTION_TIMEZONE = 'tz';
const OPTION_INSTANCE_NAME = 'name';
const OPTION_DEBUG = 'debug';
const OPTION_INSTALL_SAMPLES = 'samples';

public function __construct($rootUrl, $localNamespace, $options = [])
{
$this->rootUrl = $rootUrl;
$this->localNamespace = $localNamespace;
$this->options = $options;
}

public function getRootUrl(): string
{
return $this->rootUrl;
}

public function getLocalNamespace(): string
{
return $this->localNamespace;
}

public function getDefaultLanguage(): string
{
return isset($this->options[self::OPTION_LANGUAGE])
? $this->options[self::OPTION_LANGUAGE]
: 'en-US';
}

public function getDefaultTimezone(): string
{
return isset($this->options[self::OPTION_TIMEZONE])
? $this->options[self::OPTION_TIMEZONE]
: date_default_timezone_get();
}

public function getInstanceName(): string
{
return isset($this->options[self::OPTION_INSTANCE_NAME])
? $this->options[self::OPTION_INSTANCE_NAME]
: null;
}

public function useDebugMode(): bool
{
return isset($this->options[self::OPTION_DEBUG])
? $this->options[self::OPTION_DEBUG]
: true;
}

public function installSamples(): bool
{
return isset($this->options[self::OPTION_INSTALL_SAMPLES])
? $this->options[self::OPTION_INSTALL_SAMPLES]
: true;
}
}
Loading