* Moved provider instantiation and info to a separate class
authorChristian Opitz <christian.opitz@netresearch.de>
Fri, 11 Jan 2013 19:00:36 +0000 (19:00 +0000)
committerChristian Opitz <christian.opitz@netresearch.de>
Fri, 31 May 2013 13:22:25 +0000 (15:22 +0200)
* Wrote provider listing for BE

classes/class.tx_t3build_providerInfo.php [new file with mode: 0644]
dispatch.php
mod1/index.php
mod1/locallang.xml

diff --git a/classes/class.tx_t3build_providerInfo.php b/classes/class.tx_t3build_providerInfo.php
new file mode 100644 (file)
index 0000000..5696455
--- /dev/null
@@ -0,0 +1,78 @@
+<?php
+
+require_once t3lib_extMgm::extPath('t3build').'provider/class.abstract.php';
+
+class tx_t3build_providerInfo
+{
+       const ExtKey = 't3build';
+       const Mask_ClassName = 'tx_t3build_provider_%s';
+       const Mask_FileName = 'class.%s.php';
+
+       protected $extProviders;
+       protected $providers;
+
+       public function getClassPath()
+       {
+               return t3lib_extMgm::extPath(self::ExtKey).'provider';
+       }
+
+       public function getExtProviders()
+       {
+           if (!$this->extProviders) {
+                   $this->extProviders = (array) $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['t3build']['providers'];
+           }
+           return $this->extProviders;
+       }
+
+       public function getProviders()
+       {
+           if (!$this->providers) {
+            $directory = new DirectoryIterator($this->getClassPath());
+            $pattern = '/^class\.([a-zA-Z0-9]+)\.php$/';
+            $providers = array_keys($this->getExtProviders());
+            foreach ($directory as $file) {
+                /* @var $file SplFileInfo */
+                if ($file->isFile() && preg_match($pattern, $file->getFilename(), $match) && $match[1] != 'abstract') {
+                    $providers[] = $match[1];
+                }
+            }
+            $providers = array_unique($providers);
+            sort($providers);
+            $this->providers = $providers;
+           }
+           return $this->providers;
+       }
+
+       /**
+        * Instanciate a provider
+        *
+        * @param string $provider Provider name
+        * @return tx_t3build_provider_abstract
+        */
+       public function getProviderInstance($provider)
+       {
+               $extProviders = $this->getExtProviders();
+
+               /* @var $instance tx_t3build_provider_abstract */
+               if (array_key_exists($provider, $extProviders)) {
+                   $instance = t3lib_div::getUserObj($extProviders[$provider], '');
+               } else {
+               $className = sprintf(self::Mask_ClassName, $provider);
+               if (!class_exists($className)) {
+                   $file = $this->getClassPath().DIRECTORY_SEPARATOR.sprintf(self::Mask_FileName, $provider);
+                   if (!file_exists($file)) {
+                       die('Invalid provider "'.$provider.'"');
+                   }
+                       t3lib_div::requireOnce($file);
+               }
+                   $instance = t3lib_div::makeInstance($className);
+               }
+
+               if (!$instance instanceof tx_t3build_provider_abstract) {
+                   echo 'Controller '.$provider.' must extend tx_t3build_provider_abstract';
+                   exit;
+               }
+
+               return $instance;
+       }
+}
\ No newline at end of file
index 106d875..4f6823f 100644 (file)
@@ -13,7 +13,7 @@ if (!defined ('TYPO3_cliMode')) {
 }
 
 require_once PATH_t3lib . 'class.t3lib_cli.php';
-require_once t3lib_extMgm::extPath('t3build').'provider/class.abstract.php';
+require_once t3lib_extMgm::extPath('t3build').'classes/class.tx_t3build_providerInfo.php';
 
 /**
  * General CLI dispatcher for the t3build extension.
@@ -23,14 +23,18 @@ require_once t3lib_extMgm::extPath('t3build').'provider/class.abstract.php';
  */
 class tx_t3build_dispatch extends t3lib_cli {
        const ExtKey = 't3build';
-       const Mask_ClassName = 'tx_t3build_provider_%s';
-       const Mask_FileName = 'class.%s.php';
+
+       /**
+        * @var tx_t3build_providerInfo
+        */
+       protected $providerInfo;
 
        /**
         * Creates this object.
         */
        public function __construct() {
                parent::__construct();
+               $this->providerInfo = t3lib_div::makeInstance('tx_t3build_providerInfo');
                $this->cli_help = array_merge($this->cli_help, array(
                        'name' => 'tx_t3build_dispatch',
                        'synopsis' => self::ExtKey . ' controller action ###OPTIONS###',
@@ -46,51 +50,21 @@ class tx_t3build_dispatch extends t3lib_cli {
         * @return void
         */
        public function dispatch() {
-               $controller = (string)$this->cli_args['_DEFAULT'][1];
+               $provider = (string)$this->cli_args['_DEFAULT'][1];
                $action = (string)$this->cli_args['_DEFAULT'][2];
-               $classPath = t3lib_extMgm::extPath(self::ExtKey).'provider';
-               $extProviders = (array) $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['t3build']['providers'];
 
                if (isset($this->cli_args['--debug'])) {
                    restore_exception_handler();
                    restore_error_handler();
                }
 
-               if (!$controller) {
+               if (!$provider) {
                    $this->cli_echo('No command provided - please specify one of the following commands:'.PHP_EOL, true);
-            $directory = new DirectoryIterator($classPath);
-            $pattern = '/^class\.([a-zA-Z0-9]+)\.php$/';
-            $providers = array_keys($extProviders);
-            foreach ($directory as $file) {
-                /* @var $file SplFileInfo */
-                if ($file->isFile() && preg_match($pattern, $file->getFilename(), $match) && $match[1] != 'abstract') {
-                    $providers[] = $match[1];
-                }
-            }
-            $providers = array_unique($providers);
-            sort($providers);
-            echo implode(PHP_EOL, $providers).PHP_EOL;
+            echo implode(PHP_EOL, $this->providerInfo->getProviders()).PHP_EOL;
             return;
                }
 
-               /* @var $instance tx_t3build_abstractController */
-               if (array_key_exists($controller, $extProviders)) {
-                   $instance = t3lib_div::getUserObj($extProviders[$controller], '');
-               } else {
-               $className = sprintf(self::Mask_ClassName, $controller);
-               if (!class_exists($className)) {
-                   $file = $classPath.DIRECTORY_SEPARATOR.sprintf(self::Mask_FileName, $controller);
-                   if (!file_exists($file)) {
-                       die('Invalid command "'.$controller.'"');
-                   }
-                       t3lib_div::requireOnce($file);
-               }
-                   $instance = t3lib_div::makeInstance($className);
-               }
-               if (!$instance instanceof tx_t3build_provider_abstract) {
-                   echo 'Controller '.$controller.' must extend tx_t3build_provider_abstract';
-                   exit;
-               }
+               $instance = $this->providerInfo->getProviderInstance($provider);
                $instance->init($this->cli_args);
                $instance->run($action);
        }
index bbecd5b..7ea0524 100644 (file)
@@ -27,6 +27,8 @@ $LANG->includeLLFile('EXT:t3build/mod1/locallang.xml');
        // This checks permissions and exits if the users has no permission for entry.
 $BE_USER->modAccess($MCONF, 1);
 
+require_once t3lib_extMgm::extPath('t3build').'classes/class.tx_t3build_providerInfo.php';
+
 
 /**
  * Module 't3build' for the 't3build' extension.
@@ -42,6 +44,11 @@ class tx_t3build_module extends t3lib_SCbase {
        protected $pageinfo;
 
        /**
+        * @var tx_t3build_providerInfo
+        */
+       protected $providerInfo;
+
+       /**
         * Initializes the Module
         *
         * @return      void
@@ -49,6 +56,8 @@ class tx_t3build_module extends t3lib_SCbase {
        public function __construct() {
                parent::init();
 
+               $this->providerInfo = t3lib_div::makeInstance('tx_t3build_providerInfo');
+
                        // initialize document
                $this->doc = t3lib_div::makeInstance('template');
                $this->doc->setModuleTemplate(
@@ -70,7 +79,7 @@ class tx_t3build_module extends t3lib_SCbase {
                $this->MOD_MENU = array(
                        'function' => array(
                            'status' => $GLOBALS['LANG']->getLL('status'),
-                           'export' => $GLOBALS['LANG']->getLL('export'),
+                           'providers' => $GLOBALS['LANG']->getLL('providers'),
                        )
                );
 
@@ -153,11 +162,30 @@ class tx_t3build_module extends t3lib_SCbase {
            return str_pad(nl2br($string), intval(ini_get('output_buffering')))."\n";
        }
 
-       protected function exportAction() {
-           $post = (array) t3lib_div::_POST();
-           t3lib_div::requireOnce(t3lib_extMgm::extPath('t3build').'provider/class.export.php');
-           /* @var $provider tx_t3build_provider_export */
-           $provider = t3lib_div::makeInstance('tx_t3build_provider_export');
+       protected function providersAction()
+       {
+        if (t3lib_div::_GP('action')) {
+            return call_user_func(array($this, t3lib_div::_GP('action').'Action'));
+        }
+
+        $content .= '<input type="hidden" name="action" value="runProvider"/>';
+        $content .= '<input type="hidden" id="providerInput" name="provider" value="run"/>';
+        $content .= '<dl class="t3-overview-list">';
+               foreach ($this->providerInfo->getProviders() as $provider) {
+                   $instance = $this->providerInfo->getProviderInstance($provider);
+                   $reflection = new ReflectionClass($instance);
+            preg_match_all('/^\s+\* ([^@\/].*)$/m', $reflection->getDocComment(), $lines);
+            $help = nl2br(trim(implode("\n", $lines[1])));
+            $content .= '<dt><a href="#" onclick="$(\'providerInput\').value = \''.$provider.'\'; $$(\'form\')[0].submit();">'.$provider.'</a></dt>';
+                       $content .= '<dd style="white-space:pre;">'.htmlspecialchars(trim(implode("\n", $lines[1]))).'</dd>';
+               }
+               $content .= '</dl>';
+               return $content;
+       }
+
+       protected function runProviderAction()
+       {
+           $provider = $this->providerInfo->getProviderInstance(t3lib_div::_GP('provider'));
 
            $provider->init(array());
 
@@ -170,7 +198,7 @@ class tx_t3build_module extends t3lib_SCbase {
            }
            }
 
-           if ($post['download'] || $post['save']) {
+           if ($post['run']) {
                // prepare console output in iframe:
                t3lib_div::cleanOutputBuffers();
                ob_implicit_flush(true);
@@ -199,9 +227,8 @@ class tx_t3build_module extends t3lib_SCbase {
            $form .= '
            <div class="t3buildButtons">
            <input type="submit" name="download" value="Download"/>&nbsp;
-           <input type="submit" name="save" value="Export"/>';
-           if (file_exists($filename))
-       $form .= '<div class="divider" style="margin-bottom:10px;"></div>
+           <input type="submit" name="save" value="Export"/>
+           <div class="divider" style="margin-bottom:10px;"></div>
            </div>
            <div class="t3buildFormContainer">';
            $i = 0;
index 0224e9a..1f31e81 100644 (file)
@@ -23,7 +23,7 @@
                        <label index="changeBeUser">Change user</label>
                        <label index="changeBeUser.success">Backend user changed</label>
                        
-                       <label index="export">Export</label>
+                       <label index="providers">Providers</label>
                </languageKey>
                <languageKey index="de" type="array">
                        <label index="mlang_tabs_tab">Build</label>