add EntryPointManager

This commit is contained in:
Taras Machyshyn
2014-01-24 13:49:08 +02:00
parent fea3ac311b
commit dd4b91b2f1
15 changed files with 393 additions and 24 deletions
+7
View File
@@ -87,6 +87,13 @@ class Container
);
}
private function loadEntryPointManager()
{
return new \Espo\Core\EntryPointManager(
$this
);
}
private function loadLog()
{
return new \Espo\Core\Utils\Log(
+153
View File
@@ -0,0 +1,153 @@
<?php
namespace Espo\Core;
use \Espo\Core\Exceptions\NotFound,
\Espo\Core\Utils\Util;
class EntryPointManager
{
private $container;
private $fileManager;
protected $data = null;
protected $cacheFile = 'data/cache/application/entryPoints.php';
protected $allowMethods = array(
'run',
);
/**
* @var array - path to entryPoint files
*/
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',
);
public function __construct(\Espo\Core\Container $container)
{
$this->container = $container;
$this->fileManager = $this->getContainer()->get('fileManager');
}
protected function getContainer()
{
return $this->container;
}
protected function getFileManager()
{
return $this->fileManager;
}
public function run($name)
{
$className = $this->get($name);
if ($className === false) {
throw new NotFound();
}
$entryPoint = new $className($this->container);
$entryPoint->run();
}
public function get($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];
}
return false;
}
public function getAll()
{
return $this->get();
}
protected function init()
{
if (file_exists($this->cacheFile) && $this->getContainer()->get('config')->get('useCache')) {
$this->data = $this->getFileManager()->getContent($this->cacheFile);
} else {
$this->data = $this->getData( array($this->paths['corePath'], $this->customPaths['corePath']) );
foreach ($this->getContainer()->get('metadata')->getModuleList() as $moduleName) {
$path = str_replace('{*}', $moduleName, $this->paths['modulePath']);
$customPath = str_replace('{*}', $moduleName, $this->customPaths['modulePath']);
$this->data = array_merge($this->data, $this->getData(array($path, $customPath)));
}
$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();
}
}
}
protected function getData(array $dirs)
{
$entryPoints = 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);
foreach ($this->allowMethods as $methodName) {
if (method_exists($className, $methodName)) {
$entryPoints[$fileName] = $className;
}
}
}
}
}
return $entryPoints;
}
}
+7 -9
View File
@@ -395,26 +395,24 @@ class Manager
*
* @return array
*/
function getFileName($filename, $ext='')
{
public function getFileName($fileName, $ext='')
{
if (empty($ext)) {
$realFileName= substr($filename, 0, strrpos($filename, '.', -1));
$fileName= substr($fileName, 0, strrpos($fileName, '.', -1));
}
else {
if (substr($ext, 0, 1)!='.') {
$ext= '.'.$ext;
}
if (substr($filename, -(strlen($ext)))==$ext) {
$realFileName= substr($filename, 0, -(strlen($ext)));
if (substr($fileName, -(strlen($ext)))==$ext) {
$fileName= substr($fileName, 0, -(strlen($ext)));
}
}
if (!empty($realFileName)) {
return $realFileName;
}
$exFileName = explode('/', Utils\Util::toFormat($fileName, '/'));
return $filename;
return end($exFileName);
}
+28 -12
View File
@@ -44,13 +44,12 @@ class Util
/**
* Convert name to Camel Case format
* ex. camel-case to camelCase
*
* @param string $name
*
* @return string
*/
* Convert name to Camel Case format, ex. camel-case to camelCase
* @param string $name
* @param boolean $capitaliseFirstChar
* @param string $symbol
* @return string
*/
public static function toCamelCase($name, $capitaliseFirstChar = false, $symbol = '-')
{
if($capitaliseFirstChar) {
@@ -276,11 +275,11 @@ class Util
*
* @param array $content
* @param array $unsets in format
array(
$entity['name'] => array(
'fields.UnsetFieldName',
),
)
* array(
* $entity['name'] => array(
* 'fields.UnsetFieldName',
* ),
* )
*
* @return array
*/
@@ -306,6 +305,23 @@ class Util
return $content;
}
/**
* Get class name from the file path
*
* @param string $filePath
*
* @return string
*/
public static function getClassName($filePath)
{
$className = preg_replace('/\.php$/i', '', $filePath);
$className = preg_replace('/^application\//i', '', $className);
$className = '\\'.static::toFormat($className, '\\');
return $className;
}
}
+1 -1
View File
@@ -1,6 +1,6 @@
<?php
namespace Espo\Core\EntryPoints;
namespace Espo\EntryPoints;
use \Espo\Core\Exceptions\NotFound;
use \Espo\Core\Exceptions\Forbidden;
+2 -1
View File
@@ -8,7 +8,8 @@
},
"autoload": {
"psr-0": {
"": "application/"
"": "application/",
"tests": ""
}
},
"require-dev": {
Generated
+1 -1
View File
@@ -3,7 +3,7 @@
"This file locks the dependencies of your project to a known state",
"Read more about it at http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file"
],
"hash": "1ddc368269ad9c948b31918fe61d8a9f",
"hash": "457d9eb8c6fae33a444f431e130455d4",
"packages": [
{
"name": "doctrine/annotations",
+75
View File
@@ -0,0 +1,75 @@
<?php
namespace tests\Espo\Core;
use tests\ReflectionHelper;
class EntryPointManagerTest extends \PHPUnit_Framework_TestCase
{
protected $object;
protected $objects;
protected $filesPath= 'tests/testData/EntryPoints';
protected function setUp()
{
$this->objects['container'] = $this->getMockBuilder('\\Espo\\Core\\Container')->disableOriginalConstructor()->getMock();
$this->object = new \Espo\Core\EntryPointManager($this->objects['container']);
$this->reflection = new ReflectionHelper($this->object);
$fileManager = new \Espo\Core\Utils\File\Manager( (object) array());
$this->reflection->setProperty('fileManager', $fileManager);
$this->reflection->setProperty('cacheFile', 'tests/testData/EntryPoints/cache/entryPoints.php');
$this->reflection->setProperty('paths', array(
'corePath' => 'tests/testData/EntryPoints/Espo/EntryPoints',
'modulePath' => 'tests/testData/EntryPoints/Espo/Modules/Crm/EntryPoints',
));
$this->reflection->setProperty('customPaths', array(
'corePath' => '',
'modulePath' => '',
));
}
protected function tearDown()
{
$this->object = NULL;
}
function testGetData()
{
$result = array(
'Download' => '\tests\testData\EntryPoints\Espo\EntryPoints\Download',
'Test' => '\tests\testData\EntryPoints\Espo\EntryPoints\Test',
'InModule' => '\tests\testData\EntryPoints\Espo\Modules\Crm\EntryPoints\InModule'
);
$this->assertEquals( $result, $this->reflection->invokeMethod('getData', array($this->reflection->getProperty('paths'))) );
}
function testGet()
{
$this->reflection->setProperty('data', array(
'Test' => '\tests\testData\EntryPoints\Espo\EntryPoints\Test',
));
$this->assertEquals('\tests\testData\EntryPoints\Espo\EntryPoints\Test', $this->reflection->invokeMethod('get', array('test')) );
}
function testRun()
{
$this->reflection->setProperty('data', array(
'Test' => '\tests\testData\EntryPoints\Espo\EntryPoints\Test',
));
$this->assertNull( $this->reflection->invokeMethod('run', array('test')) );
}
}
?>
@@ -0,0 +1,48 @@
<?php
namespace tests\Espo\Core\Utils\File;
class ManagerTest extends \PHPUnit_Framework_TestCase
{
protected $object;
protected $objects;
protected $filesPath= 'tests/testData/FileManager';
protected function setUp()
{
$this->object = new \Espo\Core\Utils\File\Manager(
(object) array(
'defaultPermissions' => (object) array (
'dir' => '0775',
'file' => '0664',
'user' => '',
'group' => '',
),
)
);
}
protected function tearDown()
{
$this->object = NULL;
}
function testGetFileName()
{
$this->assertEquals('Donwload', $this->object->getFileName('Donwload.php'));
$this->assertEquals('Donwload', $this->object->getFileName('/Donwload.php'));
$this->assertEquals('Donwload', $this->object->getFileName('\Donwload.php'));
$this->assertEquals('Donwload', $this->object->getFileName('application/Espo/EntryPoints/Donwload.php'));
}
}
?>
+9
View File
@@ -211,6 +211,15 @@ class UtilTest extends \PHPUnit_Framework_TestCase
}
function testGetClassName()
{
$this->assertEquals('\Espo\EntryPoints\Donwload', Util::getClassName('application/Espo/EntryPoints/Donwload.php'));
$this->assertEquals('\Espo\EntryPoints\Donwload', Util::getClassName('Espo/EntryPoints/Donwload.php'));
}
/*function testGetScopeModuleName()
{
$this->assertEquals('Crm', $this->fixture->getScopeModuleName('Account'));
+15
View File
@@ -0,0 +1,15 @@
<?php
namespace tests\testData\EntryPoints\Espo\EntryPoints;
class Download extends \Espo\Core\EntryPoints\Base
{
protected $authRequired = true;
public function run()
{
}
}
+16
View File
@@ -0,0 +1,16 @@
<?php
namespace tests\testData\EntryPoints\Espo\EntryPoints;
class Test
{
protected $authRequired = true;
public function run()
{
}
}
@@ -0,0 +1,15 @@
<?php
namespace tests\testData\EntryPoints\Espo\Modules\Crm\EntryPoints;
class InModule
{
protected $authRequired = true;
public function run()
{
}
}
@@ -0,0 +1,15 @@
<?php
namespace tests\testData\EntryPoints\Espo\Modules\Crm\EntryPoints;
class InModuleNoRunMethod
{
protected $authRequired = true;
public function noRunMethod()
{
}
}
+1
View File
@@ -6,6 +6,7 @@ $vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);
return array(
'tests' => array($baseDir . '/'),
'Zend\\Validator\\' => array($vendorDir . '/zendframework/zend-validator'),
'Zend\\Stdlib\\' => array($vendorDir . '/zendframework/zend-stdlib'),
'Zend\\ServiceManager\\' => array($vendorDir . '/zendframework/zend-servicemanager'),