Created
September 27, 2012 02:06
-
-
Save adatta02/3791760 to your computer and use it in GitHub Desktop.
Supports doing a partial Doctrine rebuild in Symfony 1.4
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 | |
class FastModelRebuild { | |
// Just return some standard Symfony configs | |
private static function getConfig(){ | |
return array( | |
"data_fixtures_path" => array( sfConfig::get("sf_data_dir") . "/fixtures" ), | |
"models_path" => sfConfig::get("sf_lib_dir") . "/model/doctrine", | |
"migrations_path" => sfConfig::get("sf_lib_dir") . "/migration/doctrine", | |
"sql_path" => sfConfig::get("sf_data_dir") . "/sql", | |
"yaml_schema_path" => sfConfig::get("sf_config_dir") . "/doctrine" | |
); | |
} | |
/* | |
* This is the main function. Just pass in an array of names of models to rebuild. | |
*/ | |
public static function doRebuild( $modelArray ){ | |
// Grab the schema file and convert it to an array | |
$schemaYAML = sfYaml::load( sfConfig::get("sf_config_dir") . "/doctrine/schema.yml" ); | |
$tempYAML = array(); | |
// Pull out only the models that need to get rebuilt | |
foreach( $modelArray as $key ){ | |
$tempYAML[ $key ] = $schemaYAML[ $key ]; | |
} | |
// Create a temporary file for the partial schema | |
// For some reason it needs a ".yml" extension (don't know why) | |
$schema = tempnam( sys_get_temp_dir() , "sf" ) . ".yml"; | |
file_put_contents( $schema, sfYaml::dump($tempYAML, 10) ); | |
self::doModelRebuild( $schema ); | |
sfAutoload::getInstance()->reloadClasses(true); | |
self::doFormRebuild( $schema ); | |
unlink( $schema ); | |
// Clear the cache to reset the autoloader | |
exec( "/usr/bin/php " . sfConfig::get("sf_root_dir") . "/symfony cc --app=frontend" ); | |
} | |
private static function doFormRebuild( $schema ){ | |
$config = sfContext::getInstance()->getConfiguration(); | |
$config->fastRebuildSchema = $schema; | |
$databaseManager = new sfDatabaseManager( $config ); | |
$generatorManager = new sfGeneratorManager( $config ); | |
$generatorManager->generate("sfDoctrineFormGeneratorFast", array( | |
'model_dir_name' => "model", | |
'form_dir_name' => "form", | |
)); | |
$properties = parse_ini_file(sfConfig::get('sf_config_dir').DIRECTORY_SEPARATOR.'properties.ini', true); | |
$constants = array( | |
'PROJECT_NAME' => isset($properties['symfony']['name']) ? $properties['symfony']['name'] : 'symfony', | |
'AUTHOR_NAME' => isset($properties['symfony']['author']) ? $properties['symfony']['author'] : 'Your name here' | |
); | |
// customize php and yml files | |
$finder = sfFinder::type('file')->name('*.php'); | |
$filesystem = new sfFilesystem(); | |
$filesystem->replaceTokens($finder->in(sfConfig::get('sf_lib_dir').'/form/'), '##', '##', $constants); | |
} | |
private static function doModelRebuild( $schema ){ | |
$config = self::getConfig(); | |
$builderOptions = sfContext::getInstance()->getConfiguration()->getPluginConfiguration('sfDoctrinePlugin')->getModelBuilderOptions(); | |
$stubFinder = sfFinder::type('file')->prune('base')->name('*'.$builderOptions['suffix']); | |
$before = $stubFinder->in($config['models_path']); | |
$import = new Doctrine_Import_Schema(); | |
$import->setOptions($builderOptions); | |
$import->importSchema($schema, 'yml', $config['models_path']); | |
// markup base classes with magic methods | |
foreach (sfYaml::load($schema) as $model => $definition) | |
{ | |
$file = sprintf('%s%s/%s/Base%s%s', $config['models_path'], isset($definition['package']) ? '/'.substr($definition['package'], 0, strpos($definition['package'], '.')) : '', $builderOptions['baseClassesDirectory'], $model, $builderOptions['suffix']); | |
$code = file_get_contents($file); | |
// introspect the model without loading the class | |
if (preg_match_all('/@property (\w+) \$(\w+)/', $code, $matches, PREG_SET_ORDER)) | |
{ | |
$properties = array(); | |
foreach ($matches as $match) | |
{ | |
$properties[$match[2]] = $match[1]; | |
} | |
$typePad = max(array_map('strlen', array_merge(array_values($properties), array($model)))); | |
$namePad = max(array_map('strlen', array_keys(array_map(array('sfInflector', 'camelize'), $properties)))); | |
$setters = array(); | |
$getters = array(); | |
foreach ($properties as $name => $type) | |
{ | |
$camelized = sfInflector::camelize($name); | |
$collection = 'Doctrine_Collection' == $type; | |
$getters[] = sprintf('@method %-'.$typePad.'s %s%-'.($namePad + 2).'s Returns the current record\'s "%s" %s', $type, 'get', $camelized.'()', $name, $collection ? 'collection' : 'value'); | |
$setters[] = sprintf('@method %-'.$typePad.'s %s%-'.($namePad + 2).'s Sets the current record\'s "%s" %s', $model, 'set', $camelized.'()', $name, $collection ? 'collection' : 'value'); | |
} | |
// use the last match as a search string | |
$code = str_replace($match[0], $match[0].PHP_EOL.' * '.PHP_EOL.' * '.implode(PHP_EOL.' * ', array_merge($getters, $setters)), $code); | |
file_put_contents($file, $code); | |
} | |
} | |
$properties = parse_ini_file(sfConfig::get('sf_config_dir').'/properties.ini', true); | |
$tokens = array( | |
'##PACKAGE##' => isset($properties['symfony']['name']) ? $properties['symfony']['name'] : 'symfony', | |
'##SUBPACKAGE##' => 'model', | |
'##NAME##' => isset($properties['symfony']['author']) ? $properties['symfony']['author'] : 'Your name here', | |
' <##EMAIL##>' => '', | |
"{\n\n}" => "{\n}\n", | |
); | |
// cleanup new stub classes | |
$after = $stubFinder->in($config['models_path']); | |
$filesystem = new sfFilesystem(); | |
$filesystem->replaceTokens(array_diff($after, $before), '', '', $tokens); | |
// cleanup base classes | |
$baseFinder = sfFinder::type('file')->name('Base*'.$builderOptions['suffix']); | |
$baseDirFinder = sfFinder::type('dir')->name('base'); | |
$filesystem->replaceTokens($baseFinder->in($baseDirFinder->in($config['models_path'])), '', '', $tokens); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment