custom folder support. new service factory

This commit is contained in:
Yuri Kuznetsov
2014-01-27 18:02:55 +02:00
parent 2ac19d2eb9
commit 29178d4ca6
9 changed files with 168 additions and 79 deletions
+1 -1
View File
@@ -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,
+2 -2
View File
@@ -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);
}
}
+7 -13
View File
@@ -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)
+28 -48
View File
@@ -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;
}
}
+2 -2
View File
@@ -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')) {
+23 -4
View File
@@ -1,6 +1,7 @@
<?php
namespace Espo\Core\ORM;
use \Espo\Core\Utils\Util;
class EntityManager extends \Espo\ORM\EntityManager
@@ -13,6 +14,10 @@ class EntityManager extends \Espo\ORM\EntityManager
protected $container;
private $repositoryClassNameHash = array();
private $entityClassNameHash = array();
public function setContainer(\Espo\Core\Container $container)
{
$this->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];
}
}
+101 -3
View File
@@ -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;
}
}
@@ -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);
+1 -1
View File
@@ -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;
}