From 29178d4ca6e8853aa92a712e896d42e37c08360d Mon Sep 17 00:00:00 2001 From: Yuri Kuznetsov Date: Mon, 27 Jan 2014 18:02:55 +0200 Subject: [PATCH] custom folder support. new service factory --- application/Espo/Controllers/Stream.php | 2 +- application/Espo/Core/Controllers/Base.php | 4 +- application/Espo/Core/Controllers/Record.php | 20 ++-- application/Espo/Core/EntryPointManager.php | 76 +++++-------- application/Espo/Core/HookManager.php | 4 +- application/Espo/Core/ORM/EntityManager.php | 27 ++++- application/Espo/Core/ServiceFactory.php | 104 +++++++++++++++++- .../Modules/Crm/Controllers/Activities.php | 8 +- application/Espo/Services/Record.php | 2 +- 9 files changed, 168 insertions(+), 79 deletions(-) diff --git a/application/Espo/Controllers/Stream.php b/application/Espo/Controllers/Stream.php index c1900be590..165481c6f7 100644 --- a/application/Espo/Controllers/Stream.php +++ b/application/Espo/Controllers/Stream.php @@ -16,7 +16,7 @@ class Stream extends \Espo\Core\Controllers\Base $offset = intval($request->get('offset')); $maxSize = intval($request->get('maxSize')); - $service = $this->getService('\\Espo\\Services\\Stream'); + $service = $this->getService('Stream'); $result = $service->find($scope, $id, array( 'offset' => $offset, diff --git a/application/Espo/Core/Controllers/Base.php b/application/Espo/Core/Controllers/Base.php index bb091c0a98..e8b0655f49 100644 --- a/application/Espo/Core/Controllers/Base.php +++ b/application/Espo/Core/Controllers/Base.php @@ -69,9 +69,9 @@ abstract class Base return $this->container->get('serviceFactory'); } - protected function getService($className) + protected function getService($name) { - return $this->getServiceFactory()->createByClassName($className); + return $this->getServiceFactory()->create($name); } } diff --git a/application/Espo/Core/Controllers/Record.php b/application/Espo/Core/Controllers/Record.php index ca81dce35b..05b7c29f72 100644 --- a/application/Espo/Core/Controllers/Record.php +++ b/application/Espo/Core/Controllers/Record.php @@ -19,20 +19,14 @@ abstract class Record extends Base protected function getRecordService() { - $moduleName = $this->getMetadata()->getScopeModuleName($this->name); - if ($moduleName) { - $className = '\\Espo\\Modules\\' . $moduleName . '\\Services\\' . Util::normilizeClassName($this->name); - } else { - $className = '\\Espo\\Services\\' . Util::normilizeClassName($this->name); - } - if (!class_exists($className)) { - $className = '\\Espo\\Services\\Record'; + if ($this->getServiceFactory()->checkExists($this->name)) { + $service = $this->getServiceFactory()->create($this->name); + } else { + $service = $this->getServiceFactory()->create('Record'); } - - $service = $this->getService($className); - $service->setEntityName($this->name); - - return $service; + $service->setEntityName($this->name); + + return $service; } public function actionRead($params) diff --git a/application/Espo/Core/EntryPointManager.php b/application/Espo/Core/EntryPointManager.php index 56964fb643..6d377d4b02 100644 --- a/application/Espo/Core/EntryPointManager.php +++ b/application/Espo/Core/EntryPointManager.php @@ -25,15 +25,8 @@ class EntryPointManager */ private $paths = array( 'corePath' => 'application/Espo/EntryPoints', - 'modulePath' => 'application/Espo/Modules/{*}/EntryPoints', - ); - - /** - * @var array - path to entryPoint files in a custom folder - */ - private $customPaths = array( - 'corePath' => 'application/Espo/Custom/EntryPoints', - 'modulePath' => 'application/Espo/Custom/Modules/{*}/EntryPoints', + 'modulePath' => 'application/Espo/Modules/{*}/EntryPoints', + 'customPath' => 'application/Espo/Custom/EntryPoints', ); @@ -55,7 +48,7 @@ class EntryPointManager public function checkAuthRequired($name) { - $className = $this->get($name); + $className = $this->getClassName($name); if ($className === false) { throw new NotFound(); } @@ -64,7 +57,7 @@ class EntryPointManager public function run($name) { - $className = $this->get($name); + $className = $this->getClassName($name); if ($className === false) { throw new NotFound(); } @@ -73,16 +66,14 @@ class EntryPointManager $entryPoint->run(); } - protected function get($name = '') + protected function getClassName($name) { + $name = Util::normilizeClassName($name); + if (!isset($this->data)) { $this->init(); } - if (empty($name)) { - return $this->data; - } - $name = ucfirst($name); if (isset($this->data[$name])) { return $this->data[$name]; @@ -92,61 +83,50 @@ class EntryPointManager } - protected function getAll() - { - return $this->get(); - } - protected function init() { - if (file_exists($this->cacheFile) && $this->getContainer()->get('config')->get('useCache')) { + $config = $this->getContainer()->get('config'); + + if (file_exists($this->cacheFile) && $config->get('useCache')) { $this->data = $this->getFileManager()->getContent($this->cacheFile); - } else { - - $this->data = $this->getData( array($this->paths['corePath'], $this->customPaths['corePath']) ); + } else { + $this->data = $this->getClassNameHash(array($this->paths['corePath'], $this->paths['customPath']) ); foreach ($this->getContainer()->get('metadata')->getModuleList() as $moduleName) { - $path = str_replace('{*}', $moduleName, $this->paths['modulePath']); - $customPath = str_replace('{*}', $moduleName, $this->customPaths['modulePath']); + $path = str_replace('{*}', $moduleName, $this->paths['modulePath']); - $this->data = array_merge($this->data, $this->getData(array($path, $customPath))); + $this->data = array_merge($this->data, $this->getClassNameHash(array($path))); } - - $result = $this->getFileManager()->setContentPHP($this->data, $this->cacheFile); - if ($result == false) { - $GLOBALS['log']->add('EXCEPTION', 'EntryPoint::init() - Cannot save EntryPoints to a file'); - throw new \Espo\Core\Exceptions\Error(); - } + if ($config->get('useCache')) { + $result = $this->getFileManager()->setContentPHP($this->data, $this->cacheFile); + if ($result == false) { + throw new \Espo\Core\Exceptions\Error(); + } + } } } - - protected function getData(array $dirs) + + // TODO delegate to another class + protected function getClassNameHash(array $dirs) { - $entryPoints = array(); - - foreach ($dirs as $dir) { - + $data = array(); + foreach ($dirs as $dir) { if (file_exists($dir)) { $fileList = $this->getFileManager()->getFileList($dir, false, '\.php$', 'file'); - - foreach ($fileList as $file) { - + foreach ($fileList as $file) { $filePath = Util::concatPath($dir, $file); - $className = Util::getClassName($filePath); $fileName = $this->getFileManager()->getFileName($filePath); foreach ($this->allowMethods as $methodName) { if (method_exists($className, $methodName)) { - $entryPoints[$fileName] = $className; + $data[$fileName] = $className; } } } } - } - - return $entryPoints; + return $data; } } diff --git a/application/Espo/Core/HookManager.php b/application/Espo/Core/HookManager.php index dc488c7d5e..e5fce8b310 100644 --- a/application/Espo/Core/HookManager.php +++ b/application/Espo/Core/HookManager.php @@ -52,9 +52,9 @@ class HookManager $metadata = $this->container->get('metadata'); - $this->data = $this->getHookData( array('Espo/Hooks', 'Espo/Custom/Hooks') ); + $this->data = $this->getHookData(array('Espo/Hooks', 'Espo/Custom/Hooks') ); foreach ($metadata->getModuleList() as $moduleName) { - $this->data = array_merge($this->data, $this->getHookData(array('Espo/Modules/'.$moduleName.'/Hooks', 'Espo/Custom/Modules/'.$moduleName.'/Hooks'))); + $this->data = array_merge($this->data, $this->getHookData(array('Espo/Modules/' . $moduleName . '/Hooks'))); } if ($this->getConfig()->get('useCache')) { diff --git a/application/Espo/Core/ORM/EntityManager.php b/application/Espo/Core/ORM/EntityManager.php index 5e64ab2a4b..93a23a2665 100644 --- a/application/Espo/Core/ORM/EntityManager.php +++ b/application/Espo/Core/ORM/EntityManager.php @@ -1,6 +1,7 @@ container = $container; @@ -50,12 +55,26 @@ class EntityManager extends \Espo\ORM\EntityManager public function normalizeRepositoryName($name) { - return $this->espoMetadata->getRepositoryPath($name); + if (empty($this->repositoryClassNameHash[$name])) { + $className = '\\Espo\\Custom\\Repositories\\' . Util::normilizeClassName($name); + if (!class_exists($className)) { + $className = $this->espoMetadata->getRepositoryPath($name); + } + $this->repositoryClassNameHash[$name] = $className; + } + return $this->repositoryClassNameHash[$name]; } - + public function normalizeEntityName($name) - { - return $this->espoMetadata->getEntityPath($name); + { + if (empty($this->entityClassNameHash[$name])) { + $className = '\\Espo\\Custom\\Entities\\' . Util::normilizeClassName($name); + if (!class_exists($className)) { + $className = $this->espoMetadata->getEntityPath($name); + } + $this->entityClassNameHash[$name] = $className; + } + return $this->entityClassNameHash[$name]; } } diff --git a/application/Espo/Core/ServiceFactory.php b/application/Espo/Core/ServiceFactory.php index a2c41fefe4..b0bff268bb 100644 --- a/application/Espo/Core/ServiceFactory.php +++ b/application/Espo/Core/ServiceFactory.php @@ -4,16 +4,95 @@ namespace Espo\Core; use \Espo\Core\Exceptions\Error; +use \Espo\Core\Utils\Util; + class ServiceFactory { private $container; + + protected $cacheFile = 'data/cache/application/services.php'; + + /** + * @var array - path to Service files + */ + private $paths = array( + 'corePath' => 'application/Espo/Services', + 'modulePath' => 'application/Espo/Modules/{*}/Services', + 'customPath' => 'application/Espo/Custom/Services', + ); + + protected $data; public function __construct(Container $container) { - $this->container = $container; - } + $this->container = $container; + } + + protected function init() + { + $config = $this->getContainer()->get('config'); + + if (file_exists($this->cacheFile) && $config->get('useCache')) { + $this->data = $this->getFileManager()->getContent($this->cacheFile); + } else { + $this->data = $this->getClassNameHash(array($this->paths['corePath'], $this->paths['customPath'])); - public function createByClassName($className) + foreach ($this->getContainer()->get('metadata')->getModuleList() as $moduleName) { + $path = str_replace('{*}', $moduleName, $this->paths['modulePath']); + $this->data = array_merge($this->data, $this->getClassNameHash(array($path))); + } + if ($config->get('useCache')) { + $result = $this->getFileManager()->setContentPHP($this->data, $this->cacheFile); + if ($result == false) { + throw new \Espo\Core\Exceptions\Error(); + } + } + } + } + + protected function getFileManager() + { + return $this->container->get('fileManager'); + } + + protected function getContainer() + { + return $this->container; + } + + protected function getClassName($name) + { + $name = Util::normilizeClassName($name); + + if (!isset($this->data)) { + $this->init(); + } + + $name = ucfirst($name); + if (isset($this->data[$name])) { + return $this->data[$name]; + } + + return false; + } + + public function checkExists($name) { + $className = $this->getClassName($name); + if (!empty($className)) { + return true; + } + } + + public function create($name) + { + $className = $this->getClassName($name); + if (empty($className)) { + throw new Error(); + } + return $this->createByClassName($className); + } + + protected function createByClassName($className) { if (class_exists($className)) { $service = new $className(); @@ -25,5 +104,24 @@ class ServiceFactory } throw new Error("Class '$className' does not exist"); } + + // TODO delegate to another class + protected function getClassNameHash(array $dirs) + { + $data = array(); + + foreach ($dirs as $dir) { + if (file_exists($dir)) { + $fileList = $this->getFileManager()->getFileList($dir, false, '\.php$', 'file'); + foreach ($fileList as $file) { + $filePath = Util::concatPath($dir, $file); + $className = Util::getClassName($filePath); + $fileName = $this->getFileManager()->getFileName($filePath); + $data[$fileName] = $className; + } + } + } + return $data; + } } diff --git a/application/Espo/Modules/Crm/Controllers/Activities.php b/application/Espo/Modules/Crm/Controllers/Activities.php index 0d9a5fb215..39a08d672d 100644 --- a/application/Espo/Modules/Crm/Controllers/Activities.php +++ b/application/Espo/Modules/Crm/Controllers/Activities.php @@ -8,9 +8,7 @@ use \Espo\Core\Exceptions\Error, class Activities extends \Espo\Core\Controllers\Base { - public static $defaultAction = 'index'; - - protected $serviceClassName = '\\Espo\\Modules\\Crm\\Services\\Activities'; + public static $defaultAction = 'index'; public function actionListEvents($params, $data, $request) { @@ -21,7 +19,7 @@ class Activities extends \Espo\Core\Controllers\Base throw new BadRequest(); } - $service = $this->getService($this->serviceClassName); + $service = $this->getService('Activities'); return $service->getEvents($from, $to); } @@ -42,7 +40,7 @@ class Activities extends \Espo\Core\Controllers\Base $scope = $where['scope']; } - $service = $this->getService($this->serviceClassName); + $service = $this->getService('Activities'); $methodName = 'get' . ucfirst($name); diff --git a/application/Espo/Services/Record.php b/application/Espo/Services/Record.php index 9cf1702c5c..1f90008b8f 100644 --- a/application/Espo/Services/Record.php +++ b/application/Espo/Services/Record.php @@ -87,7 +87,7 @@ class Record extends \Espo\Core\Services\Base protected function getStreamService() { if (empty($this->streamService)) { - $this->streamService = $this->getServiceFactory()->createByClassName('\\Espo\\Services\\Stream'); + $this->streamService = $this->getServiceFactory()->create('Stream'); } return $this->streamService; }