namespace SciActive; // An example of how you could use RequirePHP as a service locator. require "src/RequirePHP.php"; // Define service. RequirePHP::_('service', array(), function () { class service { private $value = 0; public function increment() { $this->value++; } public function printOut() { echo $this->value; } } return new service(); }); // Locate the service. RequirePHP::_(array('service'), function (service $service) { $service->increment(); }); // Locate the service again. RequirePHP::_(array('service'), function (service $service) { $service->increment(); echo 'You should see "2".<br>'; $service->printOut(); });
return; }); RequirePHP::_('circ2', array('circ1'), function ($circ1) { return; }); RequirePHP::_(array('circ1'), function ($circ1) { echo '<p>This shouldn\'t have run!!</p>'; }); } catch (RequireTooDeepException $e) { RequirePHP::undef('circ1'); echo '<p>Circular dependencies don\'t crash the script!! Yay!! See the message: ' . $e->getMessage() . '</p>'; } // Let's test alias and module removal. RequirePHP::_('removemodule', array(), function () { echo '<p>Uh oh. Module removal failed. :(</p>'; }); RequirePHP::alias('removealias', 'removemodule'); RequirePHP::undefAlias('removealias'); RequirePHP::_(array('removealias'), function () { echo '<p>Uh oh. Alias removal failed. :(</p>'; }); $failed = false; RequirePHP::undef('removemodule'); RequirePHP::_(array('removemodule'), function () { global $failed; $failed = true; return; }); if (!$failed) { echo '<p>Looks like alias and module removal passed! :)</p>'; }
private static function runFunctions() { foreach (RequirePHP::$functions as $key => $function) { // Load the required modules. $arguments = array(); foreach ($function['requires'] as $require) { $require = RequirePHP::parseAlias($require); if (!RequirePHP::runModule($require)) { continue 2; } $arguments[] = RequirePHP::$modules[$require]['return']; // Add this return value to the arguments. We'll pass it to the callback. } call_user_func_array($function['function'], $arguments); unset(RequirePHP::$functions[$key]); } }