diff --git a/application/Espo/Core/Application.php b/application/Espo/Core/Application.php index 173c152d81..a444c1e72e 100644 --- a/application/Espo/Core/Application.php +++ b/application/Espo/Core/Application.php @@ -133,7 +133,7 @@ class Application { $config = $this->getContainer()->get('config'); - if (file_exists($config->get('configPath')) && $config->get('isInstalled')) { + if (file_exists($config->getConfigPath()) && $config->get('isInstalled')) { return true; } diff --git a/application/Espo/Core/DataManager.php b/application/Espo/Core/DataManager.php index 63f7c01720..10e3ffeceb 100644 --- a/application/Espo/Core/DataManager.php +++ b/application/Espo/Core/DataManager.php @@ -26,6 +26,9 @@ class DataManager { private $container; + private $cachePath = 'data/cache'; + + public function __construct(Container $container) { $this->container = $container; @@ -59,9 +62,7 @@ class DataManager */ public function clearCache() { - $cacheDir = $this->getContainer()->get('config')->get('cachePath'); - - $result = $this->getContainer()->get('fileManager')->removeInDir($cacheDir); + $result = $this->getContainer()->get('fileManager')->removeInDir($this->cachePath); if ($result === false) { throw new Exceptions\Error("Error while clearing cache"); diff --git a/application/Espo/Core/Utils/Config.php b/application/Espo/Core/Utils/Config.php index c6171939cf..933211ace7 100644 --- a/application/Espo/Core/Utils/Config.php +++ b/application/Espo/Core/Utils/Config.php @@ -32,6 +32,8 @@ class Config */ private $defaultConfigPath = 'application/Espo/Core/defaults/config.php'; + protected $configPath = 'data/config.php'; + /** * Array of admin items * @@ -62,6 +64,11 @@ class Config return $this->fileManager; } + public function getConfigPath() + { + return $this->configPath; + } + /** * Get an option from config * @@ -85,7 +92,6 @@ class Config return $lastBranch; } - /** * Set an option to the config * @@ -95,15 +101,11 @@ class Config */ public function set($name, $value = '') { - if (is_array($name)) { - return $this->setArray($name); + if (!is_array($name)) { + $name = array($name => $value); } - $content = array($name => $value); - $status = $this->getFileManager()->mergeContentsPHP($this->get('configPath'), $content, true); - $this->loadConfig(true); - - return $status; + return $this->setArray($name); } @@ -119,10 +121,15 @@ class Config return false; } - $status = $this->getFileManager()->mergeContentsPHP($this->get('configPath'), $values, true); + $result = $this->getFileManager()->mergeContentsPHP($this->configPath, $values, true); $this->loadConfig(true); - return $status; + return $result; + } + + public function getDefaults() + { + return $this->getFileManager()->getContents($this->defaultConfigPath); } /** @@ -136,20 +143,9 @@ class Config return $this->configData; } - $defaultConfig = $this->getFileManager()->getContents($this->defaultConfigPath); + $configPath = file_exists($this->configPath) ? $this->configPath : $this->defaultConfigPath; - $config = $this->getFileManager()->getContents($defaultConfig['configPath']); - if (empty($config)) { - $config = array(); - } - - if (!empty($defaultConfig['rewriteOptions'])) { - $this->configData = Util::merge((array) $defaultConfig, (array) $config, 1, $defaultConfig['rewriteOptions']); - } else { - $this->configData = Util::merge((array) $defaultConfig, (array) $config); - } - - $this->adminItems = $this->getRestrictItems(); + $this->configData = $this->getFileManager()->getContents($configPath); return $this->configData; } @@ -161,7 +157,7 @@ class Config * @param $isAdmin * @return array */ - public function getData($isAdmin=false) + public function getData($isAdmin = false) { $configData = $this->loadConfig(); @@ -182,7 +178,7 @@ class Config * @param $isAdmin * @return bool */ - public function setData($data, $isAdmin=false) + public function setData($data, $isAdmin = false) { $restrictItems = $this->getRestrictItems($isAdmin); @@ -224,7 +220,7 @@ class Config * @param $isAdmin * @return bool */ - protected function isAllowed($name, $isAdmin=false) + protected function isAllowed($name, $isAdmin = false) { if (in_array($name, $this->getRestrictItems($isAdmin))) { return false; diff --git a/application/Espo/Core/defaults/config.php b/application/Espo/Core/defaults/config.php index 741826ae0f..986b8f8c2c 100644 --- a/application/Espo/Core/defaults/config.php +++ b/application/Espo/Core/defaults/config.php @@ -21,13 +21,13 @@ ************************************************************************/ return array ( - 'configPath' => 'data/config.php', - 'cachePath' => 'data/cache', - 'database' => array ( 'driver' => 'pdo_mysql', 'host' => 'localhost', + 'dbname' => '', + 'user' => '', + 'password' => '', ), 'useCache' => true, 'recordsPerPage' => 20, @@ -44,6 +44,12 @@ return array ( array ( ), 'defaultCurrency' => 'USD', + 'currency' => + array( + 'base' => 'USD', + 'rate' => array( + ), + ), 'outboundEmailIsShared' => true, 'outboundEmailFromName' => 'EspoCRM', 'outboundEmailFromAddress' => '', @@ -60,9 +66,9 @@ return array ( 'logger' => array ( 'path' => 'data/logs/espo.log', - 'level' => 'ERROR', /*DEBUG, INFO, NOTICE, WARNING, ERROR, CRITICAL, ALERT, EMERGENCY*/ - 'isRotate' => true, /*rotate every day every logs files*/ - 'maxRotateFiles' => 30, /*max number of rotate files*/ + 'level' => 'ERROR', /** DEBUG, INFO, NOTICE, WARNING, ERROR, CRITICAL, ALERT, EMERGENCY */ + 'isRotate' => true, /** rotate log files every day */ + 'maxRotateFiles' => 30, /** max number of rotate files */ ), 'defaultPermissions' => array ( @@ -72,16 +78,9 @@ return array ( 'group' => '', ), 'cron' => array( - 'maxJobNumber' => 15, /*Max number of jobs per one execution*/ - 'jobPeriod' => 7800, /*Period for jobs, ex. if cron executed at 15:35, it will execute all pending jobs for times from 14:05 to 15:35*/ - 'minExecutionTime' => 50, /*to avoid too frequency execution*/ - ), - 'currency' => - array( - 'base' => 'USD', - 'rate' => array( - 'EUR' => 1.37, - ), + 'maxJobNumber' => 15, /** Max number of jobs per one execution */ + 'jobPeriod' => 7800, /** Period for jobs, ex. if cron executed at 15:35, it will execute all pending jobs for times from 14:05 to 15:35 */ + 'minExecutionTime' => 50, /** to avoid too frequency execution **/ ), 'globalSearchEntityList' => array ( @@ -91,6 +90,8 @@ return array ( 3 => 'Prospect', 4 => 'Opportunity', ), + "tabList" => array("Account", "Contact", "Lead", "Opportunity", "Calendar", "Meeting", "Call", "Task", "Case", "Prospect", "Email"), + "quickCreateList" => array("Account", "Contact", "Lead", "Opportunity", "Meeting", "Call", "Task", "Case", "Prospect"), 'crud' => array( 'get' => 'read', 'post' => 'create', @@ -116,7 +117,6 @@ return array ( 'isInstalled', 'defaultPermissions', 'systemUser', - 'rewriteOptions', ), 'adminItems' => array ( @@ -132,14 +132,6 @@ return array ( 'smtpPassword', 'cron', ), - 'rewriteOptions' => - array ( - 'tabList', - 'quickCreateList', - 'globalSearchEntityList', - ), - "tabList" => array("Account", "Contact", "Lead", "Opportunity", "Calendar", "Meeting", "Call", "Task", "Case", "Prospect", "Email"), - "quickCreateList" => array("Account", "Contact", "Lead", "Opportunity", "Meeting", "Call", "Task", "Case", "Prospect"), 'isInstalled' => false, ); diff --git a/install/core/Installer.php b/install/core/Installer.php index 66e2190776..6346bfcdf8 100644 --- a/install/core/Installer.php +++ b/install/core/Installer.php @@ -66,13 +66,20 @@ class Installer public function __construct() { $this->app = new \Espo\Core\Application(); - $this->writableList[] = $this->app->getContainer()->get('config')->get('configPath'); $user = $this->getEntityManager()->getEntity('User'); $this->app->getContainer()->setUser($user); require_once('install/core/SystemHelper.php'); $this->systemHelper = new SystemHelper(); + + $configPath = $this->getConfig()->getConfigPath(); + $this->writableList[] = $configPath; + + if (!file_exists($configPath)) { + $configData = $this->getConfig()->getDefaults(); + $this->getConfig()->set($configData); + } } protected function getContainer() @@ -85,6 +92,11 @@ class Installer return $this->getContainer()->get('entityManager'); } + protected function getConfig() + { + return $this->app->getContainer()->get('config'); + } + protected function getSystemHelper() { return $this->systemHelper; @@ -191,9 +203,13 @@ class Installer public function setPreferences($preferences) { - $currencyList = $this->app->getContainer()->get('config')->get('currencyList'); + $currencyList = $this->getConfig()->get('currencyList'); if (isset($preferences['defaultCurrency']) && !in_array($preferences['defaultCurrency'], $currencyList)) { + $preferences['currencyList'] = array($preferences['defaultCurrency']); + + $preferences['currency'] = $this->getConfig()->get('currency'); + $preferences['currency']['base'] = $preferences['defaultCurrency']; } $res = $this->saveConfig($preferences); diff --git a/tests/Espo/Core/Utils/ConfigTest.php b/tests/Espo/Core/Utils/ConfigTest.php index 00d61feafb..8602763d97 100644 --- a/tests/Espo/Core/Utils/ConfigTest.php +++ b/tests/Espo/Core/Utils/ConfigTest.php @@ -8,19 +8,19 @@ use tests\ReflectionHelper; class ConfigTest extends \PHPUnit_Framework_TestCase { protected $object; - + protected $objects; - protected $defaultConfigPath = 'tests/testData/Utils/Config/defaultConfig.php'; + protected $configPath = 'tests/testData/Utils/Config/config.php'; protected function setUp() - { - $this->objects['fileManager'] = new \Espo\Core\Utils\File\Manager(); + { + $this->objects['fileManager'] = new \Espo\Core\Utils\File\Manager(); $this->object = new \Espo\Core\Utils\Config($this->objects['fileManager']); - $this->reflection = new ReflectionHelper($this->object); - $this->reflection->setProperty('defaultConfigPath', $this->defaultConfigPath); + $this->reflection = new ReflectionHelper($this->object); + $this->reflection->setProperty('configPath', $this->configPath); } protected function tearDown() @@ -30,7 +30,7 @@ class ConfigTest extends \PHPUnit_Framework_TestCase function testLoadConfig() - { + { $this->assertArrayHasKey('database', $this->reflection->invokeMethod('loadConfig', array())); $this->assertArrayHasKey('dateFormat', $this->reflection->invokeMethod('loadConfig', array())); @@ -39,7 +39,7 @@ class ConfigTest extends \PHPUnit_Framework_TestCase function testGet() { $result = array( - 'driver' => 'mysqli', + 'driver' => 'pdo_mysql', 'host' => 'localhost', 'dbname' => 'espocrm', 'user' => 'root', @@ -47,14 +47,16 @@ class ConfigTest extends \PHPUnit_Framework_TestCase ); $this->assertEquals($result, $this->object->get('database')); - $result = 'mysqli'; + $result = 'pdo_mysql'; $this->assertEquals($result, $this->object->get('database.driver')); - $result = 'MM/DD/YYYY'; + $result = 'YYYY-MM-DD'; $this->assertEquals($result, $this->object->get('dateFormat')); + + $this->assertTrue($this->object->get('isInstalled')); } - + function testSet() { @@ -69,12 +71,10 @@ class ConfigTest extends \PHPUnit_Framework_TestCase function testSetArray() { - $this->reflection->setProperty('defaultConfigPath', 'tests/testData/Utils/Config/defaultConfigArray.php'); - $values = array( 'testOption' => 'Test', - 'testOption2' => 'Test2', - ); + 'testOption2' => 'Test2', + ); $this->assertTrue($this->object->set($values)); $this->assertEquals('Test', $this->object->get('testOption')); @@ -84,8 +84,6 @@ class ConfigTest extends \PHPUnit_Framework_TestCase 'testOption' => 'Another Wrong Value', ); $this->assertTrue($this->object->set($wrongArray)); - - $this->reflection->setProperty('defaultConfigPath', $this->defaultConfigPath); } } \ No newline at end of file diff --git a/tests/testData/Utils/Config/config.php b/tests/testData/Utils/Config/config.php index c2460e7f50..e0a3a722af 100644 --- a/tests/testData/Utils/Config/config.php +++ b/tests/testData/Utils/Config/config.php @@ -3,26 +3,21 @@ return array ( 'database' => array ( - 'driver' => 'mysqli', + 'driver' => 'pdo_mysql', 'host' => 'localhost', 'dbname' => 'espocrm', 'user' => 'root', 'password' => '', ), - 'logger' => - array ( - 'dir' => 'data/logs', - 'file' => 'espo.log', - 'level' => 'DEBUG', - ), 'useCache' => false, 'recordsPerPage' => 20, 'recordsPerPageSmall' => 5, 'applicationName' => 'EspoCRM', - 'timeZone' => '', - 'dateFormat' => 'MM/DD/YYYY', + 'version' => '1.0', + 'timeZone' => 'UTC', + 'dateFormat' => 'YYYY-MM-DD', 'timeFormat' => 'HH:mm', - 'weekStart' => '1', + 'weekStart' => 1, 'thousandSeparator' => ',', 'decimalMark' => '.', 'currencyList' => @@ -30,6 +25,126 @@ return array ( 0 => 'USD', 1 => 'EUR', ), + 'defaultCurrency' => 'USD', + 'currency' => + array ( + 'base' => 'USD', + 'rate' => + array ( + 'EUR' => '1.37', + ), + ), + 'outboundEmailIsShared' => true, + 'outboundEmailFromName' => 'EspoCRM', + 'outboundEmailFromAddress' => '', + 'smtpServer' => '', + 'smtpPort' => 25, + 'smtpAuth' => true, + 'smtpSecurity' => '', + 'smtpUsername' => '', + 'smtpPassword' => '', + 'languageList' => + array ( + 0 => 'en_US', + ), + 'language' => 'en_US', + 'logger' => + array ( + 'path' => 'data/logs/espo.log', + 'level' => 'INFO', + 'isRotate' => true, + 'maxRotateFiles' => 5, + ), + 'defaultPermissions' => + array ( + 'dir' => '0775', + 'file' => '0664', + 'user' => '', + 'group' => '', + ), + 'cron' => + array ( + 'maxJobNumber' => 15, + 'jobPeriod' => 7800, + 'minExecutionTime' => 50, + ), + 'globalSearchEntityList' => + array ( + 0 => 'Account', + 1 => 'Contact', + 2 => 'Lead', + 3 => 'Prospect', + 4 => 'Opportunity', + ), + 'tabList' => + array ( + 0 => 'Contact', + 1 => 'Account', + 2 => 'Lead', + 3 => 'Opportunity', + 4 => 'Calendar', + 5 => 'Meeting', + 6 => 'Call', + 7 => 'Task', + 8 => 'Case', + 9 => 'Prospect', + ), + 'quickCreateList' => + array ( + 0 => 'Account', + 1 => 'Contact', + 2 => 'Lead', + 3 => 'Opportunity', + 4 => 'Meeting', + 5 => 'Call', + 6 => 'Task', + 7 => 'Case', + 8 => 'Prospect', + ), + 'crud' => + array ( + 'get' => 'read', + 'post' => 'create', + 'put' => 'update', + 'patch' => 'patch', + 'delete' => 'delete', + ), + 'systemUser' => + array ( + 'id' => 'system', + 'userName' => 'system', + 'firstName' => '', + 'lastName' => 'System', + ), + 'systemItems' => + array ( + 0 => 'systemItems', + 1 => 'adminItems', + 2 => 'configPath', + 3 => 'cachePath', + 4 => 'database', + 5 => 'crud', + 6 => 'logger', + 7 => 'isInstalled', + 8 => 'defaultPermissions', + 9 => 'systemUser', + ), + 'adminItems' => + array ( + 0 => 'devMode', + 1 => 'outboundEmailIsShared', + 2 => 'outboundEmailFromName', + 3 => 'outboundEmailFromAddress', + 4 => 'smtpServer', + 5 => 'smtpPort', + 6 => 'smtpAuth', + 7 => 'smtpSecurity', + 8 => 'smtpUsername', + 9 => 'smtpPassword', + 10 => 'cron', + ), + 'isInstalled' => true, + 'cacheTimestamp' => 1399456816, 'testOption' => 'Another Wrong Value', 'testOption2' => 'Test2', );