Permalink
Browse files

feature(application): adds elgg() and makes Application a service pro…

…vider

elgg() provides access to the booted Application instance which will
provide access to future public API service objects (instead of global
functions).

As a first step, this adds only the “config” service, which is needed to
use the method getVolatile() before site boot.
  • Loading branch information...
mrclay committed May 9, 2015
1 parent 9587ae0 commit d43de92fe0a10673fd28ed2cb4b209751e9a4d17
@@ -26,6 +26,27 @@ class Application {
*/
private $install_dir;
+ /**
+ * Property names of the service provider to be exposed via __get()
+ *
+ * E.g. the presence of `'foo' => true` in the list would allow _elgg_services()->foo to
+ * be accessed via elgg()->foo.
+ *
+ * @var string[]
+ */
+ private static $public_services = [
+ 'config' => true,
+ ];
+
+ /**
+ * Reference to the loaded Application returned by elgg()
+ *
+ * @internal Do not use this. use elgg() to access the application
+ * @access private
+ * @var Application
+ */
+ public static $_instance;
+
/**
* Constructor
*
@@ -53,19 +74,6 @@ public function __construct(ServiceProvider $services = null) {
$this->install_dir = dirname($this->engine_dir);
}
- /**
- * Get the service provider
- *
- * Developers should not use this function
- *
- * @return ServiceProvider
- * @internal
- * @access private
- */
- public function getServices() {
- return $this->services;
- }
-
/**
* Load settings.php
*
@@ -78,18 +86,6 @@ public function loadSettings() {
$this->services->config->loadSettingsFile();
}
- /**
- * Get the Elgg\Config object (not global $CONFIG)
- *
- * @note Before application boot, it may be unsafe to call Elgg\Config::get for some values. You should use
- * Elgg\Config::getVolatile before system boot.
- *
- * @return Config
- */
- public function getConfig() {
- return $this->services->config;
- }
-
/**
* Load all Elgg procedural code and wire up boot events, but don't boot
*
@@ -100,7 +96,7 @@ public function getConfig() {
* @internal
*/
public function loadCore() {
- if (function_exists('_elgg_services')) {
+ if (function_exists('elgg')) {
return;
}
@@ -183,6 +179,9 @@ public function loadCore() {
}
}
+ // store instance to be returned by elgg()
+ self::$_instance = $this;
+
$events = $this->services->events;
$hooks = $this->services->hooks;
@@ -254,7 +253,7 @@ public function bootCore() {
}
/**
- * Get the Database instance for performing queries with minimal resources loaded
+ * Get the Database instance for performing queries without booting Elgg
*
* If settings.php has not been loaded, it will be loaded to configure the DB connection.
*
@@ -346,4 +345,18 @@ public function run() {
return true;
}
+
+ /**
+ * Get an undefined property
+ *
+ * @param string $name The property name accessed
+ *
+ * @return mixed
+ */
+ public function __get($name) {
+ if (isset(self::$public_services[$name])) {
+ return $this->services->{$name};
+ }
+ trigger_error("Undefined property: " . __CLASS__ . ":\${$name}");
+ }
}
@@ -32,7 +32,7 @@ public function __construct(Application $app) {
* @return void
*/
public function handleRequest($get_vars, $server_vars) {
- $config = $this->application->getConfig();
+ $config = $this->application->config;
if (empty($get_vars['request'])) {
$this->send403();
@@ -143,7 +143,7 @@ public function parseRequestVar($request_var) {
*/
protected function setupSimplecache() {
// we can't use Elgg\Config::get yet. It fails before the core is booted
- $config = $this->application->getConfig();
+ $config = $this->application->config;
$config->loadSettingsFile();
if ($config->getVolatile('dataroot') && $config->getVolatile('simplecache_enabled') !== null) {
@@ -246,15 +246,15 @@ protected function renderView($view, $viewtype) {
}
// disable error reporting so we don't cache problems
- $this->application->getConfig()->set('debug', null);
+ $this->application->config->set('debug', null);
// @todo elgg_view() checks if the page set is done (isset($CONFIG->pagesetupdone)) and
// triggers an event if it's not. Calling elgg_view() here breaks submenus
// (at least) because the page setup hook is called before any
// contexts can be correctly set (since this is called before page_handler()).
// To avoid this, lie about $CONFIG->pagehandlerdone to force
// the trigger correctly when the first view is actually being output.
- $this->application->getConfig()->set('pagesetupdone', true);
+ $this->application->config->set('pagesetupdone', true);
return elgg_view($view);
}
@@ -3,13 +3,9 @@
/**
- * WARNING: API IN FLUX. DO NOT USE DIRECTLY.
+ * Access to configuration values
*
- * @access private
- *
- * @package Elgg.Core
- * @subpackage Config
- * @since 1.10.0
+ * @since 1.10.0
*/
class Config {
/**
@@ -27,6 +23,8 @@ class Config {
/**
* Constructor
*
+ * @internal Access this object via Elgg\Application::$config
+ *
* @param \stdClass $config Elgg's $CONFIG object
* @param bool $set_global Copy the config object to global $CONFIG
*/
@@ -106,6 +104,9 @@ public function getRootPath() {
/**
* Get an Elgg configuration value, possibly loading it from the DB's config table
*
+ * Before application boot, it may be unsafe to call get() for some values. You should use
+ * getVolatile() before system boot.
+ *
* @param string $name Name of the configuration value
* @param int $site_guid null for installation setting, 0 for default site
*
@@ -2,10 +2,8 @@
namespace Elgg;
/**
- * PRIVATE CLASS. API IN FLUX. DO NOT USE DIRECTLY.
- *
- * PLUGIN DEVELOPERS SHOULD USE elgg_*_context FUNCTIONS INSTEAD.
- *
+ * Manages a global stack of strings for sharing information about the current execution context
+ *
* Views can modify their output based on the local context. You may want to
* display a list of blogs on a blog page or in a small widget. The rendered
* output could be different for those two contexts ('blog' vs 'widget').
@@ -19,10 +17,9 @@
*
* @warning The context is not available until the page_handler runs (after
* the 'init, system' event processing has completed).
- *
- * @package Elgg.Core
- * @access private
- * @since 1.10.0
+ *
+ * @access private
+ * @since 1.10.0
*/
final class Context {
@@ -61,4 +61,70 @@ public function trigger($event, $type, $object = null, array $options = array())
return true;
}
+
+ /**
+ * Trigger a "Before event" indicating a process is about to begin.
+ *
+ * Like regular events, a handler returning false will cancel the process and false
+ * will be returned.
+ *
+ * To register for a before event, append ":before" to the event name when registering.
+ *
+ * @param string $event The event type. The fired event type will be appended with ":before".
+ * @param string $object_type The object type
+ * @param string $object The object involved in the event
+ *
+ * @return bool False if any handler returned false, otherwise true
+ *
+ * @see trigger
+ * @see triggerAfter
+ * @since 2.0.0
+ */
+ function triggerBefore($event, $object_type, $object = null) {
+ return $this->trigger("$event:before", $object_type, $object);
+ }
+
+ /**
+ * Trigger an "After event" indicating a process has finished.
+ *
+ * Unlike regular events, all the handlers will be called, their return values ignored.
+ *
+ * To register for an after event, append ":after" to the event name when registering.
+ *
+ * @param string $event The event type. The fired event type will be appended with ":after".
+ * @param string $object_type The object type
+ * @param string $object The object involved in the event
+ *
+ * @return true
+ *
+ * @see triggerBefore
+ * @since 2.0.0
+ */
+ public function triggerAfter($event, $object_type, $object = null) {
+ $options = array(
+ self::OPTION_STOPPABLE => false,
+ );
+ return $this->trigger("$event:after", $object_type, $object, $options);
+ }
+
+ /**
+ * Trigger an event normally, but send a notice about deprecated use if any handlers are registered.
+ *
+ * @param string $event The event type
+ * @param string $object_type The object type
+ * @param string $object The object involved in the event
+ * @param string $message The deprecation message
+ * @param string $version Human-readable *release* version: 1.9, 1.10, ...
+ *
+ * @return bool
+ *
+ * @see trigger
+ */
+ function triggerDeprecated($event, $object_type, $object = null, $message, $version) {
+ $options = array(
+ self::OPTION_DEPRECATION_MESSAGE => $message,
+ self::OPTION_DEPRECATION_VERSION => $version,
+ );
+ return $this->trigger($event, $object_type, $object, $options);
+ }
}
View
@@ -7,6 +7,16 @@
* purposes and subsystems. Many of them should be moved to more relevant files.
*/
+/**
+ * Get a reference to the global Application object
+ *
+ * @return Elgg\Application
+ * @since 2.0.0
+ */
+function elgg() {
+ return Elgg\Application::$_instance;
+}
+
/**
* Register a PHP file as a library.
*
@@ -592,7 +602,7 @@ function elgg_trigger_event($event, $object_type, $object = null) {
* @see elgg_trigger_after_event
*/
function elgg_trigger_before_event($event, $object_type, $object = null) {
- return _elgg_services()->events->trigger("$event:before", $object_type, $object);
+ return _elgg_services()->events->triggerBefore($event, $object_type, $object);
}
/**
@@ -611,10 +621,7 @@ function elgg_trigger_before_event($event, $object_type, $object = null) {
* @see elgg_trigger_before_event
*/
function elgg_trigger_after_event($event, $object_type, $object = null) {
- $options = array(
- \Elgg\EventsService::OPTION_STOPPABLE => false,
- );
- return _elgg_services()->events->trigger("$event:after", $object_type, $object, $options);
+ return _elgg_services()->events->triggerAfter($event, $object_type, $object);
}
/**
@@ -631,11 +638,7 @@ function elgg_trigger_after_event($event, $object_type, $object = null) {
* @see elgg_trigger_event
*/
function elgg_trigger_deprecated_event($event, $object_type, $object = null, $message, $version) {
- $options = array(
- \Elgg\EventsService::OPTION_DEPRECATION_MESSAGE => $message,
- \Elgg\EventsService::OPTION_DEPRECATION_VERSION => $version,
- );
- return _elgg_services()->events->trigger($event, $object_type, $object, $options);
+ return _elgg_services()->events->triggerDeprecated($event, $object_type, $object, $message, $version);
}
/**
View
@@ -12,25 +12,22 @@
*
* @param int $site_guid Optional. Site GUID.
*
- * @return \ElggSite
+ * @return \ElggSite|false
* @since 1.8.0
*/
function elgg_get_site_entity($site_guid = 0) {
global $CONFIG;
- $result = false;
-
if ($site_guid == 0) {
- $site = $CONFIG->site;
- } else {
- $site = get_entity($site_guid);
+ return $CONFIG->site;
}
-
- if ($site instanceof \ElggSite) {
- $result = $site;
+
+ $site = _elgg_services()->entityTable->get($site_guid);
+ if (!$site instanceof \ElggSite) {
+ return false;
}
- return $result;
+ return $site;
}
/**
Oops, something went wrong.

0 comments on commit d43de92

Please sign in to comment.