root/lang/php/symfony_tasks/myPakePropelAdminGeneratorPlus.php

Revision 26679, 4.0 kB (checked in by n0ts, 7 months ago)

Added

Line 
1<?php
2
3/*
4 * This task based by {$sf_symfony_data}/tasks/sfPakePropelAdminGenerator.php
5 *
6 * @author     n0ts
7 * @version    SVN: $Id: $
8 */
9
10
11/*
12 * This file is part of the symfony package.
13 * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
14 *
15 * For the full copyright and license information, please view the LICENSE
16 * file that was distributed with this source code.
17 */
18
19pake_desc('initialize a new propel admin module plus');
20pake_task('propel-init-admin-plus', 'app_exists');
21
22function run_propel_init_admin_plus($task, $args)
23{
24  if (count($args) < 2)
25  {
26    throw new Exception('You must provide your module name.');
27  }
28
29  if (count($args) < 3)
30  {
31    throw new Exception('You must provide your model class name.');
32  }
33
34  $app         = $args[0];
35  $module      = $args[1];
36  $model_class = $args[2];
37  $theme       = isset($args[3]) ? $args[3] : 'default';
38
39  try
40  {
41    $author_name = $task->get_property('author', 'symfony');
42  }
43  catch (pakeException $e)
44  {
45    $author_name = 'Your name here';
46  }
47
48  $gen_fields = '';
49  $gen_display = array();
50  $validator = '';
51
52  $schema = new sfPropelDatabaseSchema;
53  $schema->loadYAML(sfConfig::get('sf_root_dir').'/config/schema.yml');
54  $tables = $schema->getTables();
55  $table = $tables[sfInflector::underscore($model_class)];
56  if ($table) {
57      foreach ($table as $column => $attributes) {
58          if (substr($column, 0, 1) === '_') {
59              continue;
60          }
61
62          $gen_fields .= sprintf("      %s:\n", $column);
63          $gen_fields .= sprintf("        name: \"%s\"\n", $column);
64          $gen_display []= $column;
65
66          if ($column === 'id') {
67              continue;
68          }
69
70          $validator_header = sprintf("  %s{%s}:\n",
71                                      sfInflector::underscore($model_class),
72                                      $column);
73          $validator_detail = '';
74
75          $required = sfInflector::underscore($attributes['required']);
76          if ((bool) $required === true) {
77              $validator_detail .= "    required:\n";
78              $validator_detail .= sprintf("      msg: \"%s is required.\"\n",
79                                           $column);
80          }
81
82          $size = sfInflector::underscore($attributes['size']);
83          if ($size) {
84              $validator_detail .= "    sfStringValidator:\n";
85              $validator_detail .= sprintf("      max: %d\n", $size);
86              $validator_detail .= "      max_error: \"This field is too long.\"\n";
87          }
88
89          $type = sfInflector::underscore($attributes['type']);
90          switch ($type) {
91          case 'timestamp':
92              $validator_detail .= "    myDateValidator:\n";
93              break;
94          }
95
96          if ($validator_detail) {
97              $validator .= $validator_header . $validator_detail;
98          }
99          else {
100              $validator .= '#' . $validator_header;
101          }
102      }
103  }
104
105
106  $constants = array(
107    'PROJECT_NAME' => $task->get_property('name', 'symfony'),
108    'APP_NAME'     => $app,
109    'MODULE_NAME'  => $module,
110    'MODEL_NAME'   => sfInflector::underscore($model_class),
111    'MODEL_CLASS'  => $model_class,
112    'GEN_FIELDS'   => $gen_fields,
113    'GEN_DISPLAY'  => sprintf('[%s]', implode(', ', $gen_display)),
114    'VALIDATOR'    => $validator,
115    'AUTHOR_NAME'  => $author_name,
116    'THEME'        => $theme,
117  );
118
119
120  $moduleDir = sfConfig::get('sf_root_dir').'/'.sfConfig::get('sf_apps_dir_name').'/'.$app.'/'.sfConfig::get('sf_app_module_dir_name').'/'.$module;
121
122  // create module structure
123  $finder = pakeFinder::type('any')->ignore_version_control()->discard('.sf');
124  $dirs = sfLoader::getGeneratorSkeletonDirs('sfPropelAdmin', $theme);
125  foreach ($dirs as $dir)
126  {
127    if (is_dir($dir))
128    {
129      pake_mirror($finder, $dir, $moduleDir);
130      break;
131    }
132  }
133
134  // customize php and yml files
135  $finder = pakeFinder::type('file')->ignore_version_control()->name('*.php', '*.yml');
136  pake_replace_tokens($finder, $moduleDir, '##', '##', $constants);
137}
Note: See TracBrowser for help on using the browser.