Last active
August 29, 2015 14:01
-
-
Save weierophinney/9d1145960eec6d202cdb to your computer and use it in GitHub Desktop.
delegator for injecting TableGateway features into TableGateways created as part of the Apigility DB-Connected functionality.
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
<?php | |
return array( | |
/* ... */ | |
'service_manager' => array( | |
/* ... */ | |
'delegators' => array( | |
'Some\V1\Rest\Foo\FooResource\Table' => array( | |
'Some\TableGatewayFeaturesDelegatorFactory', | |
), | |
), | |
), | |
/* ... */ | |
'zf-apigility' => array( | |
'db-connected' => array( | |
/* ... */ | |
'Some\V1\Rest\Foo\FooResource' => array( | |
'features' => array( | |
/* assoc array of feature name => options */ | |
), | |
), | |
), | |
), | |
); |
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
<?php | |
use Zend\Db\TableGateway\Feature; | |
use Zend\ServiceManager\DelegatorFactoryInterface; | |
use Zend\ServiceManager\ServiceLocatorInterface; | |
class TableGatewayFeaturesDelegatorFactory implements DelegatorFactoryInterface | |
{ | |
public function createDelegatorWithName( | |
ServiceLocatorInterface $services, | |
$name, | |
$requestedName, | |
$callback | |
) { | |
$table = $callback(); | |
$config = $services->get('Config'); | |
// TableGateway service for DB-Connected ends in "\\Table"; strip that | |
$resourceName = substr($requestedName, 0, strlen($requestedName) - 6); | |
$config = $config['zf-apigility']['db-connected'][$resourceName]; | |
if (! isset($config['features'])) { | |
return $table; | |
} | |
$featureSet = $table->getFeatureSet(); | |
foreach ($config['features'] as $featureName => $options) { | |
// logic to create feature... | |
switch ($featureName) { | |
case 'sequence': | |
$feature = new Feature\SequenceFeature( | |
$options['primaryKeyField'], | |
$options['sequenceName'] | |
); | |
break; | |
default: | |
// Unknown feature; do nothing | |
continue; | |
} | |
// and then add it to the feature-set | |
$featureSet->addFeature($feature); | |
} | |
return $table; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment