Skip to content

Instantly share code, notes, and snippets.

@robo47
Created December 10, 2011 09:07
Show Gist options
  • Save robo47/1454854 to your computer and use it in GitHub Desktop.
Save robo47/1454854 to your computer and use it in GitHub Desktop.
wordpress bridge generator
<?php
// @todo extract functions via tokenizer from files and create dummys including phpdoc
$data = array('admin-bar', 'author-template', 'bookmark', 'bookmark-template',
'canonical', 'capabilities', 'category', 'category-template', 'comment',
'comment-template', 'compat', 'cron', 'default-constants', 'default-widgets',
'deprecated', 'feed', 'formatting', 'functions.wp-scripts', 'functions.wp-styles',
'general-template', 'http', 'kses', 'l10n', 'link-template', 'load', 'media',
'meta', 'ms-blogs', 'ms-default-constants', 'ms-deprecated', 'ms-functions',
'ms-load', 'nav-menu', 'nav-menu-template', 'pluggable', 'pluggable-deprecated',
'plugin', 'post', 'post-template', 'post-thumbnail-template', 'query', 'rewrite',
'rss', 'shortcodes', 'taxonomy', 'theme', 'update', 'user', 'widgets',
);
$templates = array(
'service-container-configuration' => file_get_contents('./data/service-container-configuration.tpl'),
'service-container-params' => file_get_contents('./data/service-container-params.tpl'),
'class-template' => file_get_contents('./data/class-template.tpl'),
);
$output = array(
'service-container-configuration' => './data/service-container-configuration.php',
'service-container-params' => './data/service-container-params.php',
);
$generated_code = array(
'service-container-configuration' => '',
'service-container-params' => ''
);
// template engine *g*
$tpleng = function($vars, $template)
{
$search = array_keys($vars);
$replace = array_values($vars);
return str_replace($search, $replace, $template);
};
$service_container_code = '';
$service_container_params_out = '';
$before = get_defined_functions();
foreach ($data as $name)
{
$classname = camelize(str_replace('.', '_', $name));
$classnamelower = strtolower($classname);
$filename = $name . '.php';
$sourcefile = './vendor/wordpress/wp-includes/' . $filename;
$vars =
array(
'%classname%' => $classname,
'%classnamelower%' => $classnamelower,
'%filename%' => $filename,
);
$classcode = $tpleng($vars, $templates['class-template']);
$generated_code['service-container-configuration'] .= $tpleng($vars, $templates['service-container-configuration']);
$generated_code['service-container-params'] .= $tpleng($vars, $templates['service-container-params']);
file_put_contents('./data/out/' . $classname . '.php', $classcode);
}
file_put_contents($output['service-container-configuration'], $generated_code['service-container-configuration']);
file_put_contents($output['service-container-params'], $generated_code['service-container-params']);
/**
* @see http://www.php.net/manual/en/function.strtoupper.php#102939
* @param string $string
* @param boolean $pascalCase
* @return string
*/
function camelize($string, $pascalCase = true)
{
$string = str_replace(array('-', '_'), ' ', $string);
$string = ucwords($string);
$string = str_replace(' ', '', $string);
if (!$pascalCase) {
return lcfirst($string);
}
return $string;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment