Skip to content

Commit

Permalink
Make EntryPoint use Application.
Browse files Browse the repository at this point in the history
  • Loading branch information
Rémi Sauvat committed Aug 28, 2015
1 parent eadfb2f commit e76355b
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 54 deletions.
6 changes: 3 additions & 3 deletions src/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class Application
*/
public function __construct($path)
{
$this->path = realpath($path);
$this->path = realpath($path) ?: $path;
}

/**
Expand Down Expand Up @@ -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'");
Expand Down
92 changes: 56 additions & 36 deletions src/EntryPoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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()
Expand All @@ -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()
Expand All @@ -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
Expand All @@ -188,7 +222,7 @@ private function initSugar()
}
$this->chdirToSugarDir();
$this->loadSugarEntryPoint();
$this->setSugarUser($this->sugarUserId);
$this->setCurrentUser($this->sugarUserId);
$this->getSugarGlobals();
}

Expand All @@ -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()
Expand All @@ -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');

Expand All @@ -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)
Expand All @@ -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();
}

Expand Down
24 changes: 12 additions & 12 deletions tests/EntryPointTest.php
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
<?php
namespace Inet\SugarCRM\Tests;

use Inet\SugarCRM\Application;
use Inet\SugarCRM\EntryPoint;
use Psr\Log\NullLogger;

/**
* @group sugarcrm
*/
class EntryPointTest extends SugarTestCase
{
/** Define a wrong folder: exception thrown
* @expectedException InvalidArgumentException
* @expectedExceptionMessageRegExp #Wrong SugarCRM folder: /foo#
* @expectedException \Inet\SugarCRM\SugarException
* @expectedExceptionMessageRegExp #Unable to find an installed instance of SugarCRM in :/foo#
* @runInSeparateProcess
*/
public function testWrongInstanciationBadFolder()
{
$logger = new NullLogger;
EntryPoint::createInstance($logger, '/foo', getenv('sugarUserId'));
EntryPoint::createInstance($logger, new Application('/foo'), '1');
}

public function testGettersSetters()
Expand All @@ -29,12 +33,14 @@ public function testGettersSetters()
$lastCwd = $entryPoint->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);

Expand All @@ -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');
}
}
12 changes: 9 additions & 3 deletions tests/SugarTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,25 @@

namespace Inet\SugarCRM\Tests;

use Inet\SugarCRM\Application;
use Inet\SugarCRM\EntryPoint;
use Psr\Log\NullLogger;

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');
}
}

0 comments on commit e76355b

Please sign in to comment.