Created
November 20, 2012 13:47
-
-
Save gabriel403/4118049 to your computer and use it in GitHub Desktop.
SM with factories and SM config in application.ini
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
servicemanager.factory.differenceService = DifferenceServiceFactory | |
servicemanager.factory.differenceDatabase = DifferenceDatabaseFactory | |
servicemanager.factory.tableManager = TableManagerFactory |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public function preDispatch() | |
{ | |
$bootstrap = $this->getInvokeArg('bootstrap'); | |
$aConfig = $bootstrap->getOptions(); | |
$this->smConfig = $aConfig['servicemanager'] | |
parent::preDispatch(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class DifferenceDatabaseFactory implements FactoryInterface { | |
public createService(ServiceManager $sm) { | |
$tableman = $sm->get('tableManager'); | |
$table = $tableman->get('differencetable'); | |
$diffdb = new DifferenceDatabase($table); | |
return $diffdb; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class DifferenceServiceFactory implements FactoryInterface { | |
public createService(ServiceManager $sm) { | |
$diffdb = $sm->get('differenceDatabase'); | |
$diff = new DifferenceService(); | |
$diff->setDb($diffdb); | |
return $diff; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface FactoryInterface | |
{ | |
public function createService(ServiceManager $serviceLocator); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ServiceManager implements ServiceManagerInterface | |
{ | |
protected function __construct() | |
{ | |
//get config somehow | |
//set $this->services to config | |
} | |
public function set($serviceName, $serviceClass) | |
{ | |
$this->services[$serviceName] = $serviceClass; | |
} | |
public function get($service) | |
{ | |
$serviceimp = new $services[$service]; | |
$serviceobj = $serviceimp->createService($this); | |
if ($serviceobj instanceof ServiceManagerAwareInterface) | |
{ | |
$serviceobj->setServiceManager($this); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface ServiceManagerAwareInterface | |
{ | |
public function setServiceManager(ServiceManager $sm); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
abstact class ServiceManagerAwareStaticAbstract | |
{ | |
protected $servicemanager; | |
public __construct() | |
{ | |
$this->servicemanager = ServiceManager::getInstance(); | |
call_user_func_array(array('parent', '__construct'), func_get_args()); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface ServiceManagerInterface { | |
protected $services = []; | |
protected function __construct(); | |
public function set($serviceName, $serviceClass); | |
public function get($service); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment