Skip to content

Instantly share code, notes, and snippets.

@abcarroll
Created December 16, 2015 23:18
Show Gist options
  • Save abcarroll/98b0016ac4e60a333ad3 to your computer and use it in GitHub Desktop.
Save abcarroll/98b0016ac4e60a333ad3 to your computer and use it in GitHub Desktop.
<?php
/**
* Class Bootstrap
*/
class Bootstrapper {
/**
* Whether or not to directly 'echo' debugging information. This is so close to the start procedures, logging facilities are not yet created.
* @const bool VERBOSE
*/
const VERBOSE = true;
/**
* Returns the CLI argument-specified or default bootstrap configuration file's array.
*
* @return array
*/
static public function getDependenciesArray() {
if(empty($argv[1])) {
$bootstrapFile = 'conf/bootstrap.php';
} else {
$bootstrapFile = $argv[1];
}
return include($bootstrapFile);
}
/**
* @return array An associative array containing the loaded & injected dependencies
* @throws \Exception
*/
static public function loadDependencies(Array $bootstrap) {
// This injects everything with everything.
// It's a bit over-engineered, I've been told.
if(!empty($bootstrap)) {
$dependencies = [];
$dependencyQueue = [];
// Initialize
foreach($bootstrap as $depName => $depCfg) {
if(self::VERBOSE) {
echo " > Creating object '$depName' as '{$depCfg['class']}'\n";
}
if(isset($bootstrap['constructorInject']) && $bootstrap['constructorInject'] === true) {
$dependencies[$depName] = new $depCfg['class']($dependencies);
} else {
$dependencies[$depName] = new $depCfg['class']();
}
// Each module that this dependency requests
if(!empty($depCfg['inject'])) {
foreach($depCfg['inject'] as $injName) {
if(isset($dependencies[$injName])) {
// The requested dependency is already initiated
if(method_exists($dependencies[$depName], 'set' . $injName)) {
if(self::VERBOSE) {
echo " >> Injecting '$injName' into '$depName' via setX() method\n";
}
$dependencies[$depName]->{'set' . $injName}($dependencies[$injName]);
} elseif(method_exists($dependencies[$depName], 'inject')) {
if(self::VERBOSE) {
echo " >> Injecting '$injName' into '$depName' via Injectable inject() method\n";
}
$dependencies[$depName]->inject($injName, $dependencies[$injName]);
} else {
throw new \ErrorException("No way to inject $injName into $depName!");
}
} else {
// Else, add it to the queue for when it is initiated
if(self::VERBOSE) {
echo " >> Queueing $injName to be injected into $depName\n";
}
$dependencyQueue [$injName][] = $depName;
}
}
}
// Each module that has requested this dependency before it was initiated ($dependencyQueue)
if(!empty($dependencyQueue[$depName]) && count($dependencyQueue[$depName]) > 0) {
foreach($dependencyQueue[$depName] as $injectIn) {
if(self::VERBOSE) {
echo " >> Late-Injecting $depName into $injectIn\n";
}
$dependencies [$injectIn]->{'set' . $depName}($dependencies[$depName]);
}
}
}
} else {
throw new \Exception("Missing bootstrap file or it doesn't provide a \$bootstrap array");
}
return $dependencies;
}
// Some irrelevant junk removed here
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment