Advertisement
kotuha

JApplicationCLI

Jul 7th, 2011
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.68 KB | None | 0 0
  1. #!/usr/bin/php -q
  2. <?php
  3. /**
  4.  * @version $Id$
  5.  * @package Joomla.Example
  6.  * @subpackage CLI
  7.  * @copyright Copyright (C) 2008 - 2009 Louis Landry. All rights reserved.
  8.  * @license GNU General Public License, <http://www.gnu.org/copyleft/gpl.html>
  9.  */
  10.  
  11. // We are a valid Joomla entry point.
  12. define('_JEXEC', 1);
  13.  
  14.  
  15. define('JPATH_BASE', dirname(__FILE__) );
  16.  
  17. define( 'DS', DIRECTORY_SEPARATOR );
  18.  
  19. require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
  20. require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );
  21.  
  22. jimport('joomla.application.application');
  23.  
  24. /**
  25.  * Simple command line interface application class for quick bootstrapping
  26.  things to test.
  27.  *
  28.  * @package Joomla.Example
  29.  * @subpackage CLI
  30.  * @version 1.0
  31.  */
  32. class JApplicationCLI extends JApplication
  33. {
  34.     /**
  35.      * The name of the application
  36.      *
  37.      * @var array
  38.      */
  39.     public $_name = 'JApplicationCLI';
  40.  
  41.     /**
  42.      * The current working directory of the application.
  43.      *
  44.      * @var string
  45.      * @since 1.0
  46.      */
  47.     public $cwd = null;
  48.  
  49.     /**
  50.      * The application argument values.
  51.      *
  52.      * @var array
  53.      * @since 1.0
  54.      */
  55.     public $argv = array();
  56.  
  57.     /**
  58.      * The application argument options.
  59.      *
  60.      * The options are populated automatically by the private _initializeOptions
  61.      function
  62.      * which sets each command line argument as a member of the options object.
  63.      *
  64.      * @var object
  65.      * @since 1.0
  66.      */
  67.     public $options = null;
  68.  
  69.     public $shortargs = 'i:o:hflp';
  70.  
  71.     // Need to wait for PHP 5.3
  72.     public $longargs = array('help');
  73.  
  74.     public function __construct($config = array())
  75.     {  
  76.         $config['clientId'] = 2;
  77.        
  78.         parent::__construct($config);
  79.        
  80.         // Initialize the execution arguments.
  81.         $this->_initializeOptions();
  82.  
  83.         // If the help screen has been requested, print it and exit.
  84.         if (isset($this->options->h)) {
  85.             $this->help();
  86.             exit(0);
  87.         }
  88.        
  89.         // Get the current directory.
  90.         $this->cwd = getcwd();
  91.     }
  92.  
  93.     public function execute()
  94.     {
  95.         // Print the application header.
  96.         $this->out('Joomla! Example Command Line Application');
  97.         $this->out('----------------------------------------');
  98.         $this->out();
  99.        
  100.         $name = $this->getName();
  101.        
  102.         $this->out($name);
  103.                
  104.         //$login =& $this->login(array('username' => 'admin',
  105.         //                 'password' => 'admin'),
  106.         //           array('remember' => FALSE));
  107.        
  108.        
  109.         //$this->out($login);
  110.  
  111.         $listings = KFactory::tmp('admin::com.unbooked.controller.listing')->_actionDownload();
  112.        
  113.         //$this->out(file_get_contents(JComponentHelper::getParams('com_unbooked')->get('ub_api_url')."vendors"));
  114.        
  115.         //$db = JFactory::getDBO();
  116.        
  117.         //print_r($json_data);
  118.  
  119.        
  120.         $this->out('----------------------------------------');
  121.         $this->out();
  122.     }
  123.  
  124.     /**
  125.      * Method to print a line of text to stdout.
  126.      *
  127.      * @param string The line of text to print to stdout.
  128.      * @return void
  129.      * @since 1.0
  130.      */
  131.     function out($text = '')
  132.     {
  133.         echo "\n".$text;
  134.     }
  135.  
  136.     /**
  137.      * Method to build and print the help screen text to stdout.
  138.      *
  139.      * @return void
  140.      * @since 1.0
  141.      */
  142.     protected function help()
  143.     {
  144.         // Initialize variables.
  145.         $help = array();
  146.         // Build the help screen information.
  147.         $help[] = 'Your Help Screen';
  148.         $help[] = '';
  149.         $help[] = 'Here is where you put all your help screen information.';
  150.         $help[] = '';
  151.         // Print out the help information.
  152.         echo implode("\n", $help);
  153.     }
  154.  
  155.     /**
  156.      * Initialize the command line options and arguments.
  157.      *
  158.      * @return void
  159.      * @since 1.0
  160.      */
  161.     private function _initializeOptions()
  162.     {
  163.         // Get the options from the command line argument list and argument values.
  164.         $opts = getopt($this->shortargs /*, $this->longargs*/);
  165.         $args = isset($GLOBALS['argv']) ? (array) $GLOBALS['argv'] : array();
  166.         // If the argument value list is not empty, make sure the options are unset.
  167.         if (!empty($args))
  168.         {
  169.             // Iterate over the found options.
  170.             foreach ($opts as $o => $a)
  171.             {
  172.                 // Search for occurrences of the option with no value or with no space between option and value.
  173.                 while ($k = array_search('-'.$o.$a,$args))
  174.                 {
  175.                     // Remove any found options from the argument value array.
  176.                     if ($k) {
  177.                         unset($args[$k]);
  178.                     }
  179.                 }
  180.  
  181.                 // Search for remaining occurrences of the option (space between option and value).
  182.                 while ($k = array_search('-'.$o,$args))
  183.                 {
  184.                     // Remove any found options and values from the argument value array.
  185.                     if($k) {
  186.                         unset($args[$k]);
  187.                         unset($args[$k+1]);
  188.                     }
  189.                 }
  190.             }
  191.         }
  192.  
  193.         // Set the options and argument values to internal members.
  194.         $this->options = (object) $opts;
  195.         $this->argv = (array) $args;
  196.     }
  197. }
  198.  
  199. $app = new JApplicationCLI(); // Create the application object.
  200. $app->execute(); // Execute the application.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement