Skip to content

Instantly share code, notes, and snippets.

@acelaya
Last active January 19, 2016 15:57
Show Gist options
  • Save acelaya/0aca60e3ec1921a53a5f to your computer and use it in GitHub Desktop.
Save acelaya/0aca60e3ec1921a53a5f to your computer and use it in GitHub Desktop.
AbstractFactories to create TableGateways
<?php
return [
'service_manager' => [
'abstract_factories' => [
'Application\Service\Factory\TableGatewayAbstractFactory',
'Application\Service\Factory\TableAbstractFactory',
]
]
]
<?php
namespace Application\Service\Factory;
use Zend\ServiceManager\AbstractFactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
class TableGatewayAbstractFactory implements AbstractFactoryInterface
{
/**
* Determine if we can create a service with name
*
* @param ServiceLocatorInterface $serviceLocator
* @param $name
* @param $requestedName
* @return bool
*/
public function canCreateServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
{
return strpos($name, 'Album\Model\\') === 0;
}
/**
* Create service with name
*
* @param ServiceLocatorInterface $serviceLocator
* @param $name
* @param $requestedName
* @return mixed
*/
public function createServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
{
$className = substr($name, strlen('Album\Model\\'));
$entityName = substr($className, 0, strpos($className, 'Table'));
$tableGateway = $serviceLocator->get('TableGateway\\' . $entityName);
return new $className($tableGateway);
}
}
<?php
namespace Application\Service\Factory;
use Zend\ServiceManager\AbstractFactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use Zend\Db\ResultSet\ResultSet;
use Application\Entity\User;
use Zend\Db\TableGateway\TableGateway;
class TableGatewayAbstractFactory implements AbstractFactoryInterface
{
/**
* Determine if we can create a service with name
*
* @param ServiceLocatorInterface $serviceLocator
* @param $name
* @param $requestedName
* @return bool
*/
public function canCreateServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
{
$array = explode('\\', $name);
return count($array) === 2 && $array[0] === 'TableGateway';
}
/**
* Create service with name
*
* @param ServiceLocatorInterface $serviceLocator
* @param $name
* @param $requestedName
* @return mixed
*/
public function createServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
{
list($tableGateway, $entityName) = explode('/', $name);
$tableName = strtotolower($entityName);
$entityName = '\Application\Entity\\' . $entityName;
$dbAdapter = $serviceLocator->get('Zend\Db\Adapter\Adapter');
$resultSetPrototype = new ResultSet();
$resultSetPrototype->setArrayObjectPrototype(new $entityName());
return new TableGateway($tableName, $dbAdapter, null, $resultSetPrototype);
}
}
<?php
$userTableGateway = $sm->get('TableGateway\User');
$albumTableGateway = $sm->get('TableGateway\Album');
$somethingTableGateway = $sm->get('TableGateway\Something');
// ...
$userTable = $sm->get('Album\Model\UserTable');
$albumTable = $sm->get('Album\Model\AlbumTable');
$somethingTable = $sm->get('Album\Model\SomethingTable');
// ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment