diff --git a/application/Espo/Core/ServiceFactory.php b/application/Espo/Core/ServiceFactory.php index 9daabce6d4..5d7ac6c7df 100644 --- a/application/Espo/Core/ServiceFactory.php +++ b/application/Espo/Core/ServiceFactory.php @@ -6,12 +6,13 @@ class ServiceFactory { private $container; - + + private $metadata; public function __construct(Container $container) { $this->container = $container; - + $this->metadata = $this->container->get('metadata'); } protected function getCotainer() @@ -19,21 +20,31 @@ class ServiceFactory return $this->container; } - - public function create($name) - { - // TODO lookup in metadata which module to use - $className = '\\Espo\\Services\\' . $name; + public function createByClassName() + { if (class_exists($className)) { $service = new $className(); $dependencies = $service->dependencies; foreach ($dependencies as $name) { $setMethod = 'set' . ucfirst($name); - $service->$setMethod($this->getCotainer()->get($name)); + $service->$setMethod($this->container->get($name)); } return $service; } // TODO throw an exception return null; + } + + + public function create($name) + { + $moduleName = $this->metadata->getScopeModuleName($name); + if ($moduleName) { + $className = '\\Espo\\Modules\\' . $moduleName . '\\Services\\' . $name; + } else { + $className = '\\Espo\\Services\\' . $name; + } + return $this->createByClassName($className); } + }