Skip to content

Commit

Permalink
Merge pull request #41 from OXIDprojects/feature/find_better_oxid_eshop
Browse files Browse the repository at this point in the history
Method improved to find the OXID eShop.
  • Loading branch information
TumTum committed May 4, 2019
2 parents 3992d95 + 645775e commit c4fc10a
Show file tree
Hide file tree
Showing 10 changed files with 404 additions and 61 deletions.
65 changes: 13 additions & 52 deletions src/Oxrun/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Composer\Autoload\ClassLoader;
use Oxrun\Command\Custom;
use Oxrun\Helper\BootstrapFinder;
use Oxrun\Helper\DatabaseConnection;
use Symfony\Component\Console\Application as BaseApplication;
use Symfony\Component\Console\Command\HelpCommand;
Expand Down Expand Up @@ -140,59 +141,11 @@ public function bootstrapOxid($blNeedDBConnection = true)
*/
protected function findBootstrapFile()
{
$input = new ArgvInput();
if ($input->getParameterOption('--shopDir')) {
$oxBootstrap = $input->getParameterOption('--shopDir'). '/bootstrap.php';
if ($this->checkBootstrapOxidInclude($oxBootstrap) === true) {
return true;
}
return false;
}

// try to guess where bootstrap.php is
$currentWorkingDirectory = getcwd();
do {
$oxBootstrap = $currentWorkingDirectory . '/bootstrap.php';
if ($this->checkBootstrapOxidInclude($oxBootstrap) === true) {
return true;
break;
}
$currentWorkingDirectory = dirname($currentWorkingDirectory);
} while ($currentWorkingDirectory !== '/');
return false;
}

/**
* Check if bootstrap file exists
*
* @param String $oxBootstrap Path to oxid bootstrap.php
* @param bool $skipViews Add 'blSkipViewUsage' to OXIDs config.
*
* @return bool
*/
public function checkBootstrapOxidInclude($oxBootstrap)
{
if (is_file($oxBootstrap)) {
// is it the oxid bootstrap.php?
if (strpos(file_get_contents($oxBootstrap), 'OX_BASE_PATH') !== false) {
$this->shopDir = dirname($oxBootstrap);
$realPath = (new \SplFileInfo($this->shopDir))->getRealPath();
if ($realPath) {
$this->shopDir = $realPath;
}

include_once $oxBootstrap;

// If we've an autoloader we must re-register it to avoid conflicts with a composer autoloader from shop
if (null !== $this->autoloader) {
$this->autoloader->unregister();
$this->autoloader->register(true);
}

return true;
}
$bootstrapFinder = new BootstrapFinder($this->autoloader);
if ($bootstrapFinder->isFound()) {
$this->setShopDir($bootstrapFinder->getShopDir());
return true;
}

return false;
}

Expand All @@ -219,6 +172,14 @@ public function getShopDir()
return $this->shopDir;
}

/**
* @param string $shopDir
*/
public function setShopDir($shopDir)
{
$this->shopDir = $shopDir;
}

/**
* @return bool
*/
Expand Down
191 changes: 191 additions & 0 deletions src/Oxrun/Helper/BootstrapFinder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
<?php
/**
* Created by PhpStorm.
* Autor: Tobias Matthaiou
* Date: 2019-05-03
* Time: 11:30
*/

namespace Oxrun\Helper;

use Symfony\Component\Console\Input\ArgvInput;

/**
* Class BootstrapFinder
* @package Oxrun\Helper
*/
class BootstrapFinder
{
/**
* @var \Composer\Autoload\ClassLoader
*/
private $autoloader;

/**
* @var string
*/
private $shopDir;

/**
* @inheritDoc
*/
public function __construct($autoloader)
{
$this->autoloader = $autoloader;
}

/**
* @return bool
*/
public function isFound()
{
$findByArgument = $this->findByArgument();
if ($findByArgument !== null) {
return $findByArgument;
}

$findByEnv = $this->findByEnvironmentVariable();
if ($findByEnv !== null) {
return $findByEnv;
}

$findAsPackage = $this->findAsOxidPackage();
if ($findAsPackage !== null) {
return $findAsPackage;
}

return $this->searchingBootstrap(getcwd());
}

/**
* Prüft anhand des --shopDir Argument ob der Shop vorhanden ist
*
* Wenn der Rückgabe wert null ist dann wurde kein Argument angegeben.
*
* @return bool|null
*/
public function findByArgument()
{
$input = new ArgvInput();
if ($input->getParameterOption('--shopDir') == '') {
return null;
}

//start from oxid install root folder.
$oxBootstrap = $input->getParameterOption('--shopDir') . '/source/bootstrap.php';
if ($this->checkBootstrapAndInclude($oxBootstrap) === true) {
return true;
}

//maybe is source folder.
$oxBootstrap = $input->getParameterOption('--shopDir') . '/bootstrap.php';
if ($this->checkBootstrapAndInclude($oxBootstrap) === true) {
return true;
}

return false;
}

/**
* @return bool|null
*/
protected function findByEnvironmentVariable()
{
$oxid_dir = getenv('OXID_SHOP_DIR');

if ($oxid_dir == '') {
return null;
}

return $this->searchingBootstrap($oxid_dir);
}

/**
* If oxrun installed in the same composer package as oxid eshop.
* So the path to bootstrap.php is logic.
*
* @return bool|null
*/
public function findAsOxidPackage()
{
if (\Phar::running() != '') {
return null; // oxrun is a phar script
}

//hard core place /vendor/oxidprojects/oxrun/src/Oxrun/Helper/./source/bootstrap.php
$standardOxidPath = __DIR__ . '/../../../../../../source/bootstrap.php';

if (!is_file($standardOxidPath)) {
return null;
}

return $this->checkBootstrapAndInclude($standardOxidPath);
}

/**
* Check if bootstrap file exists
*
* @param String $oxBootstrap Path to oxid bootstrap.php
*
* @return bool
*/
protected function checkBootstrapAndInclude($oxBootstrap)
{
if (is_file($oxBootstrap) == false) {
return false;
}

// is that oxid bootstrap.php file?
if (strpos(file_get_contents($oxBootstrap), 'OX_BASE_PATH') === false) {
return false;
}

$this->shopDir = dirname($oxBootstrap);
$realPath = (new \SplFileInfo($this->shopDir))->getRealPath();
if ($realPath) {
$this->shopDir = $realPath;
}

include_once $oxBootstrap;

// If we've an autoloader we must re-register it to avoid conflicts with a composer autoloader from shop
if (null !== $this->autoloader) {
$this->autoloader->unregister();
$this->autoloader->register(true);
}

return true;
}

/**
* Search in Folder and backwards
* @return bool
*/
protected function searchingBootstrap($currentWorkingDirectory)
{
//start from oxid install root folder.
$oxBootstrap = $currentWorkingDirectory . '/source/bootstrap.php';
if ($this->checkBootstrapAndInclude($oxBootstrap) === true) {
return true;
}

//try backwards to find bootstrap.
do {
$oxBootstrap = $currentWorkingDirectory . '/bootstrap.php';
if ($this->checkBootstrapAndInclude($oxBootstrap) === true) {
return true;
}
$currentWorkingDirectory = dirname($currentWorkingDirectory);
} while ($currentWorkingDirectory !== '/');

return false;
}

/**
* @return string
*/
public function getShopDir()
{
return $this->shopDir;
}
}
12 changes: 6 additions & 6 deletions tests/Oxrun/Command/Misc/GenerateYamlConfigCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ public function testExportListOfVariabels()

$dev_yml = ['config' => ['1' => ['varA' => 'besteht']]];
$shopDir = ['oxrun_config' => ['dev.yaml' => Yaml::dump($dev_yml)]];
$app->checkBootstrapOxidInclude($this->fillShopDir($shopDir)->getVirtualBootstrap());

$app->setShopDir($this->fillShopDir($shopDir)->getVirtualBootstrap());

$command = $app->find('misc:generate:yaml:config');

Expand Down Expand Up @@ -84,12 +83,11 @@ public function testExportModullVariable()
{
$app = new Application();
$app->add(new EnableAdapter(new GenerateYamlConfigCommand()));
$app->setShopDir($this->fillShopDir([])->getVirtualBootstrap());

Registry::getConfig()->saveShopConfVar('str', 'unitModuleB', 'abcd1', 1, 'module:unitTest');
Registry::getConfig()->saveShopConfVar('str', 'unitModuleW', 'cdef1', 1, 'module:unitNext');

$app->checkBootstrapOxidInclude($this->fillShopDir([])->getVirtualBootstrap());

$command = $app->find('misc:generate:yaml:config');

$commandTester = new CommandTester($command);
Expand Down Expand Up @@ -123,11 +121,11 @@ public function testExportModulVariableNameAndShop2()
{
$app = new Application();
$app->add(new EnableAdapter(new GenerateYamlConfigCommand()));
$app->setShopDir($this->fillShopDir([])->getVirtualBootstrap());

Registry::getConfig()->saveShopConfVar('str', 'unitSecondShopName', 'Mars', 2, 'module:unitMars');
Registry::getConfig()->saveShopConfVar('str', 'unitEgal', 'none', 2, 'module:unitMars');

$app->checkBootstrapOxidInclude($this->fillShopDir([])->getVirtualBootstrap());

$command = $app->find('misc:generate:yaml:config');

Expand Down Expand Up @@ -159,11 +157,12 @@ public function testExportModullVariableOnlyModulname()
{
$app = new Application();
$app->add(new EnableAdapter(new GenerateYamlConfigCommand()));
$app->setShopDir($this->fillShopDir([])->getVirtualBootstrap());


Registry::getConfig()->saveShopConfVar('str', 'unitModuleB', 'abcd1', 1, 'module:myModuleName');
Registry::getConfig()->saveShopConfVar('str', 'unitModuleZ', 'abcd2', 1, 'module:myModuleOption');

$app->checkBootstrapOxidInclude($this->fillShopDir([])->getVirtualBootstrap());

$command = $app->find('misc:generate:yaml:config');

Expand Down Expand Up @@ -201,6 +200,7 @@ protected function tearDown()
}

DatabaseProvider::getDb()->execute('DELETE FROM `oxconfig` WHERE `OXVARNAME` LIKE "unit%"');

parent::tearDown();
}
}
5 changes: 3 additions & 2 deletions tests/Oxrun/Command/Misc/GenerateYamlModuleCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ public function testExportWhitelist()

//Arrange
$shopDir['oxrun_config']['dev.yml'] = Yaml::dump(['blacklist' => ['1' => ['a','b','c']], 'whitelist' => ['2' => ['BleibtA']]]);
$app->checkBootstrapOxidInclude($this->fillShopDir($shopDir)->getVirtualBootstrap());
$app->setShopDir($this->fillShopDir($shopDir)->getVirtualBootstrap());


$moduleList = $this->prophesize(ModuleList::class);
$moduleList->getActiveModuleInfo()->willReturn(['ModuleA' => null, 'ModuleB' => null, 'ModuleC' => null]);
Expand Down Expand Up @@ -75,7 +76,7 @@ public function testExportBlacklist()

//Arrange
$shopDir['oxrun_config']['dev.yaml'] = Yaml::dump(['blacklist' => ['1' => ['a','b','c']], 'whitelist' => ['2' => ['BleibtA']]]);
$app->checkBootstrapOxidInclude($this->fillShopDir($shopDir)->getVirtualBootstrap());
$app->setShopDir($this->fillShopDir($shopDir)->getVirtualBootstrap());

$moduleList = $this->prophesize(ModuleList::class);
$moduleList->getDisabledModules()->willReturn(['ModuleA', 'ModuleB']);
Expand Down

0 comments on commit c4fc10a

Please sign in to comment.