| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
require_once(dirname(__FILE__).'/sfDoctrineBaseTask.class.php'); |
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
class sfDoctrineBuildModelTask extends sfDoctrineBaseTask |
|---|
| 24 |
{ |
|---|
| 25 |
|
|---|
| 26 |
* @see sfTask |
|---|
| 27 |
*/ |
|---|
| 28 |
protected function configure() |
|---|
| 29 |
{ |
|---|
| 30 |
$this->addOptions(array( |
|---|
| 31 |
new sfCommandOption('application', null, sfCommandOption::PARAMETER_OPTIONAL, 'The application name', true), |
|---|
| 32 |
new sfCommandOption('env', null, sfCommandOption::PARAMETER_REQUIRED, 'The environment', 'dev'), |
|---|
| 33 |
)); |
|---|
| 34 |
|
|---|
| 35 |
$this->aliases = array('doctrine-build-model'); |
|---|
| 36 |
$this->namespace = 'doctrine'; |
|---|
| 37 |
$this->name = 'build-model'; |
|---|
| 38 |
$this->briefDescription = 'Creates classes for the current model'; |
|---|
| 39 |
|
|---|
| 40 |
$this->detailedDescription = <<<EOF |
|---|
| 41 |
The [doctrine:build-model|INFO] task creates model classes from the schema: |
|---|
| 42 |
|
|---|
| 43 |
[./symfony doctrine:build-model|INFO] |
|---|
| 44 |
|
|---|
| 45 |
The task read the schema information in [config/doctrine/*.yml|COMMENT] |
|---|
| 46 |
from the project and all installed plugins. |
|---|
| 47 |
|
|---|
| 48 |
The model classes files are created in [lib/model/doctrine|COMMENT]. |
|---|
| 49 |
|
|---|
| 50 |
This task never overrides custom classes in [lib/model/doctrine|COMMENT]. |
|---|
| 51 |
It only replaces files in [lib/model/doctrine/base|COMMENT]. |
|---|
| 52 |
EOF; |
|---|
| 53 |
} |
|---|
| 54 |
|
|---|
| 55 |
|
|---|
| 56 |
* @see sfTask |
|---|
| 57 |
*/ |
|---|
| 58 |
protected function execute($arguments = array(), $options = array()) |
|---|
| 59 |
{ |
|---|
| 60 |
$this->logSection('doctrine', 'generating model classes'); |
|---|
| 61 |
|
|---|
| 62 |
$config = $this->getCliConfig(); |
|---|
| 63 |
|
|---|
| 64 |
$this->_checkForPackageParameter($config['yaml_schema_path']); |
|---|
| 65 |
|
|---|
| 66 |
$tmpPath = sfConfig::get('sf_cache_dir').DIRECTORY_SEPARATOR.'tmp'; |
|---|
| 67 |
|
|---|
| 68 |
if (!file_exists($tmpPath)) |
|---|
| 69 |
{ |
|---|
| 70 |
Doctrine_Lib::makeDirectories($tmpPath); |
|---|
| 71 |
} |
|---|
| 72 |
|
|---|
| 73 |
$plugins = $this->configuration->getPlugins(); |
|---|
| 74 |
foreach ($this->configuration->getAllPluginPaths() as $plugin => $path) |
|---|
| 75 |
{ |
|---|
| 76 |
if (!in_array($plugin, $plugins)) |
|---|
| 77 |
{ |
|---|
| 78 |
continue; |
|---|
| 79 |
} |
|---|
| 80 |
$schemas = sfFinder::type('file')->name('*.yml')->in($path.'/config/doctrine'); |
|---|
| 81 |
foreach ($schemas as $schema) |
|---|
| 82 |
{ |
|---|
| 83 |
$tmpSchemaPath = $tmpPath.DIRECTORY_SEPARATOR.$plugin.'-'.basename($schema); |
|---|
| 84 |
|
|---|
| 85 |
$models = Doctrine_Parser::load($schema, 'yml'); |
|---|
| 86 |
if (!isset($models['package'])) |
|---|
| 87 |
{ |
|---|
| 88 |
$models['package'] = $plugin.'.lib.model.doctrine'; |
|---|
| 89 |
$models['package_custom_path'] = $path.'/lib/model/doctrine'; |
|---|
| 90 |
} |
|---|
| 91 |
Doctrine_Parser::dump($models, 'yml', $tmpSchemaPath); |
|---|
| 92 |
} |
|---|
| 93 |
} |
|---|
| 94 |
|
|---|
| 95 |
$options = array('generateBaseClasses' => true, |
|---|
| 96 |
'generateTableClasses' => true, |
|---|
| 97 |
'packagesPath' => sfConfig::get('sf_plugins_dir'), |
|---|
| 98 |
'packagesPrefix' => 'Plugin', |
|---|
| 99 |
'suffix' => '.class.php', |
|---|
| 100 |
'baseClassesDirectory' => 'base', |
|---|
| 101 |
'baseClassName' => 'sfDoctrineRecord'); |
|---|
| 102 |
$options = array_merge($options, sfConfig::get('doctrine_model_builder_options', array())); |
|---|
| 103 |
|
|---|
| 104 |
$import = new Doctrine_Import_Schema(); |
|---|
| 105 |
$import->setOptions($options); |
|---|
| 106 |
$import->importSchema(array($tmpPath, $config['yaml_schema_path']), 'yml', $config['models_path']); |
|---|
| 107 |
} |
|---|
| 108 |
|
|---|
| 109 |
|
|---|
| 110 |
* Check for package parameter in main schema files. |
|---|
| 111 |
* sfDoctrinePlugin uses the package feature of Doctrine |
|---|
| 112 |
* for plugins and cannot be used by the user |
|---|
| 113 |
* |
|---|
| 114 |
* @param string $path |
|---|
| 115 |
* @return void |
|---|
| 116 |
*/ |
|---|
| 117 |
protected function _checkForPackageParameter($path) |
|---|
| 118 |
{ |
|---|
| 119 |
$files = sfFinder::type('file')->name('*.yml')->in($path); |
|---|
| 120 |
foreach ($files as $file) |
|---|
| 121 |
{ |
|---|
| 122 |
$array = sfYaml::load($file); |
|---|
| 123 |
if (is_array($array) AND !empty($array)) |
|---|
| 124 |
{ |
|---|
| 125 |
foreach ($array as $key => $value) |
|---|
| 126 |
{ |
|---|
| 127 |
if ($key == 'package' || (is_array($value) && isset($value['package']))) |
|---|
| 128 |
{ |
|---|
| 129 |
throw new sfDoctrineException( |
|---|
| 130 |
sprintf('Cannot use package parameter in symfony Doctrine schema files. Found in "%s"', $file) |
|---|
| 131 |
); |
|---|
| 132 |
} |
|---|
| 133 |
} |
|---|
| 134 |
} |
|---|
| 135 |
} |
|---|
| 136 |
} |
|---|
| 137 |
} |
|---|