Source for file cli.php

Documentation is available at cli.php

  1. <?php
  2. /**
  3.  * @package     Joomla.Platform
  4.  * @subpackage  Application
  5.  *
  6.  * @copyright   Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
  7.  * @license     GNU General Public License version 2 or later; see LICENSE
  8.  */
  9.  
  10. defined('JPATH_PLATFORM'or die;
  11.  
  12. /**
  13.  * Base class for a Joomla! command line application.
  14.  *
  15.  * @package     Joomla.Platform
  16.  * @subpackage  Application
  17.  * @since       11.4
  18.  */
  19. {
  20.     /**
  21.      * @var    JRegistry  The application configuration object.
  22.      * @since  11.1
  23.      */
  24.     protected $config;
  25.  
  26.     /**
  27.      * @var    JApplicationCli  The application instance.
  28.      * @since  11.1
  29.      */
  30.     protected static $instance;
  31.  
  32.     /**
  33.      * Class constructor.
  34.      *
  35.      * @param   mixed  $input       An optional argument to provide dependency injection for the application's
  36.      *                               input object.  If the argument is a JInputCli object that object will become
  37.      *                               the application's input object, otherwise a default input object is created.
  38.      * @param   mixed  $config      An optional argument to provide dependency injection for the application's
  39.      *                               config object.  If the argument is a JRegistry object that object will become
  40.      *                               the application's config object, otherwise a default config object is created.
  41.      * @param   mixed  $dispatcher  An optional argument to provide dependency injection for the application's
  42.      *                               event dispatcher.  If the argument is a JEventDispatcher object that object will become
  43.      *                               the application's event dispatcher, if it is null then the default event dispatcher
  44.      *                               will be created based on the application's loadDispatcher() method.
  45.      *
  46.      * @see     JApplicationBase::loadDispatcher()
  47.      * @since   11.1
  48.      */
  49.     public function __construct(JInputCli $input nullJRegistry $config nullJEventDispatcher $dispatcher null)
  50.     {
  51.         // Close the application if we are not executed from the command line.
  52.         // @codeCoverageIgnoreStart
  53.         if (!defined('STDOUT'|| !defined('STDIN'|| !isset($_SERVER['argv']))
  54.         {
  55.             $this->close();
  56.         }
  57.         // @codeCoverageIgnoreEnd
  58.  
  59.         // If a input object is given use it.
  60.         if ($input instanceof JInput)
  61.         {
  62.             $this->input = $input;
  63.         }
  64.         // Create the input based on the application logic.
  65.         else
  66.         {
  67.             if (class_exists('JInput'))
  68.             {
  69.                 $this->input = new JInputCLI;
  70.             }
  71.         }
  72.  
  73.         // If a config object is given use it.
  74.         if ($config instanceof JRegistry)
  75.         {
  76.             $this->config = $config;
  77.         }
  78.         // Instantiate a new configuration object.
  79.         else
  80.         {
  81.             $this->config = new JRegistry;
  82.         }
  83.  
  84.         $this->loadDispatcher($dispatcher);
  85.  
  86.         // Load the configuration object.
  87.         $this->loadConfiguration($this->fetchConfigurationData());
  88.  
  89.         // Set the execution datetime and timestamp;
  90.         $this->set('execution.datetime'gmdate('Y-m-d H:i:s'));
  91.         $this->set('execution.timestamp'time());
  92.  
  93.         // Set the current directory.
  94.         $this->set('cwd'getcwd());
  95.     }
  96.  
  97.     /**
  98.      * Returns a property of the object or the default value if the property is not set.
  99.      *
  100.      * @param   string  $key      The name of the property.
  101.      * @param   mixed   $default  The default value (optional) if none is set.
  102.      *
  103.      * @return  mixed   The value of the configuration.
  104.      *
  105.      * @since   11.3
  106.      */
  107.     public function get($key$default null)
  108.     {
  109.         return $this->config->get($key$default);
  110.     }
  111.  
  112.     /**
  113.      * Returns a reference to the global JApplicationCli object, only creating it if it doesn't already exist.
  114.      *
  115.      * This method must be invoked as: $cli = JApplicationCli::getInstance();
  116.      *
  117.      * @param   string  $name  The name (optional) of the JApplicationCli class to instantiate.
  118.      *
  119.      * @return  JApplicationCli 
  120.      *
  121.      * @since   11.1
  122.      */
  123.     public static function getInstance($name null)
  124.     {
  125.         // Only create the object if it doesn't exist.
  126.         if (empty(self::$instance))
  127.         {
  128.             if (class_exists($name&& (is_subclass_of($name'JApplicationCli')))
  129.             {
  130.                 self::$instance new $name;
  131.             }
  132.             else
  133.             {
  134.                 self::$instance new JApplicationCli;
  135.             }
  136.         }
  137.  
  138.         return self::$instance;
  139.     }
  140.  
  141.     /**
  142.      * Execute the application.
  143.      *
  144.      * @return  void 
  145.      *
  146.      * @since   11.1
  147.      */
  148.     public function execute()
  149.     {
  150.         // Trigger the onBeforeExecute event.
  151.         $this->triggerEvent('onBeforeExecute');
  152.  
  153.         // Perform application routines.
  154.         $this->doExecute();
  155.  
  156.         // Trigger the onAfterExecute event.
  157.         $this->triggerEvent('onAfterExecute');
  158.     }
  159.  
  160.     /**
  161.      * Load an object or array into the application configuration object.
  162.      *
  163.      * @param   mixed  $data  Either an array or object to be loaded into the configuration object.
  164.      *
  165.      * @return  JApplicationCli  Instance of $this to allow chaining.
  166.      *
  167.      * @since   11.1
  168.      */
  169.     public function loadConfiguration($data)
  170.     {
  171.         // Load the data into the configuration object.
  172.         if (is_array($data))
  173.         {
  174.             $this->config->loadArray($data);
  175.         }
  176.         elseif (is_object($data))
  177.         {
  178.             $this->config->loadObject($data);
  179.         }
  180.  
  181.         return $this;
  182.     }
  183.  
  184.     /**
  185.      * Write a string to standard output.
  186.      *
  187.      * @param   string   $text  The text to display.
  188.      * @param   boolean  $nl    True (default) to append a new line at the end of the output string.
  189.      *
  190.      * @return  JApplicationCli  Instance of $this to allow chaining.
  191.      *
  192.      * @codeCoverageIgnore
  193.      * @since   11.1
  194.      */
  195.     public function out($text ''$nl true)
  196.     {
  197.         fwrite(STDOUT$text ($nl "\n" null));
  198.  
  199.         return $this;
  200.     }
  201.  
  202.     /**
  203.      * Get a value from standard input.
  204.      *
  205.      * @return  string  The input string from standard input.
  206.      *
  207.      * @codeCoverageIgnore
  208.      * @since   11.1
  209.      */
  210.     public function in()
  211.     {
  212.         return rtrim(fread(STDIN8192)"\n");
  213.     }
  214.  
  215.     /**
  216.      * Modifies a property of the object, creating it if it does not already exist.
  217.      *
  218.      * @param   string  $key    The name of the property.
  219.      * @param   mixed   $value  The value of the property to set (optional).
  220.      *
  221.      * @return  mixed   Previous value of the property
  222.      *
  223.      * @since   11.3
  224.      */
  225.     public function set($key$value null)
  226.     {
  227.         $previous $this->config->get($key);
  228.         $this->config->set($key$value);
  229.  
  230.         return $previous;
  231.     }
  232.  
  233.     /**
  234.      * Method to load a PHP configuration class file based on convention and return the instantiated data object.  You
  235.      * will extend this method in child classes to provide configuration data from whatever data source is relevant
  236.      * for your specific application.
  237.      *
  238.      * @param   string  $file   The path and filename of the configuration file. If not provided, configuration.php
  239.      *                           in JPATH_BASE will be used.
  240.      * @param   string  $class  The class name to instantiate.
  241.      *
  242.      * @return  mixed   Either an array or object to be loaded into the configuration object.
  243.      *
  244.      * @since   11.1
  245.      */
  246.     protected function fetchConfigurationData($file ''$class 'JConfig')
  247.     {
  248.         // Instantiate variables.
  249.         $config array();
  250.  
  251.         if (empty($file&& defined('JPATH_BASE'))
  252.         {
  253.             $file JPATH_BASE '/configuration.php';
  254.  
  255.             // Applications can choose not to have any configuration data
  256.             // by not implementing this method and not having a config file.
  257.             if (!file_exists($file))
  258.             {
  259.                 $file '';
  260.             }
  261.         }
  262.  
  263.         if (!empty($file))
  264.         {
  265.             JLoader::register($class$file);
  266.  
  267.             if (class_exists($class))
  268.             {
  269.                 $config new $class;
  270.             }
  271.             else
  272.             {
  273.                 throw new RuntimeException('Configuration class does not exist.');
  274.             }
  275.         }
  276.  
  277.         return $config;
  278.     }
  279.  
  280.     /**
  281.      * Method to run the application routines.  Most likely you will want to instantiate a controller
  282.      * and execute it, or perform some sort of task directly.
  283.      *
  284.      * @return  void 
  285.      *
  286.      * @codeCoverageIgnore
  287.      * @since   11.3
  288.      */
  289.     protected function doExecute()
  290.     {
  291.         // Your application routines go here.
  292.     }
  293. }

Documentation generated on Tue, 19 Nov 2013 14:55:45 +0100 by phpDocumentor 1.4.3