Skip to content
Browse files

You can now specify --store [storeid] or --website [id]. The specifie…

…d scope code and id will be used to initialize Magento. This happens on a global level, outside of the plugin.

Moved argument parsing into the main Wiz class and cleaned up plugins' usage of arguments.
Made the initial wiz.php use the singleton for Wiz instead of static functions.
  • Loading branch information...
1 parent e81b14c commit 3f6fbabcc1c4b6188d4bdc1bd9aa1b33dd65840c Nicholas Vahalik committed Mar 29, 2011
Showing with 86 additions and 81 deletions.
  1. +78 −48 app/Wiz.php
  2. +1 −7 app/plugins/301.php
  3. +5 −8 app/plugins/Config.php
  4. +1 −16 app/plugins/Magento.php
  5. +0 −1 app/plugins/Sql.php
  6. +1 −1 wiz.php
View
126 app/Wiz.php
@@ -34,7 +34,7 @@
*/
class Wiz {
- const WIZ_VERSION = '0.8.2-beta';
+ const WIZ_VERSION = '0.9.0-beta';
public static function getWiz() {
static $_wiz;
@@ -48,17 +48,22 @@ public static function getVersion() {
return Wiz::WIZ_VERSION;
}
- public static function getPlugin($code) {
-
- }
-
function __construct($args = null) {
$this->pluginDirectory = dirname(__FILE__). WIZ_DS . 'plugins';
$this->_findPlugins();
+ $this->_parseArgs();
+ }
+
+ static function getUserConfig() {
+ $options = array();
+ if (file_exists($_SERVER['HOME'].'/.wizrc')) {
+ parse_str(strtr(file_get_contents($_SERVER['HOME'].'/.wizrc'), PHP_EOL, '&'), $options);
+ }
+ return $options;
}
- function getAllParameters() {
- return $this->getCoreCommandLineArgs();
+ static function setUserConfig($options) {
+ file_put_contents(realpath($_SERVER['HOME'].'/.wizrc'), rawurldecode(http_build_query($options, '', PHP_EOL)));
}
static function getMagentoRoot() {
@@ -81,11 +86,28 @@ static function getMagentoRoot() {
return $wizMagentoRoot;
}
+ /**
+ * Instantiates and sets up Magento. By default, use the admin scopeCode so we run
+ * inside of the administration context.
+ *
+ * @param string $scopeCode
+ * @param string $scopeId
+ * @return Mage_Core_Model_App
+ * @author Nicholas Vahalik <nick@classyllama.com>
+ */
public static function getMagento($scopeCode = 'admin', $scopeId = 'store') {
+
+ /**
+ * Our local copy of the Magento Application Object
+ *
+ * @see Mage_Core_Model_App
+ */
static $_magento;
+
if (!$_magento) {
// Did we get a directory from an environment variable?
$wizMagentoRoot = Wiz::getMagentoRoot();
+
// No dice. :-(
if ($wizMagentoRoot === FALSE) {
die ('Please specify a Magento root directory by setting WIZ_MAGE_ROOT.'.PHP_EOL);
@@ -105,6 +127,16 @@ public static function getMagento($scopeCode = 'admin', $scopeId = 'store') {
umask(0);
+ // If someone passes a scope code via he CLI, then use that.
+ foreach (array('store', 'website') as $scope) {
+ if (($argScopeCode = Wiz::getWiz()->getArg($scope)) !== FALSE) {
+ // If --store is specified, but not provided, use the default.
+ $scopeCode = $argScopeCode === TRUE ? '' : $argScopeCode;
+ $scopeId = $scope;
+ break;
+ }
+ }
+
$_magento = Mage::app($scopeCode, $scopeId);
}
return $_magento;
@@ -123,7 +155,7 @@ private function _findPlugins() {
foreach ($pluginFiles as $file) {
$fileExtension = substr($file->getFilename(), -3);
if ($file->isFile() && $fileExtension == "php") {
- include($file->getPathname());
+ require($file->getPathname());
$plugins[] = basename($file->getFilename(), '.php');
}
}
@@ -206,7 +238,7 @@ function _initWiz() {
public function run() {
$argv = $_SERVER['argv'];
- // var_dump($argv);
+
array_shift($argv);
$command = array_shift($argv);
if (array_key_exists($command, $this->_availableCommands)) {
@@ -246,6 +278,42 @@ public static function inspect() {
}
/**
+ * Parse input arguments
+ *
+ * @return Wiz_Plugin_Abstract
+ */
+ protected function _parseArgs() {
+ $current = null;
+ foreach ($_SERVER['argv'] as $arg) {
+ $match = array();
+ if (preg_match('#^--([\w\d_-]{1,})$#', $arg, $match) || preg_match('#^-([\w\d_]{1,})$#', $arg, $match)) {
+ $current = $match[1];
+ $this->_args[$current] = true;
+ } else {
+ if ($current) {
+ $this->_args[$current] = $arg;
+ } else if (preg_match('#^([\w\d_]{1,})$#', $arg, $match)) {
+ $this->_args[$match[1]] = true;
+ }
+ }
+ }
+ return $this;
+ }
+
+ /**
+ * Retrieve argument value by name or false
+ *
+ * @param string $name the argument name
+ * @return mixed
+ */
+ public function getArg($name) {
+ if (isset($this->_args[$name])) {
+ return $this->_args[$name];
+ }
+ return false;
+ }
+
+ /**
* Modified version of the code at the site below:
* @see http://www.pyrosoft.co.uk/blog/2007/07/01/php-array-to-text-table-function/
*/
@@ -343,7 +411,7 @@ class Wiz_Plugin_Abstract {
* @author Nicholas Vahalik <nick@classyllama.com>
*/
public function __construct() {
- $this->_parseArgs();
+ // $this->_parseArgs();
}
/**
@@ -362,44 +430,6 @@ public function getActions() {
}
return $_commands;
}
-
- /**
- * Parse input arguments
- *
- * @return Wiz_Plugin_Abstract
- */
- protected function _parseArgs() {
- $current = null;
- foreach ($_SERVER['argv'] as $arg) {
- $match = array();
- if (preg_match('#^--([\w\d_-]{1,})$#', $arg, $match) || preg_match('#^-([\w\d_]{1,})$#', $arg, $match)) {
- $current = $match[1];
- $this->_args[$current] = true;
- } else {
- if ($current) {
- $this->_args[$current] = $arg;
- } else if (preg_match('#^([\w\d_]{1,})$#', $arg, $match)) {
- $this->_args[$match[1]] = true;
- }
- }
- }
- return $this;
- }
-
- /**
- * Retrieve argument value by name or false
- *
- * @param string $name the argument name
- * @return mixed
- */
- public function getArg($name) {
- if (isset($this->_args[$name])) {
- return $this->_args[$name];
- }
- return false;
- }
-
-
}
class Wiz_Inspector {
View
8 app/plugins/301.php
@@ -131,13 +131,7 @@ function htgenAction($options) {
* @author Nicholas Vahalik <nick@classyllama.com>
*/
public function xmlsm2csvAction($options) {
- $settings['path-only'] = false;
- foreach ($options as $k => $option) {
- if (substr($option, 0, 2) == '--') {
- $settings[substr($option, 2)] = true;
- unset($options[$k]);
- }
- }
+ $settings['path-only'] = Wiz::getWiz()->getArg('path-only');
$filename = array_shift($options);
View
13 app/plugins/Config.php
@@ -119,18 +119,15 @@ private function _recurseXpathOutput($parents, $xmlelement) {
/**
* Returns the entire Magento config as nicely formatted XML to stdout.
- *
- * @param ugly (optional) - Makes the output ugly (no tabs or newlines)
+ * Options:
+ * --ugly (optional) - Makes the output ugly (no tabs or newlines)
+ *
* @return The Magento Configuration as as nicely printed XML File.
* @author Nicholas Vahalik <nick@classyllama.com>
*/
- public function asxmlAction($options) {
- $ugly = 0;
- if (count($options) > 0 && strtolower($options[0]) == 'ugly') {
- $ugly = NULL;
- }
+ public function asxmlAction() {
Wiz::getMagento();
- echo Mage::getConfig()->getNode()->asNiceXml('', $ugly);
+ echo Mage::getConfig()->getNode()->asNiceXml('', Wiz::getWiz()->getArg('ugly'));
echo PHP_EOL;
return TRUE;
}
View
17 app/plugins/Magento.php
@@ -46,21 +46,6 @@ function versionAction() {
* @author Nicholas Vahalik <nick@classyllama.com>
*/
function scriptAction($options) {
- $store = 'admin';
- if (count($options) > 1) {
- if (($k = array_search('--default-store', $options)) !== FALSE) {
- unset($options[$k]);
- $store = '';
- }
- if (($k = array_search('--store', $options)) !== FALSE) {
- $optionArray = array_slice($options, $k, 2);
- if ($optionArray[1] != '') {
- $store = $optionArray[1];
- }
- unset($options[$k]);
- unset($options[$k+1]);
- }
- }
if (count($options) < 1) {
echo 'Please enter a script to execute.'.PHP_EOL;
return FALSE;
@@ -71,7 +56,7 @@ function scriptAction($options) {
}
else {
$path = realpath($options[0]);
- Wiz::getMagento($store);
+ Wiz::getMagento();
include $path;
return TRUE;
}
View
1 app/plugins/Sql.php
@@ -82,7 +82,6 @@ public function execAction($options) {
$query = mysql_real_escape_string(array_shift($options));
$config = $this->_getDbConfig();
if (count($options) > 0 && $options[0] == 'batch') {
- echo 'hi';
$batch = '--batch';
}
proc_close(proc_open("mysql $batch -u{$config['username']} -p{$config['password']} -P{$config['port']} -h{$config['host']} {$config['dbname']} -e \"$query\"", array(0 => STDIN, 1 => STDOUT, 2 => STDERR), $pipes));
View
2 wiz.php
@@ -20,5 +20,5 @@
include 'app/Wiz.php';
-$wiz = new Wiz();
+$wiz = Wiz::getWiz();
$wiz->run();

0 comments on commit 3f6fbab

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