diff --git a/application/Espo/Core/EntryPointManager.php b/application/Espo/Core/EntryPointManager.php index 3c1b3ca334..fc80a706f3 100644 --- a/application/Espo/Core/EntryPointManager.php +++ b/application/Espo/Core/EntryPointManager.php @@ -25,12 +25,12 @@ * * In accordance with Section 7(b) of the GNU General Public License version 3, * these Appropriate Legal Notices must retain the display of the "EspoCRM" word. - ************************************************************************/ + ************************************************************************/ namespace Espo\Core; -use \Espo\Core\Exceptions\NotFound, - \Espo\Core\Utils\Util; +use Espo\Core\Exceptions\NotFound, + Espo\Core\Utils\Util; class EntryPointManager { @@ -42,19 +42,18 @@ class EntryPointManager protected $cacheFile = 'data/cache/application/entryPoints.php'; - protected $allowedMethods = array( + protected $allowedMethods = [ 'run', - ); + ]; /** * @var array - path to entryPoint files */ - private $paths = array( + private $paths = [ 'corePath' => 'application/Espo/EntryPoints', 'modulePath' => 'application/Espo/Modules/{*}/EntryPoints', 'customPath' => 'custom/Espo/Custom/EntryPoints', - ); - + ]; public function __construct(\Espo\Core\Container $container) { @@ -117,13 +116,9 @@ class EntryPointManager return false; } - protected function init() { $classParser = $this->getContainer()->get('classParser'); - $classParser->setAllowedMethods($this->allowedMethods); - $this->data = $classParser->getData($this->paths, $this->cacheFile); + $this->data = $classParser->getData($this->paths, $this->cacheFile, $this->allowedMethods); } - } - diff --git a/application/Espo/Core/ServiceFactory.php b/application/Espo/Core/ServiceFactory.php index a058039515..ee0265a7b4 100644 --- a/application/Espo/Core/ServiceFactory.php +++ b/application/Espo/Core/ServiceFactory.php @@ -67,7 +67,6 @@ class ServiceFactory protected function init() { $classParser = $this->getContainer()->get('classParser'); - $classParser->setAllowedMethods(null); $this->data = $classParser->getData($this->paths, $this->cacheFile); } diff --git a/application/Espo/Core/Utils/Database/Schema/Schema.php b/application/Espo/Core/Utils/Database/Schema/Schema.php index bdf3651e17..822b8373dd 100644 --- a/application/Espo/Core/Utils/Database/Schema/Schema.php +++ b/application/Espo/Core/Utils/Database/Schema/Schema.php @@ -50,19 +50,19 @@ class Schema private $databaseHelper; - protected $fieldTypePaths = array( + protected $fieldTypePaths = [ 'application/Espo/Core/Utils/Database/DBAL/FieldTypes', 'custom/Espo/Custom/Core/Utils/Database/DBAL/FieldTypes', - ); + ]; /** * Paths of rebuild action folders * @var array */ - protected $rebuildActionsPath = array( + protected $rebuildActionsPath = [ 'corePath' => 'application/Espo/Core/Utils/Database/Schema/rebuildActions', 'customPath' => 'custom/Espo/Custom/Core/Utils/Database/Schema/rebuildActions', - ); + ]; /** * Array of rebuildActions classes in format: @@ -74,7 +74,10 @@ class Schema */ protected $rebuildActionClasses = null; - public function __construct(\Espo\Core\Utils\Config $config, \Espo\Core\Utils\Metadata $metadata, \Espo\Core\Utils\File\Manager $fileManager, \Espo\Core\ORM\EntityManager $entityManager, \Espo\Core\Utils\File\ClassParser $classParser, \Espo\Core\Utils\Metadata\OrmMetadata $ormMetadata) + public function __construct( + \Espo\Core\Utils\Config $config, \Espo\Core\Utils\Metadata $metadata, + \Espo\Core\Utils\File\Manager $fileManager, \Espo\Core\ORM\EntityManager $entityManager, + \Espo\Core\Utils\File\ClassParser $classParser, \Espo\Core\Utils\Metadata\OrmMetadata $ormMetadata) { $this->config = $config; $this->metadata = $metadata; @@ -145,7 +148,7 @@ class Schema protected function initFieldTypes() { - foreach($this->fieldTypePaths as $path) { + foreach ($this->fieldTypePaths as $path) { $typeList = $this->getFileManager()->getFileList($path, false, '\.php$'); if ($typeList !== false) { @@ -257,12 +260,11 @@ class Schema */ protected function initRebuildActions($currentSchema = null, $metadataSchema = null) { - $methods = array('beforeRebuild', 'afterRebuild'); + $methods = ['beforeRebuild', 'afterRebuild']; - $this->getClassParser()->setAllowedMethods($methods); - $rebuildActions = $this->getClassParser()->getData($this->rebuildActionsPath); + $rebuildActions = $this->getClassParser()->getData($this->rebuildActionsPath, null, $methods); - $classes = array(); + $classes = []; foreach ($rebuildActions as $actionName => $actionClass) { $rebuildActionClass = new $actionClass($this->metadata, $this->config, $this->entityManager); if (isset($currentSchema)) { diff --git a/application/Espo/Core/Utils/File/ClassParser.php b/application/Espo/Core/Utils/File/ClassParser.php index c82013fef4..3117f7bedc 100644 --- a/application/Espo/Core/Utils/File/ClassParser.php +++ b/application/Espo/Core/Utils/File/ClassParser.php @@ -28,7 +28,13 @@ ************************************************************************/ namespace Espo\Core\Utils\File; -use \Espo\Core\Utils\Util; + +use Espo\Core\Utils\Util; +use Espo\Core\Utils\File\Manager as FileManager; +use Espo\Core\Utils\Config; +use Espo\Core\Utils\Metadata; + +use Espo\Core\Exceptions\Error; class ClassParser { @@ -38,13 +44,7 @@ class ClassParser private $metadata; - protected $cacheFile = null; - - protected $allowedMethods = array( - 'run', - ); - - public function __construct(\Espo\Core\Utils\File\Manager $fileManager, \Espo\Core\Utils\Config $config, \Espo\Core\Utils\Metadata $metadata) + public function __construct(FileManager $fileManager, Config $config, Metadata $metadata) { $this->fileManager = $fileManager; $this->config = $config; @@ -66,53 +66,54 @@ class ClassParser return $this->metadata; } - public function setAllowedMethods($methods) - { - $this->allowedMethods = $methods; - } - /** - * Return path data of classes + * Return paths to class files. * - * @param string $cacheFile full path for a cache file, ex. data/cache/application/entryPoints.php - * @param string | array $paths in format array( + * @param string | array $paths in format [ * 'corePath' => '', * 'modulePath' => '', * 'customPath' => '', - * ); - * @return array + * ] + * @param $cacheFile Full path for a cache file, ex. data/cache/application/entryPoints.php. + * @param $allowedMethods If specified, classes w/o specified method will be ignored. */ - public function getData($paths, $cacheFile = false) + public function getData($paths, ?string $cacheFile = null, ?array $allowedMethods = null) : array { $data = null; if (is_string($paths)) { - $paths = array( + $paths = [ 'corePath' => $paths, - ); + ]; } if ($cacheFile && file_exists($cacheFile) && $this->getConfig()->get('useCache')) { $data = $this->getFileManager()->getPhpContents($cacheFile); - } else { - $data = $this->getClassNameHash($paths['corePath']); + + if (!is_array($data)) { + $GLOBALS['log']->error("ClassParser: Non-array value stored in {$cacheFile}."); + } + } + + if (!is_array($data)) { + $data = $this->getClassNameHash($paths['corePath'], $allowedMethods); if (isset($paths['modulePath'])) { foreach ($this->getMetadata()->getModuleList() as $moduleName) { $path = str_replace('{*}', $moduleName, $paths['modulePath']); - $data = array_merge($data, $this->getClassNameHash($path)); + $data = array_merge($data, $this->getClassNameHash($path, $allowedMethods)); } } if (isset($paths['customPath'])) { - $data = array_merge($data, $this->getClassNameHash($paths['customPath'])); + $data = array_merge($data, $this->getClassNameHash($paths['customPath'], $allowedMethods)); } if ($cacheFile && $this->getConfig()->get('useCache')) { $result = $this->getFileManager()->putPhpContents($cacheFile, $data); if ($result == false) { - throw new \Espo\Core\Exceptions\Error(); + throw new Error("ClassParser: Could not save file {$cacheFile}."); } } } @@ -120,13 +121,13 @@ class ClassParser return $data; } - protected function getClassNameHash($dirs) + protected function getClassNameHash($dirs, ?array $allowedMethods = []) { if (is_string($dirs)) { $dirs = (array) $dirs; } - $data = array(); + $data = []; foreach ($dirs as $dir) { if (file_exists($dir)) { $fileList = $this->getFileManager()->getFileList($dir, false, '\.php$', true); @@ -139,12 +140,12 @@ class ClassParser $scopeName = ucfirst($fileName); $normalizedScopeName = Util::normilizeScopeName($scopeName); - if (empty($this->allowedMethods)) { + if (empty($allowedMethods)) { $data[$normalizedScopeName] = $className; continue; } - foreach ($this->allowedMethods as $methodName) { + foreach ($allowedMethods as $methodName) { if (method_exists($className, $methodName)) { $data[$normalizedScopeName] = $className; } @@ -156,5 +157,4 @@ class ClassParser return $data; } - -} \ No newline at end of file +} diff --git a/application/Espo/Core/Utils/ScheduledJob.php b/application/Espo/Core/Utils/ScheduledJob.php index bb85db9be7..665a2cb297 100644 --- a/application/Espo/Core/Utils/ScheduledJob.php +++ b/application/Espo/Core/Utils/ScheduledJob.php @@ -55,18 +55,18 @@ class ScheduledJob /** * @var array - path to cron job files */ - private $paths = array( + private $paths = [ 'corePath' => 'application/Espo/Jobs', 'modulePath' => 'application/Espo/Modules/{*}/Jobs', 'customPath' => 'custom/Espo/Custom/Jobs', - ); + ]; - protected $cronSetup = array( + protected $cronSetup = [ 'linux' => '* * * * * cd {DOCUMENT_ROOT}; {PHP-BIN-DIR} -f {CRON-FILE} > /dev/null 2>&1', 'windows' => '{PHP-BINARY} -f {FULL-CRON-PATH}', 'mac' => '* * * * * cd {DOCUMENT_ROOT}; {PHP-BIN-DIR} -f {CRON-FILE} > /dev/null 2>&1', 'default' => '* * * * * cd {DOCUMENT_ROOT}; {PHP-BIN-DIR} -f {CRON-FILE} > /dev/null 2>&1', - ); + ]; public function __construct(\Espo\Core\Container $container) { @@ -169,8 +169,7 @@ class ScheduledJob protected function init() { $classParser = $this->getContainer()->get('classParser'); - $classParser->setAllowedMethods( array($this->allowedMethod) ); - $this->data = $classParser->getData($this->paths, $this->cacheFile); + $this->data = $classParser->getData($this->paths, $this->cacheFile, [$this->allowedMethod]); } public function getSetupMessage() diff --git a/tests/unit/Espo/Core/Utils/File/ClassParserTest.php b/tests/unit/Espo/Core/Utils/File/ClassParserTest.php index 336e38e1e9..a9913cf6c6 100644 --- a/tests/unit/Espo/Core/Utils/File/ClassParserTest.php +++ b/tests/unit/Espo/Core/Utils/File/ClassParserTest.php @@ -31,7 +31,6 @@ namespace tests\unit\Espo\Core\Utils\File; use tests\unit\ReflectionHelper; - class ClassParserTest extends \PHPUnit\Framework\TestCase { protected $object; @@ -40,14 +39,14 @@ class ClassParserTest extends \PHPUnit\Framework\TestCase protected $reflection; - protected function setUp() : void { $this->objects['fileManager'] = new \Espo\Core\Utils\File\Manager(); $this->objects['config'] = $this->getMockBuilder('\Espo\Core\Utils\Config')->disableOriginalConstructor()->getMock(); $this->objects['metadata'] = $this->getMockBuilder('\Espo\Core\Utils\Metadata')->disableOriginalConstructor()->getMock(); - $this->object = new \Espo\Core\Utils\File\ClassParser($this->objects['fileManager'], $this->objects['config'], $this->objects['metadata']); + $this->object = new \Espo\Core\Utils\File\ClassParser( + $this->objects['fileManager'], $this->objects['config'], $this->objects['metadata']); $this->reflection = new ReflectionHelper($this->object); } @@ -57,23 +56,21 @@ class ClassParserTest extends \PHPUnit\Framework\TestCase $this->object = NULL; } - function testGetClassNameHash() { - $paths = array( + $paths = [ 'tests/unit/testData/EntryPoints/Espo/EntryPoints', - 'tests/unit/testData/EntryPoints/Espo/Modules/Crm/EntryPoints', - ); + 'tests/unit/testData/EntryPoints/Espo/Modules/Crm/EntryPoints', + ]; - $result = array( + $result = [ 'Download' => '\tests\unit\testData\EntryPoints\Espo\EntryPoints\Download', 'Test' => '\tests\unit\testData\EntryPoints\Espo\EntryPoints\Test', 'InModule' => '\tests\unit\testData\EntryPoints\Espo\Modules\Crm\EntryPoints\InModule' - ); - $this->assertEquals( $result, $this->reflection->invokeMethod('getClassNameHash', array($paths)) ); + ]; + $this->assertEquals($result, $this->reflection->invokeMethod('getClassNameHash', [$paths, ['run']])); } - function testGetDataWithCache() { $this->objects['config'] @@ -82,17 +79,17 @@ class ClassParserTest extends \PHPUnit\Framework\TestCase ->will($this->returnValue(true)); $cacheFile = 'tests/unit/testData/EntryPoints/cache/entryPoints.php'; - $paths = array( + $paths = [ 'corePath' => 'tests/unit/testData/EntryPoints/Espo/EntryPoints', - 'modulePath' => 'tests/unit/testData/EntryPoints/Espo/Modules/{*}/EntryPoints', + 'modulePath' => 'tests/unit/testData/EntryPoints/Espo/Modules/{*}/EntryPoints', 'customPath' => 'tests/unit/testData/EntryPoints/Espo/Custom/EntryPoints', - ); + ]; - $result = array ( - 'Download' => '\\tests\\unit\\testData\\EntryPoints\\Espo\\EntryPoints\\Download', - ); + $result = [ + 'Download' => '\\tests\\unit\\testData\\EntryPoints\\Espo\\EntryPoints\\Download', + ]; - $this->assertEquals( $result, $this->reflection->invokeMethod('getData', array($paths, $cacheFile)) ); + $this->assertEquals($result, $this->reflection->invokeMethod('getData', [$paths, $cacheFile, ['run']]) ); } function testGetDataWithNoCache() @@ -106,28 +103,27 @@ class ClassParserTest extends \PHPUnit\Framework\TestCase ->expects($this->once()) ->method('getModuleList') ->will($this->returnValue( - array( + [ 'Crm', - ) + ] )); $cacheFile = 'tests/unit/testData/EntryPoints/cache/entryPoints.php'; - $paths = array( + $paths = [ 'corePath' => 'tests/unit/testData/EntryPoints/Espo/EntryPoints', - 'modulePath' => 'tests/unit/testData/EntryPoints/Espo/Modules/{*}/EntryPoints', + 'modulePath' => 'tests/unit/testData/EntryPoints/Espo/Modules/{*}/EntryPoints', 'customPath' => 'tests/unit/testData/EntryPoints/Espo/Custom/EntryPoints', - ); + ]; - $result = array( + $result = [ 'Download' => '\tests\unit\testData\EntryPoints\Espo\EntryPoints\Download', 'Test' => '\tests\unit\testData\EntryPoints\Espo\EntryPoints\Test', 'InModule' => '\tests\unit\testData\EntryPoints\Espo\Modules\Crm\EntryPoints\InModule' - ); + ]; - $this->assertEquals( $result, $this->reflection->invokeMethod('getData', array($paths, $cacheFile)) ); + $this->assertEquals($result, $this->reflection->invokeMethod('getData', [$paths, $cacheFile, ['run']])); } - function testGetDataWithNoCacheString() { $this->objects['config'] @@ -139,23 +135,22 @@ class ClassParserTest extends \PHPUnit\Framework\TestCase ->expects($this->never()) ->method('getModuleList') ->will($this->returnValue( - array( + [ 'Crm', - ) + ] )); $cacheFile = 'tests/unit/testData/EntryPoints/cache/entryPoints.php'; $path = 'tests/unit/testData/EntryPoints/Espo/EntryPoints'; - $result = array( + $result = [ 'Download' => '\tests\unit\testData\EntryPoints\Espo\EntryPoints\Download', 'Test' => '\tests\unit\testData\EntryPoints\Espo\EntryPoints\Test', - ); + ]; - $this->assertEquals( $result, $this->reflection->invokeMethod('getData', array($path, $cacheFile)) ); + $this->assertEquals($result, $this->reflection->invokeMethod('getData', [$path, $cacheFile, ['run']])); } - function testGetDataWithCacheFalse() { $this->objects['config'] @@ -167,26 +162,21 @@ class ClassParserTest extends \PHPUnit\Framework\TestCase ->expects($this->never()) ->method('getModuleList') ->will($this->returnValue( - array( + [ 'Crm', - ) + ] )); - $paths = array( + $paths = [ 'corePath' => 'tests/unit/testData/EntryPoints/Espo/EntryPoints', 'customPath' => 'tests/unit/testData/EntryPoints/Espo/Custom/EntryPoints', - ); + ]; - $result = array( + $result = [ 'Download' => '\tests\unit\testData\EntryPoints\Espo\EntryPoints\Download', 'Test' => '\tests\unit\testData\EntryPoints\Espo\EntryPoints\Test', - ); + ]; - $this->assertEquals( $result, $this->reflection->invokeMethod('getData', array($paths)) ); + $this->assertEquals($result, $this->reflection->invokeMethod('getData', [$paths, null, ['run']])); } - - - } - -?>