Skip to content

Loading…

Move config methods to JApplicationBase #1732

Closed
wants to merge 1 commit into from

4 participants

@mbabker
Joomla! member

To clean up a little bit of duplicated code, this moves the get, set, loadConfiguration, and fetchConfigurationData methods from the child classes of JApplicationBase into the parent class itself.

@eddieajau

I think most of it is ok; it's perfectly reasonable to assume an application is going to need configuration out of the gate. The only problem I have is what to do with fetchConfigurationData. I don't think that should be in Base, and looking at it, I'm not sure it technically belongs in Web and Cli.

@dongilbert

What are the arguments against having it in the base class? Most (?) apps would require some sort of configuration data.

@eddieajau

The loadConfiguration method is fine. I'd be happier if fetchConfigurationData in Base was abstract.

@LouisLandry

Yep, I agree with @eddieajau on that. I am fine assuming that all apps will have configuration, but I think I'd rather have the fetchConfigurationData method be abstract. Continuing that it would make sense that it remain abstract in JApplicationWeb, JApplicationCli and JApplicationDaemon.

@mbabker
Joomla! member

Question - Were JApplicationWeb or JApplicationCli intended to be abstract, or is it reasonable to assume they could theoretically work on their own (as the code seems to have indicated for the last year)? Abstracting the method down to those classes would mean abstracting classes that aren't already, an interesting change in the API.

@LouisLandry

Ya I thought about that. Realistically you can't build anything useful without extending them, so I'm fine making them abstract.

@mbabker
Joomla! member

I'll do some work then to tweak this out and submit a new PR when that's ready.

@mbabker mbabker closed this
@mbabker mbabker deleted the unknown repository branch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Dec 3, 2012
  1. @mbabker
Showing with 110 additions and 214 deletions.
  1. +110 −0 libraries/joomla/application/base.php
  2. +0 −104 libraries/joomla/application/cli.php
  3. +0 −110 libraries/joomla/application/web.php
View
110 libraries/joomla/application/base.php
@@ -19,6 +19,12 @@
abstract class JApplicationBase
{
/**
+ * @var JRegistry The application configuration object.
+ * @since 12.3
+ */
+ protected $config;
+
+ /**
* The application dispatcher object.
*
* @var JEventDispatcher
@@ -58,6 +64,86 @@ public function close($code = 0)
}
/**
+ * 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.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;
+ }
+
+ /**
+ * 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 12.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 12.3
+ */
+ public function set($key, $value = null)
+ {
+ $previous = $this->config->get($key);
+ $this->config->set($key, $value);
+
+ return $previous;
+ }
+
+ /**
* Get the application identity.
*
* @return mixed A JUser object or null.
@@ -110,6 +196,30 @@ public function triggerEvent($event, array $args = null)
}
/**
+ * 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 Instance of $this to allow chaining.
+ *
+ * @since 12.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;
+ }
+
+ /**
* Allows the application to load a custom or default dispatcher.
*
* The logic and options for creating this object are adequately generic for default cases
View
104 libraries/joomla/application/cli.php
@@ -96,21 +96,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();
@@ -159,30 +144,6 @@ public function execute()
}
/**
- * 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)
- {
- // 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;
- }
-
- /**
* Write a string to standard output.
*
* @param string $text The text to display.
@@ -214,71 +175,6 @@ public function in()
}
/**
- * 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 run the application routines. Most likely you will want to instantiate a controller
* and execute it, or perform some sort of task directly.
*
View
110 libraries/joomla/application/web.php
@@ -43,12 +43,6 @@ class JApplicationWeb extends JApplicationBase
public $client;
/**
- * @var JRegistry The application configuration object.
- * @since 11.3
- */
- protected $config;
-
- /**
* @var JDocument The application document object.
* @since 11.3
*/
@@ -532,63 +526,6 @@ public function redirect($url, $moved = false)
}
/**
- * 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;
- }
-
- /**
- * 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.
*
@@ -881,53 +818,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.
*
Something went wrong with that request. Please try again.