/** * method: autoloadResources * * todo: write documentation */ protected function autoloadResources() { // STEP 1: Autoload all resources from each plugin as they are requested foreach (Amslib_Plugin_Manager::listPlugin() as $name) { $p = Amslib_Plugin_Manager::getAPI($name); if ($p) { $p->autoloadResources(); } else { Amslib_Debug::log("plugin not found?", $p); } } $default = Amslib_Router::getRouteParam("plugin"); $route = Amslib_Router::getRoute(); // STEP 2: Autoload all resources which are bound the current route. // hack into place the adding or removing of all the stylesheets and javascripts foreach (Amslib_Array::valid(Amslib_Router::getJavascript()) as $j) { // datatables does't load because it's trying with the wrong plugin $name = isset($j["plugin"]) ? str_replace("__CURRENT_PLUGIN__", $route["group"], $j["plugin"]) : $default; $plugin = Amslib_Plugin_Manager::getAPI($name); if ($plugin) { if (isset($j["remove"])) { Amslib_Resource::removeJavascript($j["value"]); } else { $plugin->addJavascript($j["value"]); } } } foreach (Amslib_Array::valid(Amslib_Router::getStylesheet()) as $c) { $name = isset($c["plugin"]) ? str_replace("__CURRENT_PLUGIN__", $route["group"], $c["plugin"]) : $default; $plugin = Amslib_Plugin_Manager::getAPI($name); if ($plugin) { if (isset($c["remove"])) { Amslib_Resource::removeStylesheet($c["value"]); } else { $plugin->addStylesheet($c["value"]); } } } }
/** * method: getCallback * * Use the callback data to generate a callback usable in call_user_func* function. This * function can be provided with a fallback plugin or api object in case the detected plugin * doesn't exist. This is especially useful when the plugins are being loaded, it's not fully * installed in the system, but it's required to generate a callback * * params: * $callback - An array of data for the callback, or a string of comma separated parts * $fallback - Either null, a Plugin or API object * * returns: * - A callback in any case, if there is a problem with the data, it passed an exception * callback which will fail on execution * * variatons: * - array(plugin,object,method) * - array(plugin,method) * - "plugin,object,method => explode(,) => array(plugin,object,method) <-- array * - "plugin,method" => explode(,) => array(plugin,method) <-- array * - array(Object,method) <-- not static / array * - array(Class,method) <-- static / string * - "Class::method" <-- string * - "straightFunction" <-- string */ public static function getCallback($callback, $fallback = null) { $method = __METHOD__; // set output/invalid to be this default callback which throws an exception // this is so we can catch bad callbacks and trace them back to where they // where constructed $output = $invalid = function () use($method, $callback) { $args = func_get_args(); throw new Amslib_Exception("{$method}, callback was not valid", array("callback" => $callback instanceof Closure ? "was closure" : $callback, "arguments" => $args)); }; // supports: "plugin,object,method => array(plugin,object,method) // supports: "plugin,method" => array(plugin,method) if (is_string($callback) && strpos($callback, ",") !== false) { $callback = explode(",", $callback); } // supports: array(Object,method) // supports: array(plugin,object,method) // supports: array(plugin,method) if (is_array($callback) && in_array(count($callback), [2, 3])) { // The first element has to be an object or a plugin $plugin = array_shift($callback); // The last element in the array is always the method, if there is a third element // it will sit inside the middle, [plugin, >>>HERE<<<, method] and will be the object // But the third parameter is not necessary, if it's missing, the plugins API object is used $method = array_pop($callback); if (is_string($plugin) && !empty($plugin)) { // supports: array(plugin,object,method) // supports: array(plugin,method) $plugin = Amslib_Plugin_Manager::getAPI($plugin); } else { if (is_object($plugin)) { // supports: array(Object,method) $output = array($plugin, $method); } } if (!$plugin) { // supports: array(Class,method) if (class_exists($plugin)) { // The plugin was not found, therefore this must be a array(Class,Method) type call $output = array($plugin, $method); } else { if ($fallback instanceof Amslib_Plugin) { // If plugin is invalid and you were passed a plugin object $plugin = $fallback->getAPI(); } else { if ($fallback instanceof Amslib_MVC) { // If plugin is invalid and the method was passed an API object $plugin = $fallback; } } } } if ($plugin) { // Get the object to use in the callback $object = !empty($callback) ? array_shift($callback) : false; $object = !empty($object) && is_string($object) ? $plugin->getObject($object) : $plugin; // From the information, generate a new callback $output = array($object, $method); } } else { // supports: "Class::method" // supports: "theFunctionCall" // note: we don't need to do anything to support this, just fall into the else statement } // Return the callback, or a not_found default callback which throws an exception when called return is_callable($output) ? $output : $invalid; }
/** * method: getAPI * * todo: write documentation */ public function getAPI($name = NULL) { return $name ? Amslib_Plugin_Manager::getAPI($name) : $this; }