Skip to content

Instantly share code, notes, and snippets.

@christianchristensen
Created May 15, 2012 13:12
Show Gist options
  • Save christianchristensen/2701646 to your computer and use it in GitHub Desktop.
Save christianchristensen/2701646 to your computer and use it in GitHub Desktop.
Services OOP code generator
<?php
/**
* @file
* Drush integration for Services OOP.
*/
/**
* Implementation of hook_drush_help().
*/
function services_oop_drush_help($section) {
switch ($section) {
case 'drush:services_oop-generate-annotations':
return dt('Output Services OOP annotations based off of existing services array definitions.');
}
}
/**
* Implementation of hook_drush_command().
*/
function services_oop_drush_command() {
$items['services_oop-generate-annotations'] = array(
'description' => 'Generate services_oop annotations from existing array definition.',
// 'arguments' => array(
// 'filter' => '@todo filter by specific class/resource definition.',
// ),
'aliases' => array('soga'),
);
return $items;
}
function drush_services_oop_generate_annotations() {
// @todo Move these string builds to template layer
$class = <<<CLASSEOL
/**
* Class that defines @classname resource
*
@taResAnnotation
*/
class @classnameResource {
@methods
}
CLASSEOL;
foreach (services_get_resources() as $name => $resource) {
$methods = '';
$taResAnnotation = '';
foreach ($resource as $verb => $definition) {
if (in_array($verb, array('create', 'retrieve', 'update', 'delete', 'index'))) {
$methods .= _drush_services_oop_generate_annotations_methods($verb, $definition);
}
elseif ($verb == 'targeted_actions') {
foreach ($definition as $taVerb => $taDefinition) {
$methods .= _drush_services_oop_generate_annotations_methods($taVerb, $taDefinition);
$taResAnnotation .= " * @TargetedAction(name=$taVerb)\n";
}
}
elseif ($verb == 'relationships') {
foreach ($definition as $relVerb => $relDefinition) {
$methods .= _drush_services_oop_generate_annotations_methods($relVerb, $relDefinition);
$taResAnnotation .= " * @Relationship(name=$relVerb)\n";
}
}
}
$replacements = array(
'@classname' => trim($name),
'@taResAnnotation' => trim($taResAnnotation),
'@methods' => trim($methods),
);
print_r(t($class, $replacements));
}
}
function _drush_services_oop_generate_annotations_methods($name, $definition) {
$method = <<<METHODEOL
/**
*
*/
public static function @methodname(@methodargs) {
}
METHODEOL;
$replacements = array(
'@methodname' => $name,
//''
);
return t($method, $replacements);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment