Initial commit
This commit is contained in:
50
application/Espo/Core/Container/Configuration.php
Normal file
50
application/Espo/Core/Container/Configuration.php
Normal file
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM – Open Source CRM application.
|
||||
* Copyright (C) 2014-2025 EspoCRM, Inc.
|
||||
* Website: https://www.espocrm.com
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* 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 Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
* The interactive user interfaces in modified source and object code versions
|
||||
* of this program must display Appropriate Legal Notices, as required under
|
||||
* Section 5 of the GNU Affero General Public License version 3.
|
||||
*
|
||||
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
|
||||
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
|
||||
************************************************************************/
|
||||
|
||||
namespace Espo\Core\Container;
|
||||
|
||||
interface Configuration
|
||||
{
|
||||
/**
|
||||
* @return ?class-string<Loader>
|
||||
*/
|
||||
public function getLoaderClassName(string $name): ?string;
|
||||
|
||||
/**
|
||||
* @return ?class-string<object>
|
||||
*/
|
||||
public function getServiceClassName(string $name): ?string;
|
||||
|
||||
/**
|
||||
* @return ?string[]
|
||||
*/
|
||||
public function getServiceDependencyList(string $name): ?array;
|
||||
|
||||
public function isSettable(string $name): bool;
|
||||
}
|
||||
79
application/Espo/Core/Container/Container.php
Normal file
79
application/Espo/Core/Container/Container.php
Normal file
@@ -0,0 +1,79 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM – Open Source CRM application.
|
||||
* Copyright (C) 2014-2025 EspoCRM, Inc.
|
||||
* Website: https://www.espocrm.com
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* 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 Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
* The interactive user interfaces in modified source and object code versions
|
||||
* of this program must display Appropriate Legal Notices, as required under
|
||||
* Section 5 of the GNU Affero General Public License version 3.
|
||||
*
|
||||
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
|
||||
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
|
||||
************************************************************************/
|
||||
|
||||
namespace Espo\Core\Container;
|
||||
|
||||
use Espo\Core\Container\Exceptions\NotSettableException;
|
||||
use Psr\Container\ContainerInterface;
|
||||
use Psr\Container\NotFoundExceptionInterface;
|
||||
use ReflectionClass;
|
||||
|
||||
/**
|
||||
* DI container for services. Lazy initialization is used. Services are instantiated only once.
|
||||
* @see https://docs.espocrm.com/development/di/.
|
||||
*/
|
||||
interface Container extends ContainerInterface
|
||||
{
|
||||
/**
|
||||
* Obtain a service object.
|
||||
*
|
||||
* @throws NotFoundExceptionInterface If not gettable.
|
||||
*/
|
||||
public function get(string $id): object;
|
||||
|
||||
/**
|
||||
* Check whether a service can be obtained.
|
||||
*/
|
||||
public function has(string $id): bool;
|
||||
|
||||
/**
|
||||
* Set a service object. Must be configured as settable.
|
||||
*
|
||||
* @throws NotSettableException Is not settable or already set.
|
||||
*/
|
||||
public function set(string $id, object $object): void;
|
||||
|
||||
/**
|
||||
* Get a class of a service.
|
||||
*
|
||||
* @return ReflectionClass<object>
|
||||
* @throws NotFoundExceptionInterface If not gettable.
|
||||
*/
|
||||
public function getClass(string $id): ReflectionClass;
|
||||
|
||||
/**
|
||||
* Get a service by a class name. A service should be bound to a class or interface.
|
||||
*
|
||||
* @template T of object
|
||||
* @param class-string<T> $className A class name or interface name.
|
||||
* @return T A service instance.
|
||||
* @throws NotFoundExceptionInterface If not gettable.
|
||||
*/
|
||||
public function getByClass(string $className): object;
|
||||
}
|
||||
222
application/Espo/Core/Container/ContainerBuilder.php
Normal file
222
application/Espo/Core/Container/ContainerBuilder.php
Normal file
@@ -0,0 +1,222 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM – Open Source CRM application.
|
||||
* Copyright (C) 2014-2025 EspoCRM, Inc.
|
||||
* Website: https://www.espocrm.com
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* 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 Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
* The interactive user interfaces in modified source and object code versions
|
||||
* of this program must display Appropriate Legal Notices, as required under
|
||||
* Section 5 of the GNU Affero General Public License version 3.
|
||||
*
|
||||
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
|
||||
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
|
||||
************************************************************************/
|
||||
|
||||
namespace Espo\Core\Container;
|
||||
|
||||
use Espo\Core\Application\ApplicationParams;
|
||||
use Espo\Core\Container;
|
||||
use Espo\Core\Container\Container as ContainerInterface;
|
||||
|
||||
use Espo\Core\Binding\BindingContainer;
|
||||
use Espo\Core\Binding\BindingLoader;
|
||||
use Espo\Core\Binding\EspoBindingLoader;
|
||||
|
||||
use Espo\Core\Loaders\ApplicationState as ApplicationStateLoader;
|
||||
use Espo\Core\Utils\File\Manager as FileManager;
|
||||
use Espo\Core\Utils\Config\ConfigFileManager;
|
||||
use Espo\Core\Utils\Config;
|
||||
use Espo\Core\Utils\DataCache;
|
||||
use Espo\Core\Utils\Module;
|
||||
|
||||
use Espo\Core\Loaders\Log as LogLoader;
|
||||
use Espo\Core\Loaders\DataManager as DataManagerLoader;
|
||||
use Espo\Core\Loaders\Metadata as MetadataLoader;
|
||||
|
||||
/**
|
||||
* Builds a service container.
|
||||
*/
|
||||
class ContainerBuilder
|
||||
{
|
||||
/** @var class-string<ContainerInterface> */
|
||||
private string $containerClassName = Container::class;
|
||||
/** @var class-string<Configuration> */
|
||||
private string $containerConfigurationClassName = ContainerConfiguration::class;
|
||||
/** @var class-string */
|
||||
private string $configClassName = Config::class;
|
||||
/** @var class-string */
|
||||
private string $fileManagerClassName = FileManager::class;
|
||||
/** @var class-string */
|
||||
private string $dataCacheClassName = DataCache::class;
|
||||
/** @var class-string<Module> */
|
||||
private string $moduleClassName = Module::class;
|
||||
private ?BindingLoader $bindingLoader = null;
|
||||
/** @var array<string, object> */
|
||||
private $services = [];
|
||||
/** @var array<string, class-string<Loader>> */
|
||||
protected $loaderClassNames = [
|
||||
'log' => LogLoader::class,
|
||||
'dataManager' => DataManagerLoader::class,
|
||||
'metadata' => MetadataLoader::class,
|
||||
'applicationState' => ApplicationStateLoader::class,
|
||||
];
|
||||
private ?ApplicationParams $params = null;
|
||||
|
||||
public function withParams(?ApplicationParams $params): self
|
||||
{
|
||||
$this->params = $params;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function withBindingLoader(BindingLoader $bindingLoader): self
|
||||
{
|
||||
$this->bindingLoader = $bindingLoader;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, object> $services
|
||||
*/
|
||||
public function withServices(array $services): self
|
||||
{
|
||||
foreach ($services as $key => $value) {
|
||||
$this->services[$key] = $value;
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, class-string<Loader>> $classNames
|
||||
* @noinspection PhpUnused
|
||||
*/
|
||||
public function withLoaderClassNames(array $classNames): self
|
||||
{
|
||||
foreach ($classNames as $key => $value) {
|
||||
$this->loaderClassNames[$key] = $value;
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param class-string<ContainerInterface> $containerClassName
|
||||
*/
|
||||
public function withContainerClassName(string $containerClassName): self
|
||||
{
|
||||
$this->containerClassName = $containerClassName;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param class-string<Configuration> $containerConfigurationClassName
|
||||
*/
|
||||
public function withContainerConfigurationClassName(string $containerConfigurationClassName): self
|
||||
{
|
||||
$this->containerConfigurationClassName = $containerConfigurationClassName;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param class-string $configClassName
|
||||
*/
|
||||
public function withConfigClassName(string $configClassName): self
|
||||
{
|
||||
$this->configClassName = $configClassName;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param class-string $fileManagerClassName
|
||||
* @noinspection PhpUnused
|
||||
*/
|
||||
public function withFileManagerClassName(string $fileManagerClassName): self
|
||||
{
|
||||
$this->fileManagerClassName = $fileManagerClassName;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param class-string $dataCacheClassName
|
||||
* @noinspection PhpUnused
|
||||
*/
|
||||
public function withDataCacheClassName(string $dataCacheClassName): self
|
||||
{
|
||||
$this->dataCacheClassName = $dataCacheClassName;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function build(): ContainerInterface
|
||||
{
|
||||
$this->services['applicationParams'] = $this->params ?? new ApplicationParams();
|
||||
|
||||
/** @var Config $config */
|
||||
$config = $this->services['config'] ?? (
|
||||
new $this->configClassName(
|
||||
new ConfigFileManager()
|
||||
)
|
||||
);
|
||||
|
||||
/** @var FileManager $fileManager */
|
||||
$fileManager = $this->services['fileManager'] ?? (
|
||||
new $this->fileManagerClassName(
|
||||
$config->get('defaultPermissions')
|
||||
)
|
||||
);
|
||||
|
||||
/** @var DataCache $dataCache */
|
||||
$dataCache = $this->services['dataCache'] ?? (
|
||||
new $this->dataCacheClassName($fileManager)
|
||||
);
|
||||
|
||||
$useCache = $config->get('useCache') ?? false;
|
||||
|
||||
/** @var Module $module */
|
||||
$module = $this->services['module'] ?? (
|
||||
new $this->moduleClassName($fileManager, $dataCache, $useCache)
|
||||
);
|
||||
|
||||
$systemConfig = new Config\SystemConfig($config);
|
||||
|
||||
$this->services['config'] = $config;
|
||||
$this->services['fileManager'] = $fileManager;
|
||||
$this->services['dataCache'] = $dataCache;
|
||||
$this->services['module'] = $module;
|
||||
$this->services['systemConfig'] = $systemConfig;
|
||||
|
||||
$bindingLoader = $this->bindingLoader ?? (
|
||||
new EspoBindingLoader($module)
|
||||
);
|
||||
|
||||
$bindingContainer = new BindingContainer($bindingLoader->load());
|
||||
|
||||
return new $this->containerClassName(
|
||||
$this->containerConfigurationClassName,
|
||||
$bindingContainer,
|
||||
$this->loaderClassNames,
|
||||
$this->services
|
||||
);
|
||||
}
|
||||
}
|
||||
110
application/Espo/Core/Container/ContainerConfiguration.php
Normal file
110
application/Espo/Core/Container/ContainerConfiguration.php
Normal file
@@ -0,0 +1,110 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM – Open Source CRM application.
|
||||
* Copyright (C) 2014-2025 EspoCRM, Inc.
|
||||
* Website: https://www.espocrm.com
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* 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 Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
* The interactive user interfaces in modified source and object code versions
|
||||
* of this program must display Appropriate Legal Notices, as required under
|
||||
* Section 5 of the GNU Affero General Public License version 3.
|
||||
*
|
||||
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
|
||||
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
|
||||
************************************************************************/
|
||||
|
||||
namespace Espo\Core\Container;
|
||||
|
||||
use Espo\Core\Utils\Log;
|
||||
use Espo\Core\Utils\Metadata;
|
||||
|
||||
use ReflectionClass;
|
||||
use Exception;
|
||||
|
||||
class ContainerConfiguration implements Configuration
|
||||
{
|
||||
/**
|
||||
* Log must be loaded before anything.
|
||||
* @noinspection PhpPropertyOnlyWrittenInspection
|
||||
* @phpstan-ignore-next-line
|
||||
*/
|
||||
private Log $log;
|
||||
|
||||
/**
|
||||
* Must be protected.
|
||||
*/
|
||||
protected Metadata $metadata;
|
||||
|
||||
public function __construct(Log $log, Metadata $metadata)
|
||||
{
|
||||
$this->log = $log;
|
||||
$this->metadata = $metadata;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return ?class-string
|
||||
*/
|
||||
public function getLoaderClassName(string $name): ?string
|
||||
{
|
||||
$className = null;
|
||||
|
||||
try {
|
||||
$className = $this->metadata->get(['app', 'containerServices', $name, 'loaderClassName']);
|
||||
} catch (Exception) {}
|
||||
|
||||
if ($className && class_exists($className)) {
|
||||
return $className;
|
||||
}
|
||||
|
||||
$className = 'Espo\Custom\Core\Loaders\\' . ucfirst($name);
|
||||
|
||||
if (!class_exists($className)) {
|
||||
$className = 'Espo\Core\Loaders\\' . ucfirst($name);
|
||||
}
|
||||
|
||||
if (class_exists($className)) {
|
||||
$class = new ReflectionClass($className);
|
||||
|
||||
if ($class->isInstantiable()) {
|
||||
return $className;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return ?class-string
|
||||
*/
|
||||
public function getServiceClassName(string $name): ?string
|
||||
{
|
||||
return $this->metadata->get(['app', 'containerServices', $name, 'className']) ?? null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return ?string[]
|
||||
*/
|
||||
public function getServiceDependencyList(string $name): ?array
|
||||
{
|
||||
return $this->metadata->get(['app', 'containerServices', $name, 'dependencyList']) ?? null;
|
||||
}
|
||||
|
||||
public function isSettable(string $name): bool
|
||||
{
|
||||
return $this->metadata->get(['app', 'containerServices', $name, 'settable']) ?? false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM – Open Source CRM application.
|
||||
* Copyright (C) 2014-2025 EspoCRM, Inc.
|
||||
* Website: https://www.espocrm.com
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* 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 Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
* The interactive user interfaces in modified source and object code versions
|
||||
* of this program must display Appropriate Legal Notices, as required under
|
||||
* Section 5 of the GNU Affero General Public License version 3.
|
||||
*
|
||||
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
|
||||
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
|
||||
************************************************************************/
|
||||
|
||||
namespace Espo\Core\Container\Exceptions;
|
||||
|
||||
use Psr\Container\NotFoundExceptionInterface;
|
||||
use RuntimeException;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
class NotFoundException extends RuntimeException implements NotFoundExceptionInterface
|
||||
{}
|
||||
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM – Open Source CRM application.
|
||||
* Copyright (C) 2014-2025 EspoCRM, Inc.
|
||||
* Website: https://www.espocrm.com
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* 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 Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
* The interactive user interfaces in modified source and object code versions
|
||||
* of this program must display Appropriate Legal Notices, as required under
|
||||
* Section 5 of the GNU Affero General Public License version 3.
|
||||
*
|
||||
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
|
||||
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
|
||||
************************************************************************/
|
||||
|
||||
namespace Espo\Core\Container\Exceptions;
|
||||
|
||||
use RuntimeException;
|
||||
|
||||
class NotSettableException extends RuntimeException
|
||||
{}
|
||||
41
application/Espo/Core/Container/Loader.php
Normal file
41
application/Espo/Core/Container/Loader.php
Normal file
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM – Open Source CRM application.
|
||||
* Copyright (C) 2014-2025 EspoCRM, Inc.
|
||||
* Website: https://www.espocrm.com
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* 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 Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
* The interactive user interfaces in modified source and object code versions
|
||||
* of this program must display Appropriate Legal Notices, as required under
|
||||
* Section 5 of the GNU Affero General Public License version 3.
|
||||
*
|
||||
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
|
||||
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
|
||||
************************************************************************/
|
||||
|
||||
namespace Espo\Core\Container;
|
||||
|
||||
/**
|
||||
* Loads a container service.
|
||||
*/
|
||||
interface Loader
|
||||
{
|
||||
/**
|
||||
* @return object
|
||||
*/
|
||||
public function load();
|
||||
}
|
||||
Reference in New Issue
Block a user