Development

/branches/1.3/lib/plugins/sfDoctrinePlugin/lib/task/sfDoctrineBuildModelTask.class.php

You must first sign up to be able to contribute.

root/branches/1.3/lib/plugins/sfDoctrinePlugin/lib/task/sfDoctrineBuildModelTask.class.php

Revision 30901, 5.0 kB (checked in by Kris.Wallsmith, 4 years ago)

[1.3, 1.4] reverted remove of comments in Doctrine-generated table classes (closes #8880)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
Line 
1 <?php
2
3 /*
4  * This file is part of the symfony package.
5  * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
6  * (c) Jonathan H. Wage <jonwage@gmail.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 require_once(dirname(__FILE__).'/sfDoctrineBaseTask.class.php');
13
14 /**
15  * Create classes for the current model.
16  *
17  * @package    symfony
18  * @subpackage doctrine
19  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
20  * @author     Jonathan H. Wage <jonwage@gmail.com>
21  * @version    SVN: $Id$
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 enabled 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     $builderOptions = $this->configuration->getPluginConfiguration('sfDoctrinePlugin')->getModelBuilderOptions();
64
65     $stubFinder = sfFinder::type('file')->prune('base')->name('*'.$builderOptions['suffix']);
66     $before = $stubFinder->in($config['models_path']);
67
68     $schema = $this->prepareSchemaFile($config['yaml_schema_path']);
69
70     $import = new Doctrine_Import_Schema();
71     $import->setOptions($builderOptions);
72     $import->importSchema($schema, 'yml', $config['models_path']);
73
74     // markup base classes with magic methods
75     foreach (sfYaml::load($schema) as $model => $definition)
76     {
77       $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']);
78       $code = file_get_contents($file);
79
80       // introspect the model without loading the class
81       if (preg_match_all('/@property (\w+) \$(\w+)/', $code, $matches, PREG_SET_ORDER))
82       {
83         $properties = array();
84         foreach ($matches as $match)
85         {
86           $properties[$match[2]] = $match[1];
87         }
88
89         $typePad = max(array_map('strlen', array_merge(array_values($properties), array($model))));
90         $namePad = max(array_map('strlen', array_keys(array_map(array('sfInflector', 'camelize'), $properties))));
91         $setters = array();
92         $getters = array();
93
94         foreach ($properties as $name => $type)
95         {
96           $camelized = sfInflector::camelize($name);
97           $collection = 'Doctrine_Collection' == $type;
98
99           $getters[] = sprintf('@method %-'.$typePad.'s %s%-'.($namePad + 2).'s Returns the current record\'s "%s" %s', $type, 'get', $camelized.'()', $name, $collection ? 'collection' : 'value');
100           $setters[] = sprintf('@method %-'.$typePad.'s %s%-'.($namePad + 2).'s Sets the current record\'s "%s" %s', $model, 'set', $camelized.'()', $name, $collection ? 'collection' : 'value');
101         }
102
103         // use the last match as a search string
104         $code = str_replace($match[0], $match[0].PHP_EOL.' * '.PHP_EOL.' * '.implode(PHP_EOL.' * ', array_merge($getters, $setters)), $code);
105         file_put_contents($file, $code);
106       }
107     }
108
109     $properties = parse_ini_file(sfConfig::get('sf_config_dir').'/properties.ini', true);
110     $tokens = array(
111       '##PACKAGE##'    => isset($properties['symfony']['name']) ? $properties['symfony']['name'] : 'symfony',
112       '##SUBPACKAGE##' => 'model',
113       '##NAME##'       => isset($properties['symfony']['author']) ? $properties['symfony']['author'] : 'Your name here',
114       ' <##EMAIL##>'   => '',
115       "{\n\n}"         => "{\n}\n",
116     );
117
118     // cleanup new stub classes
119     $after = $stubFinder->in($config['models_path']);
120     $this->getFilesystem()->replaceTokens(array_diff($after, $before), '', '', $tokens);
121
122     // cleanup base classes
123     $baseFinder = sfFinder::type('file')->name('Base*'.$builderOptions['suffix']);
124     $baseDirFinder = sfFinder::type('dir')->name('base');
125     $this->getFilesystem()->replaceTokens($baseFinder->in($baseDirFinder->in($config['models_path'])), '', '', $tokens);
126
127     $this->reloadAutoload();
128   }
129 }
130
Note: See TracBrowser for help on using the browser.