Skip to content

Instantly share code, notes, and snippets.

@weierophinney
Last active August 29, 2015 14:01
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 weierophinney/9d1145960eec6d202cdb to your computer and use it in GitHub Desktop.
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.
<?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 */
),
),
),
),
);
<?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