Skip to content

Instantly share code, notes, and snippets.

@dragoonis
Created May 7, 2012 23:39
Show Gist options
  • Save dragoonis/2631457 to your computer and use it in GitHub Desktop.
Save dragoonis/2631457 to your computer and use it in GitHub Desktop.
<?php
/**
* Inject services into our controller using setters matching against service names
*
* @return void
*/
public function injectServices() {
if($this->_serviceLocator === null) {
return;
}
// A bunch of public methods that should be omitted.
$blackList = array('setServiceLocator', 'getServiceLocator', 'injectServices');
$r = new \ReflectionClass($this);
foreach($r->getMethods(\ReflectionMethod::IS_PUBLIC) as $method) {
if(!in_array($method->name, $blackList)
&& substr($method->name, 0, 3) === 'set'
&& $this->_serviceLocator->has(($service = substr($method->name, 3)))
) {
$this->{$method->name}($this->_serviceLocator->get($service));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment