Skip to content

Commit

Permalink
Merge pull request #11 from nevvermind/optional-bootpstrapping
Browse files Browse the repository at this point in the history
Create a new "extra" option flag to control patching of Mage.php (patching applied by default)
  • Loading branch information
Flyingmana committed Apr 29, 2015
2 parents fb8b1b6 + e8d2bac commit 1903484
Show file tree
Hide file tree
Showing 4 changed files with 171 additions and 57 deletions.
46 changes: 34 additions & 12 deletions src/MagentoHackathon/Composer/Magento/Patcher/Bootstrap.php
Original file line number Diff line number Diff line change
@@ -1,32 +1,54 @@
<?php
/**
*
*
*
*
*/

namespace MagentoHackathon\Composer\Magento\Patcher;

use MagentoHackathon\Composer\Magento\ProjectConfig;

class Bootstrap
{
protected $magentoBasePath;
/**
* @var ProjectConfig
*/
private $config;

public function __construct(ProjectConfig $config)
{
$this->config = $config;
}

public function __construct($magentoBasePath)
/**
* @return ProjectConfig
*/
private function getConfig()
{
$this->magentoBasePath = $magentoBasePath;
return $this->config;
}

private function canApplyPatch()
{
$mageClassPath = $this->getConfig()->getMagentoRootDir() . '/app/Mage.php';

return $this->getConfig()->mustApplyBootstrapPatch() &&
is_file($mageClassPath) &&
is_writable($mageClassPath);
}

/**
* @return bool
*/
public function patch()
{
$this->splitOriginalMage();
$this->generateBootstrapFile();
if ($this->canApplyPatch()) {
$this->splitOriginalMage();
$this->generateBootstrapFile();
return true;
}
return false;
}

protected function getAppPath()
{
return $this->magentoBasePath . '/app';
return $this->getConfig()->getMagentoRootDir() . '/app';
}

protected function splitOriginalMage()
Expand Down
15 changes: 6 additions & 9 deletions src/MagentoHackathon/Composer/Magento/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class Plugin implements PluginInterface, EventSubscriberInterface
* The type of packages this plugin supports
*/
const PACKAGE_TYPE = 'magento-module';

const VENDOR_DIR_KEY = 'vendor-dir';

const BIN_DIR_KEY = 'bin-dir';
Expand Down Expand Up @@ -93,7 +93,7 @@ protected function initDeployManager(Composer $composer, IOInterface $io, EventM

$this->applyEvents($eventManager);
}

protected function applyEvents(EventManager $eventManager)
{

Expand Down Expand Up @@ -141,7 +141,7 @@ public function activate(Composer $composer, IOInterface $io)
$this->config = new ProjectConfig($composer->getPackage()->getExtra(), $composer->getConfig()->all());

$this->veryfiyComposerRepositories();

$this->entryFactory = new EntryFactory(
$this->config,
new DeploystrategyFactory($this->config),
Expand Down Expand Up @@ -236,11 +236,8 @@ function (PackageInterface $package) use ($packageTypeToMatch) {
$moduleManager->updateInstalledPackages($magentoModules);
$this->deployLibraries();

if (file_exists($this->config->getMagentoRootDir() . '/app/Mage.php')) {
$patcher = new Bootstrap($this->config->getMagentoRootDir());
$patcher->patch();
}

$patcher = new Bootstrap($this->config);
$patcher->patch();
}

/**
Expand Down Expand Up @@ -272,7 +269,7 @@ protected function veryfiyComposerRepositories()
$this->io->write(sprintf($message1, 'packages.magento.com'));
$this->io->write(sprintf($message2, 'magento', 'https?://packages.magento.com'));
}

}

/**
Expand Down
24 changes: 17 additions & 7 deletions src/MagentoHackathon/Composer/Magento/ProjectConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ class ProjectConfig
// Default Values
const DEFAULT_MAGENTO_ROOT_DIR = 'root';

const EXTRA_WITH_BOOTSTRAP_PATCH_KEY = 'with-bootstrap-patch';

protected $libraryPath;
protected $libraryPackages;
protected $extra;
Expand All @@ -58,11 +60,11 @@ public function __construct(array $extra, array $composerConfig)
}

/**
* @param $array
* @param $key
* @param null $default
* @param array $array
* @param string|integer $key
* @param mixed $default
*
* @return null
* @return mixed
*/
protected function fetchVarFromConfigArray($array, $key, $default = null)
{
Expand Down Expand Up @@ -144,7 +146,7 @@ public function hasMagentoRootDir()
{
return $this->hasExtraField(self::MAGENTO_ROOT_DIR_KEY);
}

public function getMagentoVarDir()
{
return $this->getMagentoRootDir().'var'.DIRECTORY_SEPARATOR;
Expand Down Expand Up @@ -291,7 +293,7 @@ public function hasMagentoForce()
{
return $this->hasExtraField(self::MAGENTO_FORCE_KEY);
}

public function getMagentoForceByPackageName($packagename)
{
return $this->getMagentoForce();
Expand Down Expand Up @@ -422,7 +424,7 @@ public function transformArrayKeysToLowerCase(array $array)
{
return array_change_key_case($array, CASE_LOWER);
}

public function getComposerRepositories()
{
return $this->fetchVarFromConfigArray($this->composerConfig, 'repositories', array());
Expand All @@ -437,4 +439,12 @@ public function getVendorDir()
{
return $this->fetchVarFromConfigArray($this->composerConfig, 'vendor-dir', 'vendor');
}

/**
* @return boolean
*/
public function mustApplyBootstrapPatch()
{
return (bool) $this->fetchVarFromExtraConfig(self::EXTRA_WITH_BOOTSTRAP_PATCH_KEY, true);
}
}
143 changes: 114 additions & 29 deletions tests/MagentoHackathon/Composer/Magento/Patcher/BootstrapTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,65 +2,150 @@

namespace MagentoHackathon\Composer\Magento\Patcher;

use MagentoHackathon\Composer\Magento\ProjectConfig;
use org\bovigo\vfs\vfsStream;

/**
*
*/
class BootstrapTest extends \PHPUnit_Framework_TestCase
{

/**
*
* @dataProvider mageFileProvider
*
* @param $MageFile
* @param $mageFile
*/
public function testMageFilesExist($MageFile)
public function testMageFilesExist($mageFile)
{

$structure = array(
'app' => array(
'Mage.php' => file_get_contents($MageFile),
'Mage.php' => file_get_contents($mageFile),
),
);
$directory = vfsStream::setup('patcherMagentoBase', null, $structure);
$patcher = new Bootstrap(vfsStream::url('patcherMagentoBase'));
$patcher->patch();
$this->assertFileExists(vfsStream::url('patcherMagentoBase/app/Mage.php'));
$this->assertFileNotEquals(
$MageFile,
vfsStream::url('patcherMagentoBase/app/Mage.php'),
'File should be modified but its not'
vfsStream::setup('root', null, $structure);

$config = new ProjectConfig(
array(
ProjectConfig::EXTRA_WITH_BOOTSTRAP_PATCH_KEY => true,
ProjectConfig::MAGENTO_ROOT_DIR_KEY => vfsStream::url('root'),
),
array()
);
$this->assertFileExists(vfsStream::url('patcherMagentoBase/app/bootstrap.php'));
$this->assertFileExists(vfsStream::url('patcherMagentoBase/app/Mage.class.php'));
$this->assertFileExists(vfsStream::url('patcherMagentoBase/app/Mage.bootstrap.php'));
$this->assertFileNotExists(vfsStream::url('patcherMagentoBase/app/Mage.nonsense.php'));

$patcher = new Bootstrap($config);
$patcher->patch();

$this->assertBootstrapWasApplied($mageFile);
}

/**
* @dataProvider mageFileProvider
*
* Ensure that the Mage class is valid PHP
* @param string $MageFile
*
* @dataProvider mageFileProvider
* @param string $mageFile
* @return void
*/
public function testMageClassFile($MageFile)
public function testMageClassFile($mageFile)
{
$structure = array(
'app' => array(
'Mage.php' => file_get_contents($MageFile),
'Mage.php' => file_get_contents($mageFile),
),
);
$directory = vfsStream::setup('patcherMagentoBase', null, $structure);
$patcher = new Bootstrap(vfsStream::url('patcherMagentoBase'));
vfsStream::setup('root', null, $structure);

$config = new ProjectConfig(
array(
ProjectConfig::EXTRA_WITH_BOOTSTRAP_PATCH_KEY => true,
ProjectConfig::MAGENTO_ROOT_DIR_KEY => vfsStream::url('root'),
),
array()
);

$patcher = new Bootstrap($config);
$patcher->patch();

require vfsStream::url('patcherMagentoBase/app/Mage.class.php');
require vfsStream::url('root/app/Mage.class.php');
$this->assertTrue(class_exists('Mage'));
}

/**
* @dataProvider mageFileProvider
*/
public function testMageFileIsNotModifiedWhenThePatchingFeatureIsOff($mageFile)
{
$structure = array(
'app' => array(
'Mage.php' => file_get_contents($mageFile),
),
);
vfsStream::setup('root', null, $structure);

$config = new ProjectConfig(
array(
ProjectConfig::EXTRA_WITH_BOOTSTRAP_PATCH_KEY => false,
ProjectConfig::MAGENTO_ROOT_DIR_KEY => vfsStream::url('root'),
),
array()
);

$patcher = new Bootstrap($config);
$patcher->patch();

$this->assertBootstrapWasNotApplied($mageFile);
}

/**
* @dataProvider mageFileProvider
*/
public function testBootstrapPatchIsAppliedByDefault($mageFile)
{
$structure = array(
'app' => array(
'Mage.php' => file_get_contents($mageFile),
),
);
vfsStream::setup('root', null, $structure);

$config = new ProjectConfig(
// the patch flag is not declared on purpose
array(
ProjectConfig::MAGENTO_ROOT_DIR_KEY => vfsStream::url('root'),
),
array()
);

$patcher = new Bootstrap($config);
$patcher->patch();

$this->assertBootstrapWasApplied($mageFile);
}

private function assertBootstrapWasNotApplied($mageFile)
{
$this->assertFileExists(vfsStream::url('root/app/Mage.php'));
$this->assertFileEquals(
$mageFile,
vfsStream::url('root/app/Mage.php'),
'File should not be modified but it is'
);
$this->assertFileNotExists(vfsStream::url('root/app/bootstrap.php'));
$this->assertFileNotExists(vfsStream::url('root/app/Mage.class.php'));
$this->assertFileNotExists(vfsStream::url('root/app/Mage.bootstrap.php'));
$this->assertFileNotExists(vfsStream::url('root/app/Mage.nonsense.php'));
}

private function assertBootstrapWasApplied($mageFile)
{
$this->assertFileExists(vfsStream::url('root/app/Mage.php'));
$this->assertFileNotEquals(
$mageFile,
vfsStream::url('root/app/Mage.php'),
'File should be modified but its not'
);
$this->assertFileExists(vfsStream::url('root/app/bootstrap.php'));
$this->assertFileExists(vfsStream::url('root/app/Mage.class.php'));
$this->assertFileExists(vfsStream::url('root/app/Mage.bootstrap.php'));
$this->assertFileNotExists(vfsStream::url('root/app/Mage.nonsense.php'));
}

public function mageFileProvider()
{
$fixturesBasePath = __DIR__ . '/../../../../res/fixtures';
Expand Down

0 comments on commit 1903484

Please sign in to comment.