diff --git a/src/Application.php b/src/Application.php index dde3670..093c456 100644 --- a/src/Application.php +++ b/src/Application.php @@ -35,7 +35,7 @@ class Application */ public function __construct($path) { - $this->path = realpath($path); + $this->path = realpath($path) ?: $path; } /** @@ -93,8 +93,8 @@ public function getSugarConfig($clearCache = false) $this->clearConfigCache(); } if ($this->config == null) { - $path = $this->path; - if ($this->isValid($path) and is_file($path . '/config.php')) { + $path = $this->getPath(); + if ($this->isValid() and is_file($path . '/config.php')) { require($path . '/config.php'); if (!isset($sugar_config) or !is_array($sugar_config)) { throw new SugarException("Invalid sugarcrm configuration file at '$path/config.php'"); diff --git a/src/EntryPoint.php b/src/EntryPoint.php index 4142117..382480a 100644 --- a/src/EntryPoint.php +++ b/src/EntryPoint.php @@ -33,12 +33,12 @@ class EntryPoint protected $log; /** - * SugarCRM Directory + * SugarCRM Application * @var string */ - protected $sugarDir; + protected $sugarApp; /** - * Last Current working directory before changing to sugarDir + * Last Current working directory before changing to getPath() * @var string */ protected $lastCwd; @@ -80,31 +80,39 @@ class EntryPoint /** * Constructor, to get the Container, then the log and config * @param LoggerInterface $log Allow any logger extended from PSR\Log - * @param string $sugarDir + * @param Application $sugarApp * @param string $sugarUserId */ - private function __construct(LoggerInterface $logger, $sugarDir, $sugarUserId) + private function __construct(LoggerInterface $logger, Application $sugarApp, $sugarUserId) { $this->logPrefix = __CLASS__ . ': '; $this->log = $logger; - $this->sugarDir = realpath($sugarDir); + $this->sugarApp = $sugarApp; $this->sugarUserId = $sugarUserId; } + /** + * @return true if the EntryPoint instances is already created. + */ + public static function isCreated() + { + return !is_null(self::$instance); + } + /** * Create the singleton instance only if it doesn't exists already. * @param LoggerInterface $log Allow any logger extended from PSR\Log - * @param string $sugarDir + * @param Application $sugarApp * @param string $sugarUserId * @throws \RuntimeException */ - public static function createInstance(LoggerInterface $logger, $sugarDir, $sugarUserId) + public static function createInstance(LoggerInterface $logger, Application $sugarApp, $sugarUserId) { if (!is_null(self::$instance)) { throw new \RuntimeException('Unable to create a SugarCRM\EntryPoint more than once.'); } - $instance = new EntryPoint($logger, $sugarDir, $sugarUserId); + $instance = new EntryPoint($logger, $sugarApp, $sugarUserId); $instance->initSugar(); self::$instance = $instance; } @@ -134,16 +142,25 @@ public function getLogger() } /** - * Returns the Sugar Dir where the entryPoint entered + * Returns the Sugar Application used by this EntryPoint * @return string */ - public function getSugarDir() + public function getApplication() { - return $this->sugarDir; + return $this->sugarApp; } /** - * Returns the last working directory before moving to sugarDir + * Alias for $this->getApplication()->getPath() + * @return string Sugar path. + */ + public function getPath() + { + return $this->getApplication()->getPath(); + } + + /** + * Returns the last working directory before moving to getPath() * @return string */ public function getLastCwd() @@ -152,7 +169,7 @@ public function getLastCwd() } /** - * Returns the Sugar Dir where the entryPoint entered + * Returns the SugarDb of this instance * @return mysqli */ public function getSugarDb() @@ -169,6 +186,23 @@ public function getCurrentUser() return $this->currentUser; } + /** + * Set the SugarCRM current user. This user will be used for all remaining operation. + * @param string $sugarUserId Database id of the sugar crm user. + */ + public function setCurrentUser($sugarUserId) + { + // Retrieve my User + $current_user = new \User; + $current_user = $current_user->retrieve($sugarUserId); + if (empty($current_user)) { + throw new \InvalidArgumentException('Wrong User ID: ' . $sugarUserId); + } + $this->currentUser = $current_user; + $this->sugarUserId = $sugarUserId; + $this->log->info($this->logPrefix . "Changed current user to {$current_user->full_name}."); + } + /** * Returns the List Of Beans * @return array @@ -188,7 +222,7 @@ private function initSugar() } $this->chdirToSugarDir(); $this->loadSugarEntryPoint(); - $this->setSugarUser($this->sugarUserId); + $this->setCurrentUser($this->sugarUserId); $this->getSugarGlobals(); } @@ -198,11 +232,11 @@ private function initSugar() */ private function chdirToSugarDir() { - if (!file_exists($this->sugarDir . '/include/entryPoint.php')) { - throw new \InvalidArgumentException('Wrong SugarCRM folder: ' . $this->sugarDir, 1); + if (!$this->getApplication()->isInstalled()) { + throw new SugarException('Unable to find an installed instance of SugarCRM in :' . $this->getPath(), 1); } $this->lastCwd = realpath(getcwd()); - @chdir($this->sugarDir); + @chdir($this->getPath()); } private function loadSugarEntryPoint() @@ -223,6 +257,9 @@ private function loadSugarEntryPoint() global $sugar_config, $current_user, $system_config, $beanList, $app_list_strings; global $timedate, $current_entity, $locale, $current_language; + // Sugar will not reload config.php so we need to make sure the $sugar_config global is set properly. + $sugar_config = $this->getApplication()->getSugarConfig(true); + // 2. Get the "autoloader" require_once('include/entryPoint.php'); @@ -234,23 +271,6 @@ private function loadSugarEntryPoint() ); } - /** - * Set the SugarCRM current user. This user will be used for all remaining operation. - * @param string $sugarUserId Database id of the sugar crm user. - */ - public function setSugarUser($sugarUserId) - { - // Retrieve my User - $current_user = new \User; - $current_user = $current_user->retrieve($sugarUserId); - if (empty($current_user)) { - throw new \InvalidArgumentException('Wrong User ID: ' . $sugarUserId); - } - $this->currentUser = $current_user; - $this->sugarUserId = $sugarUserId; - $this->log->info($this->logPrefix . "Changed current user to {$current_user->full_name}."); - } - /** * Get some GLOBALS variables from the instance * (such as log, directly saved as $GLOBALS['log'] are not kept correctly) @@ -277,7 +297,7 @@ private function defineVariablesAsGlobal(array $variables, array $ignoreVariable array('_GET', '_POST', '_COOKIE', '_FILES', 'argv', 'argc', '_SERVER', 'GLOBALS', '_ENV', '_REQUEST') ); - if (!array_key_exists($this->sugarDir, $this->globals)) { + if (!array_key_exists($this->getPath(), $this->globals)) { $this->globals = array(); } diff --git a/tests/EntryPointTest.php b/tests/EntryPointTest.php index f6b5ad2..f81845c 100644 --- a/tests/EntryPointTest.php +++ b/tests/EntryPointTest.php @@ -1,20 +1,24 @@ getLastCwd(); $expectedSugarDir = realpath($lastCwd . '/' . $expectedSugarDir); } - $sugarDir = $entryPoint->getSugarDir(); + $sugarDir = $entryPoint->getPath(); $this->assertEquals($expectedSugarDir, $sugarDir); $sugarDB = $entryPoint->getSugarDb(); $this->assertInstanceOf('\MysqliManager', $sugarDB); + $this->assertInstanceOf('\Inet\SugarCRM\Application', $entryPoint->getApplication()); + $currentUser = $entryPoint->getCurrentUser(); $this->assertInstanceOf('\User', $currentUser); @@ -50,20 +56,14 @@ public function testGettersSetters() public function testSetBadUser() { $entryPoint = $this->getEntryPointInstance(); - $entryPoint->setSugarUser('foo'); + $entryPoint->setCurrentUser('foo'); } public function testGetInstance() { chdir(__DIR__); $entryPoint = $this->getEntryPointInstance(); - $this->assertEquals($entryPoint->getSugarDir(), getcwd()); + $this->assertEquals($entryPoint->getPath(), getcwd()); $this->assertEquals(__DIR__, $entryPoint->getLastCwd()); } - - public function tearDown() - { - // Make sure sugar is not running from local dir - $this->assertFileNotExists(__DIR__ . '/../cache'); - } } diff --git a/tests/SugarTestCase.php b/tests/SugarTestCase.php index 80b4bde..dea3aea 100644 --- a/tests/SugarTestCase.php +++ b/tests/SugarTestCase.php @@ -2,6 +2,7 @@ namespace Inet\SugarCRM\Tests; +use Inet\SugarCRM\Application; use Inet\SugarCRM\EntryPoint; use Psr\Log\NullLogger; @@ -9,12 +10,17 @@ class SugarTestCase extends \PHPUnit_Framework_TestCase { public function getEntryPointInstance() { - try { + if (!EntryPoint::isCreated()) { $logger = new NullLogger; - EntryPoint::createInstance($logger, getenv('sugarDir'), getenv('sugarUserId')); + EntryPoint::createInstance($logger, new Application(getenv('sugarDir')), getenv('sugarUserId')); $this->assertInstanceOf('Inet\SugarCRM\EntryPoint', EntryPoint::getInstance()); - } catch (\RuntimeException $e) { } return EntryPoint::getInstance(); } + + public function tearDown() + { + // Make sure sugar is not running from local dir + $this->assertFileNotExists(__DIR__ . '/../cache'); + } }