Created
November 27, 2012 20:37
-
-
Save cgmartin/4156849 to your computer and use it in GitHub Desktop.
Create common factories with closures
This file contains 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
protected function createTableFactory($tableName) { | |
return function($sm) use ($tableName) { | |
$tableGateway = $sm->get(ucfirst($tableName) . 'TableGateway'); | |
$tableClass = ucfirst($tableName) . 'Table'; | |
$table = new $tableClass($tableGateway); | |
}; | |
} | |
protected function createTableGatewayFactory($tableName) { | |
return function($sm) use ($tableName) { | |
$dbAdapter = $sm->get('Zend\Db\Adapter\Adapter'); | |
$resultSetPrototype = new ResultSet(); | |
$modelName = ucfirst($tableName) . 'Model'; | |
$resultSetPrototype->setArrayObjectPrototype(new $modelName()); | |
return new TableGateway($tableName, $dbAdapter, null, $resultSetPrototype); | |
}; | |
} | |
public function getServiceConfig() | |
{ | |
return array( | |
'factories' => array( | |
'Admin\Model\CountriesTable' => $this->createTableFactory('countries'), | |
'CountriesTableGateway' => $this->createTableGatewayFactory('countries'), | |
'Admin\Model\StatesTable' => $this->createTableFactory('states'), | |
'StatesTableGateway' => $this->createTableGatewayFactory('states'), | |
) | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment