Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 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: sfDoctrineBuildModelTask.class.php 30901 2010-09-13 17:41:16Z Kris.Wallsmith $
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->namespace = 'doctrine';
36
    $this->name = 'build-model';
37
    $this->briefDescription = 'Creates classes for the current model';
38
 
39
    $this->detailedDescription = <<<EOF
40
The [doctrine:build-model|INFO] task creates model classes from the schema:
41
 
42
  [./symfony doctrine:build-model|INFO]
43
 
44
The task read the schema information in [config/doctrine/*.yml|COMMENT]
45
from the project and all enabled plugins.
46
 
47
The model classes files are created in [lib/model/doctrine|COMMENT].
48
 
49
This task never overrides custom classes in [lib/model/doctrine|COMMENT].
50
It only replaces files in [lib/model/doctrine/base|COMMENT].
51
EOF;
52
  }
53
 
54
  /**
55
   * @see sfTask
56
   */
57
  protected function execute($arguments = array(), $options = array())
58
  {
59
    $this->logSection('doctrine', 'generating model classes');
60
 
61
    $config = $this->getCliConfig();
62
    $builderOptions = $this->configuration->getPluginConfiguration('sfDoctrinePlugin')->getModelBuilderOptions();
63
 
64
    $stubFinder = sfFinder::type('file')->prune('base')->name('*'.$builderOptions['suffix']);
65
    $before = $stubFinder->in($config['models_path']);
66
 
67
    $schema = $this->prepareSchemaFile($config['yaml_schema_path']);
68
 
69
    $import = new Doctrine_Import_Schema();
70
    $import->setOptions($builderOptions);
71
    $import->importSchema($schema, 'yml', $config['models_path']);
72
 
73
    // markup base classes with magic methods
74
    foreach (sfYaml::load($schema) as $model => $definition)
75
    {
76
      $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']);
77
      $code = file_get_contents($file);
78
 
79
      // introspect the model without loading the class
80
      if (preg_match_all('/@property (\w+) \$(\w+)/', $code, $matches, PREG_SET_ORDER))
81
      {
82
        $properties = array();
83
        foreach ($matches as $match)
84
        {
85
          $properties[$match[2]] = $match[1];
86
        }
87
 
88
        $typePad = max(array_map('strlen', array_merge(array_values($properties), array($model))));
89
        $namePad = max(array_map('strlen', array_keys(array_map(array('sfInflector', 'camelize'), $properties))));
90
        $setters = array();
91
        $getters = array();
92
 
93
        foreach ($properties as $name => $type)
94
        {
95
          $camelized = sfInflector::camelize($name);
96
          $collection = 'Doctrine_Collection' == $type;
97
 
98
          $getters[] = sprintf('@method %-'.$typePad.'s %s%-'.($namePad + 2).'s Returns the current record\'s "%s" %s', $type, 'get', $camelized.'()', $name, $collection ? 'collection' : 'value');
99
          $setters[] = sprintf('@method %-'.$typePad.'s %s%-'.($namePad + 2).'s Sets the current record\'s "%s" %s', $model, 'set', $camelized.'()', $name, $collection ? 'collection' : 'value');
100
        }
101
 
102
        // use the last match as a search string
103
        $code = str_replace($match[0], $match[0].PHP_EOL.' * '.PHP_EOL.' * '.implode(PHP_EOL.' * ', array_merge($getters, $setters)), $code);
104
        file_put_contents($file, $code);
105
      }
106
    }
107
 
108
    $properties = parse_ini_file(sfConfig::get('sf_config_dir').'/properties.ini', true);
109
    $tokens = array(
110
      '##PACKAGE##'    => isset($properties['symfony']['name']) ? $properties['symfony']['name'] : 'symfony',
111
      '##SUBPACKAGE##' => 'model',
112
      '##NAME##'       => isset($properties['symfony']['author']) ? $properties['symfony']['author'] : 'Your name here',
113
      ' <##EMAIL##>'   => '',
114
      "{\n\n}"         => "{\n}\n",
115
    );
116
 
117
    // cleanup new stub classes
118
    $after = $stubFinder->in($config['models_path']);
119
    $this->getFilesystem()->replaceTokens(array_diff($after, $before), '', '', $tokens);
120
 
121
    // cleanup base classes
122
    $baseFinder = sfFinder::type('file')->name('Base*'.$builderOptions['suffix']);
123
    $baseDirFinder = sfFinder::type('dir')->name('base');
124
    $this->getFilesystem()->replaceTokens($baseFinder->in($baseDirFinder->in($config['models_path'])), '', '', $tokens);
125
 
126
    $this->reloadAutoload();
127
  }
128
}