Skip to content

Instantly share code, notes, and snippets.

@cgmartin
Created November 27, 2012 20:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cgmartin/4156849 to your computer and use it in GitHub Desktop.
Save cgmartin/4156849 to your computer and use it in GitHub Desktop.
Create common factories with closures
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