Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added abstract class JApplicationBase.

Reworked JApplicationWeb and JApplicationCli to use JApplicationBase.
  • Loading branch information...
commit 8c2e9e36e3e334d832f1492000482a6afec60faa 1 parent d320858
@robschley robschley authored
View
271 libraries/joomla/application/base.php
@@ -0,0 +1,271 @@
+<?php
+/**
+ * @package Joomla.Platform
+ * @subpackage Application
+ *
+ * @copyright Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved.
+ * @license GNU General Public License version 2 or later; see LICENSE
+ */
+
+defined('JPATH_PLATFORM') or die;
+
+jimport('joomla.event.dispatcher');
+
+/**
+ * Joomla Platform Base Application Class
+ *
+ * @package Joomla.Platform
+ * @subpackage Application
+ * @since 12.1
+ */
+abstract class JApplicationBase
+{
+ /**
+ * The application input object.
+ *
+ * @var JInput
+ * @since 12.1
+ */
+ public $input;
+
+ /**
+ * The application configuration object.
+ *
+ * @var JRegistry
+ * @since 12.1
+ */
+ protected $config;
+
+ /**
+ * The character encoding string.
+ *
+ * @var string
+ * @since 12.1
+ */
+ protected $charSet = 'utf-8';
+
+ /**
+ * The application dispatcher object.
+ *
+ * @var JDispatcher
+ * @since 12.1
+ */
+ protected $dispatcher;
+
+ /**
+ * The application instance.
+ *
+ * @var JApplicationBase
+ * @since 12.1
+ */
+ protected static $instance;
+
+ /**
+ * Method to close the application.
+ *
+ * @param integer $code The exit code (optional; default is 0).
+ *
+ * @return void
+ *
+ * @since 12.1
+ */
+ public function close($code = 0)
+ {
+ exit($code);
+ }
+
+ /**
+ * Method to execute the application.
+ *
+ * @return void
+ *
+ * @since 12.1
+ */
+ abstract public function execute();
+
+ /**
+ * Method to get a property of the application or the default value if the property is not set.
+ *
+ * @param string $key The name of the property.
+ * @param mixed $default The default value (optional) if none is set.
+ *
+ * @return mixed The value of the configuration.
+ *
+ * @since 12.1
+ */
+ public function get($key, $default = null)
+ {
+ return $this->config->get($key, $default);
+ }
+
+ /**
+ * Method to get the application character set.
+ *
+ * @return string The character set.
+ *
+ * @since 12.1
+ */
+ public function getCharacterSet()
+ {
+ return $this->charSet;
+ }
+
+ /**
+ * Method to load an object or array into the application configuration object.
+ *
+ * @param mixed $data Either an array or object to be loaded into the configuration object.
+ *
+ * @return JApplicationBase The application to allow chaining.
+ *
+ * @since 12.1
+ */
+ public function loadConfiguration($data)
+ {
+ // Load the data into the configuration object.
+ if (is_array($data))
+ {
+ $this->config->loadArray($data);
+ }
+ elseif (is_object($data))
+ {
+ $this->config->loadObject($data);
+ }
+
+ return $this;
+ }
+
+ /**
+ * Registers a handler to a particular event group.
+ *
+ * @param string $event The event name.
+ * @param callback $handler The handler, a function or an instance of a event object.
+ *
+ * @return JApplicationBase The application to allow chaining.
+ *
+ * @since 12.1
+ */
+ public function registerEvent($event, $handler)
+ {
+ if ($this->dispatcher instanceof JDispatcher)
+ {
+ $this->dispatcher->register($event, $handler);
+ }
+
+ return $this;
+ }
+
+ /**
+ * Method to set a property of the application, creating it if it does not already exist.
+ *
+ * @param string $key The name of the property.
+ * @param mixed $value The value of the property to set (optional).
+ *
+ * @return mixed Previous value of the property
+ *
+ * @since 12.1
+ */
+ public function set($key, $value = null)
+ {
+ $previous = $this->config->get($key);
+ $this->config->set($key, $value);
+
+ return $previous;
+ }
+
+ /**
+ * Method to set the application character set.
+ *
+ * @param string $charset The character set.
+ *
+ * @return JApplicationBase The application to allow chaining.
+ *
+ * @since 12.1
+ */
+ public function setCharacterSet($charset)
+ {
+ $this->charSet = $charset;
+
+ return $this;
+ }
+
+ /**
+ * Calls all handlers associated with an event group.
+ *
+ * @param string $event The event name.
+ * @param array $args An array of arguments (optional).
+ *
+ * @return array An array of results from each function call, or null if no dispatcher is defined.
+ *
+ * @since 12.1
+ */
+ public function triggerEvent($event, array $args = null)
+ {
+ if ($this->dispatcher instanceof JDispatcher)
+ {
+ return $this->dispatcher->trigger($event, $args);
+ }
+
+ return null;
+ }
+
+ /**
+ * Method to load a PHP configuration class file based on convention and return the instantiated data object. You
+ * will extend this method in child classes to provide configuration data from whatever data source is relevant
+ * for your specific application.
+ *
+ * @param string $file The path and filename of the configuration file. If not provided, configuration.php
+ * in JPATH_BASE will be used.
+ * @param string $class The class name to instantiate.
+ *
+ * @return mixed Either an array or object to be loaded into the configuration object.
+ *
+ * @since 12.1
+ */
+ protected function fetchConfigurationData($file = '', $class = 'JConfig')
+ {
+ // Instantiate variables.
+ $config = array();
+
+ if (empty($file) && defined('JPATH_BASE'))
+ {
+ $file = JPATH_BASE . '/configuration.php';
+
+ // Applications can choose not to have any configuration data
+ // by not implementing this method and not having a config file.
+ if (!file_exists($file))
+ {
+ $file = '';
+ }
+ }
+
+ if (!empty($file))
+ {
+ JLoader::register($class, $file);
+
+ if (class_exists($class))
+ {
+ $config = new $class;
+ }
+ else
+ {
+ throw new RuntimeException('Configuration class does not exist.');
+ }
+ }
+
+ return $config;
+ }
+
+ /**
+ * Method to create an event dispatcher for the application. The logic and options for creating
+ * this object are adequately generic for default cases but for many applications it will make
+ * sense to override this method and create event dispatchers based on more specific needs.
+ *
+ * @return void
+ *
+ * @since 12.1
+ */
+ protected function loadDispatcher()
+ {
+ $this->dispatcher = JDispatcher::getInstance();
+ }
+}
View
219 libraries/joomla/application/cli.php
@@ -9,8 +9,6 @@
defined('JPATH_PLATFORM') or die;
-jimport('joomla.event.dispatcher');
-
/**
* Base class for a Joomla! command line application.
*
@@ -18,33 +16,9 @@
* @subpackage Application
* @since 11.4
*/
-class JApplicationCli
+class JApplicationCli extends JApplicationBase
{
/**
- * @var JInputCli The application input object.
- * @since 11.1
- */
- public $input;
-
- /**
- * @var JRegistry The application configuration object.
- * @since 11.1
- */
- protected $config;
-
- /**
- * @var JDispatcher The application dispatcher object.
- * @since 11.1
- */
- protected $dispatcher;
-
- /**
- * @var JApplicationCli The application instance.
- * @since 11.1
- */
- protected static $instance;
-
- /**
* Class constructor.
*
* @param mixed $input An optional argument to provide dependency injection for the application's
@@ -119,21 +93,6 @@ public function __construct(JInputCli $input = null, JRegistry $config = null, J
}
/**
- * Returns a property of the object or the default value if the property is not set.
- *
- * @param string $key The name of the property.
- * @param mixed $default The default value (optional) if none is set.
- *
- * @return mixed The value of the configuration.
- *
- * @since 11.3
- */
- public function get($key, $default = null)
- {
- return $this->config->get($key, $default);
- }
-
- /**
* Returns a reference to the global JApplicationCli object, only creating it if it doesn't already exist.
*
* This method must be invoked as: $cli = JApplicationCli::getInstance();
@@ -182,56 +141,16 @@ public function execute()
}
/**
- * Method to run the application routines. Most likely you will want to instantiate a controller
- * and execute it, or perform some sort of task directly.
- *
- * @return void
- *
- * @codeCoverageIgnore
- * @since 11.3
- */
- protected function doExecute()
- {
- // Your application routines go here.
- }
-
- /**
- * Exit the application.
- *
- * @param integer $code The exit code (optional; default is 0).
+ * Get a value from standard input.
*
- * @return void
+ * @return string The input string from standard input.
*
* @codeCoverageIgnore
* @since 11.1
*/
- public function close($code = 0)
- {
- exit($code);
- }
-
- /**
- * Load an object or array into the application configuration object.
- *
- * @param mixed $data Either an array or object to be loaded into the configuration object.
- *
- * @return JApplicationCli Instance of $this to allow chaining.
- *
- * @since 11.1
- */
- public function loadConfiguration($data)
+ public function in()
{
- // Load the data into the configuration object.
- if (is_array($data))
- {
- $this->config->loadArray($data);
- }
- elseif (is_object($data))
- {
- $this->config->loadObject($data);
- }
-
- return $this;
+ return rtrim(fread(STDIN, 8192), "\n");
}
/**
@@ -253,135 +172,17 @@ public function out($text = '', $nl = true)
}
/**
- * Get a value from standard input.
- *
- * @return string The input string from standard input.
- *
- * @codeCoverageIgnore
- * @since 11.1
- */
- public function in()
- {
- return rtrim(fread(STDIN, 8192), "\n");
- }
-
- /**
- * Registers a handler to a particular event group.
- *
- * @param string $event The event name.
- * @param callback $handler The handler, a function or an instance of a event object.
- *
- * @return JApplicationCli Instance of $this to allow chaining.
- *
- * @since 11.1
- */
- public function registerEvent($event, $handler)
- {
- if ($this->dispatcher instanceof JDispatcher)
- {
- $this->dispatcher->register($event, $handler);
- }
-
- return $this;
- }
-
- /**
- * Calls all handlers associated with an event group.
- *
- * @param string $event The event name.
- * @param array $args An array of arguments (optional).
- *
- * @return array An array of results from each function call, or null if no dispatcher is defined.
- *
- * @since 11.1
- */
- public function triggerEvent($event, array $args = null)
- {
- if ($this->dispatcher instanceof JDispatcher)
- {
- return $this->dispatcher->trigger($event, $args);
- }
-
- return null;
- }
-
- /**
- * Modifies a property of the object, creating it if it does not already exist.
- *
- * @param string $key The name of the property.
- * @param mixed $value The value of the property to set (optional).
- *
- * @return mixed Previous value of the property
- *
- * @since 11.3
- */
- public function set($key, $value = null)
- {
- $previous = $this->config->get($key);
- $this->config->set($key, $value);
-
- return $previous;
- }
-
- /**
- * Method to load a PHP configuration class file based on convention and return the instantiated data object. You
- * will extend this method in child classes to provide configuration data from whatever data source is relevant
- * for your specific application.
- *
- * @param string $file The path and filename of the configuration file. If not provided, configuration.php
- * in JPATH_BASE will be used.
- * @param string $class The class name to instantiate.
- *
- * @return mixed Either an array or object to be loaded into the configuration object.
- *
- * @since 11.1
- */
- protected function fetchConfigurationData($file = '', $class = 'JConfig')
- {
- // Instantiate variables.
- $config = array();
-
- if (empty($file) && defined('JPATH_BASE'))
- {
- $file = JPATH_BASE . '/configuration.php';
-
- // Applications can choose not to have any configuration data
- // by not implementing this method and not having a config file.
- if (!file_exists($file))
- {
- $file = '';
- }
- }
-
- if (!empty($file))
- {
- JLoader::register($class, $file);
-
- if (class_exists($class))
- {
- $config = new $class;
- }
- else
- {
- throw new RuntimeException('Configuration class does not exist.');
- }
- }
-
- return $config;
- }
-
- /**
- * Method to create an event dispatcher for the application. The logic and options for creating
- * this object are adequately generic for default cases but for many applications it will make sense
- * to override this method and create event dispatchers based on more specific needs.
+ * Method to run the application routines. Most likely you will want to instantiate a controller
+ * and execute it, or perform some sort of task directly.
*
* @return void
*
+ * @codeCoverageIgnore
* @since 11.3
*/
- protected function loadDispatcher()
+ protected function doExecute()
{
- $this->dispatcher = JDispatcher::getInstance();
+ // Your application routines go here.
}
}
View
36 libraries/joomla/application/daemon.php
@@ -351,28 +351,17 @@ public function loadConfiguration($data)
}
/**
- * Restart daemon process.
+ * Execute the daemon.
*
* @return void
*
- * @codeCoverageIgnore
* @since 11.1
*/
- public function restart()
+ public function execute()
{
- JLog::add('Stopping ' . $this->name, JLog::INFO);
- $this->shutdown(true);
- }
+ // Trigger the onBeforeExecute event.
+ $this->triggerEvent('onBeforeExecute');
- /**
- * Spawn daemon process.
- *
- * @return void
- *
- * @since 11.1
- */
- public function start()
- {
// Enable basic garbage collection. Only available in PHP 5.3+
if (function_exists('gc_enable'))
{
@@ -406,6 +395,23 @@ public function start()
{
JLog::add('Starting ' . $this->name . ' failed', JLog::INFO);
}
+
+ // Trigger the onAfterExecute event.
+ $this->triggerEvent('onAfterExecute');
+ }
+
+ /**
+ * Restart daemon process.
+ *
+ * @return void
+ *
+ * @codeCoverageIgnore
+ * @since 11.1
+ */
+ public function restart()
+ {
+ JLog::add('Stopping ' . $this->name, JLog::INFO);
+ $this->shutdown(true);
}
/**
View
200 libraries/joomla/application/web.php
@@ -11,7 +11,6 @@
jimport('joomla.application.web.webclient');
jimport('joomla.environment.uri');
-jimport('joomla.event.dispatcher');
/**
* Base class for a Joomla! Web application.
@@ -20,21 +19,9 @@
* @subpackage Application
* @since 11.4
*/
-class JApplicationWeb
+class JApplicationWeb extends JApplicationBase
{
/**
- * @var JInput The application input object.
- * @since 11.3
- */
- public $input;
-
- /**
- * @var string Character encoding string.
- * @since 11.3
- */
- public $charSet = 'utf-8';
-
- /**
* @var string Response mime type.
* @since 11.3
*/
@@ -53,18 +40,6 @@ class JApplicationWeb
public $client;
/**
- * @var JRegistry The application configuration object.
- * @since 11.3
- */
- protected $config;
-
- /**
- * @var JDispatcher The application dispatcher object.
- * @since 11.3
- */
- protected $dispatcher;
-
- /**
* @var JDocument The application document object.
* @since 11.3
*/
@@ -599,118 +574,6 @@ public function redirect($url, $moved = false)
}
/**
- * Exit the application.
- *
- * @param integer $code The exit code (optional; default is 0).
- *
- * @return void
- *
- * @codeCoverageIgnore
- * @since 11.3
- */
- public function close($code = 0)
- {
- exit($code);
- }
-
- /**
- * Load an object or array into the application configuration object.
- *
- * @param mixed $data Either an array or object to be loaded into the configuration object.
- *
- * @return JApplicationWeb Instance of $this to allow chaining.
- *
- * @since 11.3
- */
- public function loadConfiguration($data)
- {
- // Load the data into the configuration object.
- if (is_array($data))
- {
- $this->config->loadArray($data);
- }
- elseif (is_object($data))
- {
- $this->config->loadObject($data);
- }
-
- return $this;
- }
-
- /**
- * Registers a handler to a particular event group.
- *
- * @param string $event The event name.
- * @param callback $handler The handler, a function or an instance of a event object.
- *
- * @return JApplicationWeb Instance of $this to allow chaining.
- *
- * @since 11.3
- */
- public function registerEvent($event, $handler)
- {
- if ($this->dispatcher instanceof JDispatcher)
- {
- $this->dispatcher->register($event, $handler);
- }
-
- return $this;
- }
-
- /**
- * Calls all handlers associated with an event group.
- *
- * @param string $event The event name.
- * @param array $args An array of arguments (optional).
- *
- * @return array An array of results from each function call, or null if no dispatcher is defined.
- *
- * @since 11.3
- */
- public function triggerEvent($event, array $args = null)
- {
- if ($this->dispatcher instanceof JDispatcher)
- {
- return $this->dispatcher->trigger($event, $args);
- }
-
- return null;
- }
-
- /**
- * Returns a property of the object or the default value if the property is not set.
- *
- * @param string $key The name of the property.
- * @param mixed $default The default value (optional) if none is set.
- *
- * @return mixed The value of the configuration.
- *
- * @since 11.3
- */
- public function get($key, $default = null)
- {
- return $this->config->get($key, $default);
- }
-
- /**
- * Modifies a property of the object, creating it if it does not already exist.
- *
- * @param string $key The name of the property.
- * @param mixed $value The value of the property to set (optional).
- *
- * @return mixed Previous value of the property
- *
- * @since 11.3
- */
- public function set($key, $value = null)
- {
- $previous = $this->config->get($key);
- $this->config->set($key, $value);
-
- return $previous;
- }
-
- /**
* Set/get cachable state for the response. If $allow is set, sets the cachable state of the
* response. Always returns the current state.
*
@@ -1004,53 +867,6 @@ protected function detectRequestUri()
}
/**
- * Method to load a PHP configuration class file based on convention and return the instantiated data object. You
- * will extend this method in child classes to provide configuration data from whatever data source is relevant
- * for your specific application.
- *
- * @param string $file The path and filename of the configuration file. If not provided, configuration.php
- * in JPATH_BASE will be used.
- * @param string $class The class name to instantiate.
- *
- * @return mixed Either an array or object to be loaded into the configuration object.
- *
- * @since 11.3
- */
- protected function fetchConfigurationData($file = '', $class = 'JConfig')
- {
- // Instantiate variables.
- $config = array();
-
- if (empty($file) && defined('JPATH_BASE'))
- {
- $file = JPATH_BASE . '/configuration.php';
-
- // Applications can choose not to have any configuration data
- // by not implementing this method and not having a config file.
- if (!file_exists($file))
- {
- $file = '';
- }
- }
-
- if (!empty($file))
- {
- JLoader::register($class, $file);
-
- if (class_exists($class))
- {
- $config = new $class;
- }
- else
- {
- throw new RuntimeException('Configuration class does not exist.');
- }
- }
-
- return $config;
- }
-
- /**
* Method to send a header to the client. We are wrapping this to isolate the header() function
* from our code base for testing reasons.
*
@@ -1072,20 +888,6 @@ protected function header($string, $replace = true, $code = null)
}
/**
- * Method to create an event dispatcher for the Web application. The logic and options for creating
- * this object are adequately generic for default cases but for many applications it will make sense
- * to override this method and create event dispatchers based on more specific needs.
- *
- * @return void
- *
- * @since 11.3
- */
- protected function loadDispatcher()
- {
- $this->dispatcher = JDispatcher::getInstance();
- }
-
- /**
* Method to create a document for the Web application. The logic and options for creating this
* object are adequately generic for default cases but for many applications it will make sense
* to override this method and create document objects based on more specific needs.
View
8 tests/suite/joomla/application/JApplicationDaemonTest.php
@@ -48,7 +48,7 @@ public function setUp()
$this->inspector = new JApplicationDaemonInspector;
$this->inspector->setClassInstance($this->inspector);
- //$this->config->set('max_memory_limit', '2048M');
+ // $this->config->set('max_memory_limit', '2048M');
// We are only coupled to Document and Language in JFactory.
$this->saveFactoryState();
@@ -248,13 +248,13 @@ public function testSignal()
}
/**
- * Tests the JApplicationDaemon::start method.
+ * Tests the JApplicationDaemon::execute method.
*
* @return void
*
* @since 11.3
*/
- public function testStart()
+ public function testExecute()
{
$this->markTestIncomplete();
}
@@ -275,7 +275,7 @@ public function testWriteProcessIdFile()
unlink($pidPath);
}
- // we set a custom process id file path so that we don't interfere
+ // We set a custom process id file path so that we don't interfere
// with other tests that are running on a system
$this->inspector->set('application_pid_file', $pidPath);
Please sign in to comment.
Something went wrong with that request. Please try again.