Skip to content
Browse files

[FEATURE] Changed boostrapping

Bootstrap returns now the complete Application object instead of the autoloader.
If you need the autoloader you can get it from Application object by using method getAutoloader.
  • Loading branch information...
1 parent 43ba827 commit b80192e9caf8b50ce58d8ee505533bbff5171750 @cmuench cmuench committed Apr 13, 2013
View
5 _cli_stub.php
@@ -3,9 +3,8 @@
Phar::mapPhar('n98-magerun.phar');
-$autoloader = require_once 'phar://n98-magerun.phar/src/bootstrap.php';
-
-$application = new N98\Magento\Application($autoloader, true);
+$application = require_once 'phar://n98-magerun.phar/src/bootstrap.php';
+$application->setPharMode(true);
$application->run();
__HALT_COMPILER();
View
4 bin/n98-magerun
@@ -1,9 +1,7 @@
#!/usr/bin/env php
<?php
-require __DIR__.'/../src/bootstrap.php';
-
-$application = new N98\Magento\Application($loader);
+$application = require __DIR__.'/../src/bootstrap.php';
$application->run();
__HALT_COMPILER();
View
3 config.yaml
@@ -1,4 +1,7 @@
commands:
+ customCommands: []
+ aliases: []
+
N98\Magento\Command\Installer\InstallCommand:
magento-packages:
- name: magento-ce-1.6.2.0
View
4 phpunit.xml
@@ -2,7 +2,7 @@
bootstrap="tests/bootstrap.php"
backupStaticAttributes="false"
cacheTokens="false"
- colors="false"
+ colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
@@ -16,7 +16,7 @@
stopOnSkipped="false"
testSuiteLoaderClass="PHPUnit_Runner_StandardTestSuiteLoader"
strict="false"
- verbose="false ">
+ verbose="true">
<testsuites>
<testsuite name="n98-magerun Tests">
<directory>./tests</directory>
View
63 src/N98/Magento/Application.php
@@ -122,7 +122,7 @@ class Application extends BaseApplication
/**
* @var array
*/
- protected $config;
+ protected $config = array();
/**
* @var string
*/
@@ -142,31 +142,18 @@ class Application extends BaseApplication
protected $_isPharMode = false;
/**
+ * @var bool
+ */
+ protected $_isInitialized = false;
+
+ /**
* @param \Composer\Autoload\ClassLoader $autoloader
* @param bool $isPharMode
*/
- public function __construct($autoloader = null, $isPharMode = false)
+ public function __construct($autoloader = null)
{
$this->autoloader = $autoloader;
-
parent::__construct(self::APP_NAME, self::APP_VERSION);
-
- // Suppress DateTime warnings
- date_default_timezone_set(@date_default_timezone_get());
-
- $this->setPharMode($isPharMode);
- $this->detectMagento();
-
- $configLoader = new ConfigurationLoader($this->_magentoRootFolder);
- $this->config = $configLoader->toArray();
-
- $this->registerHelpers();
- if ($autoloader) {
- $this->registerCustomAutoloaders();
- $this->registerCustomCommands();
- }
-
- $this->registerCommands();
}
/**
@@ -250,9 +237,8 @@ protected function registerCustomAutoloaders()
protected function registerCustomCommands()
{
- if (isset($this->config['commands']['customCommands']) && is_array(
- $this->config['commands']['customCommands']
- )
+ if (isset($this->config['commands']['customCommands'])
+ && is_array($this->config['commands']['customCommands'])
) {
foreach ($this->config['commands']['customCommands'] as $commandClass) {
$this->add(new $commandClass);
@@ -503,6 +489,8 @@ public static function getAbbreviations($names)
*/
public function run(InputInterface $input = null, OutputInterface $output = null)
{
+ $this->init();
+
$return = parent::run($input, $output);
// Fix for no return values -> used in interactive shell to prevent error output
@@ -595,5 +583,34 @@ protected function registerCommands()
}
}
+ /**
+ * @param array $initConfig
+ * @return array
+ */
+ protected function _loadConfig($initConfig)
+ {
+ $configLoader = new ConfigurationLoader($initConfig, $this->_magentoRootFolder);
+
+ return $configLoader->toArray();
+ }
+
+ public function init()
+ {
+ if (!$this->_isInitialized) {
+ // Suppress DateTime warnings
+ date_default_timezone_set(@date_default_timezone_get());
+
+ $this->detectMagento();
+ $this->config = $this->_loadConfig($this->config);
+ $this->registerHelpers();
+ if ($this->autoloader) {
+ $this->registerCustomAutoloaders();
+ $this->registerCustomCommands();
+ }
+ $this->registerCommands();
+
+ $this->_isInitialized = true;
+ }
+ }
}
View
4 src/N98/Magento/Command/ConfigurationLoader.php
@@ -17,11 +17,13 @@ class ConfigurationLoader
protected $_customConfigFilename = 'n98-magerun.yaml';
/**
+ * @param array $initConfig
* @param string $magentoRootFolder
*/
- public function __construct($magentoRootFolder)
+ public function __construct($initConfig, $magentoRootFolder)
{
$config = Yaml::parse(__DIR__ . '/../../../../config.yaml');
+ $config = $this->mergeArrays($config, $initConfig);
// Check if there is a global config file in /etc folder
$systemWideConfigFile = '/etc/' . $this->_customConfigFilename;
View
1 src/N98/Magento/Command/PHPUnit/TestCase.php
@@ -24,6 +24,7 @@ public function getApplication()
}
$this->application = $this->getMock('N98\Magento\Application', array('getMagentoRootFolder', 'detectMagento'));
+ $this->application->init();
$this->application->expects($this->any())->method('getMagentoRootFolder')->will($this->returnValue($root));
}
View
4 src/bootstrap.php
@@ -13,4 +13,6 @@ function includeIfExists($file)
'php composer.phar install'.PHP_EOL);
}
-return $loader;
+$application = new \N98\Magento\Application($loader);
+
+return $application;
View
57 tests/N98/Magento/ApplicationTest.php
@@ -0,0 +1,57 @@
+<?php
+
+namespace N98\Magento;
+
+use Symfony\Component\Console\Input\StringInput;
+use Symfony\Component\Console\Output\NullOutput;
+use Symfony\Component\Console\Tester\CommandTester;
+use N98\Magento\Command\PHPUnit\TestCase;
+use N98\Magento\Application;
+
+class ApplicationTest extends TestCase
+{
+ public function testExecute()
+ {
+ /**
+ * Check autoloading
+ */
+ $application = require __DIR__ . '/../../../src/bootstrap.php';
+ /* @var $application Application */
+ $this->assertInstanceOf('\N98\Magento\Application', $application);
+ $loader = $application->getAutoloader();
+ $this->assertInstanceOf('\Composer\Autoload\ClassLoader', $loader);
+
+ /* @var $loader \Composer\Autoload\ClassLoader */
+ $prefixes = $loader->getPrefixes();
+ $this->assertArrayHasKey('N98', $prefixes);
+
+ $configArray = array(
+ 'autoloaders' => array(
+ 'N98MagerunTest' => __DIR__ . '/_ApplicationTestSrc',
+ ),
+ 'commands' => array(
+ 'customCommands' => array(
+ 0 => 'N98MagerunTest\TestDummyCommand'
+ ),
+ 'aliases' => array(
+ array(
+ 'cf' => 'cache:flush'
+ )
+ ),
+ ),
+ );
+ $application->setConfig($configArray);
+ $application->setAutoExit(false);
+ $application->run(new StringInput('list'), new NullOutput());
+
+ // Check if autoloaders, commands and aliases are registered
+ $prefixes = $loader->getPrefixes();
+ $this->assertArrayHasKey('N98MagerunTest', $prefixes);
+
+ $testDummyCommand = $application->find('n98mageruntest:test:dummy');
+ $this->assertInstanceOf('\N98MagerunTest\TestDummyCommand', $testDummyCommand);
+
+ // check alias
+ $this->assertInstanceOf('\N98\Magento\Command\Cache\FlushCommand', $application->find('cf'));
+ }
+}

0 comments on commit b80192e

Please sign in to comment.
Something went wrong with that request. Please try again.