Development

/branches/1.2/lib/plugins/sfPropelPlugin/lib/task/sfPropelGenerateAdminTask.class.php

You must first sign up to be able to contribute.

root/branches/1.2/lib/plugins/sfPropelPlugin/lib/task/sfPropelGenerateAdminTask.class.php

Revision 15470, 6.2 kB (checked in by Kris.Wallsmith, 6 years ago)

[1.2] cleaned formatting of generated route collections

  • Property svn:mime-type set to text/x-php
  • 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  *
7  * For the full copyright and license information, please view the LICENSE
8  * file that was distributed with this source code.
9  */
10
11 require_once(dirname(__FILE__).'/sfPropelBaseTask.class.php');
12
13 /**
14  * Generates a Propel admin module.
15  *
16  * @package    symfony
17  * @subpackage propel
18  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
19  * @version    SVN: $Id$
20  */
21 class sfPropelGenerateAdminTask extends sfPropelBaseTask
22 {
23   /**
24    * @see sfTask
25    */
26   protected function configure()
27   {
28     $this->addArguments(array(
29       new sfCommandArgument('application', sfCommandArgument::REQUIRED, 'The application name'),
30       new sfCommandArgument('route_or_model', sfCommandArgument::REQUIRED, 'The route name or the model class'),
31     ));
32
33     $this->addOptions(array(
34       new sfCommandOption('module', null, sfCommandOption::PARAMETER_REQUIRED, 'The module name', null),
35       new sfCommandOption('theme', null, sfCommandOption::PARAMETER_REQUIRED, 'The theme name', 'admin'),
36       new sfCommandOption('singular', null, sfCommandOption::PARAMETER_REQUIRED, 'The singular name', null),
37       new sfCommandOption('plural', null, sfCommandOption::PARAMETER_REQUIRED, 'The plural name', null),
38       new sfCommandOption('env', null, sfCommandOption::PARAMETER_REQUIRED, 'The environment', 'dev'),
39     ));
40
41     $this->namespace = 'propel';
42     $this->name = 'generate-admin';
43     $this->briefDescription = 'Generates a Propel admin module';
44
45     $this->detailedDescription = <<<EOF
46 The [propel:generate-admin|INFO] task generates a Propel admin module:
47
48   [./symfony propel:generate-admin frontend Article|INFO]
49
50 The task creates a module in the [%frontend%|COMMENT] application for the
51 [%Article%|COMMENT] model.
52
53 The task creates a route for you in the application [routing.yml|COMMENT].
54
55 You can also generate a Propel admin module by passing a route name:
56
57   [./symfony propel:generate-admin frontend article|INFO]
58
59 The task creates a module in the [%frontend%|COMMENT] application for the
60 [%article%|COMMENT] route definition found in [routing.yml|COMMENT].
61
62 For the filters and batch actions to work properly, you need to add
63 the [wildcard|COMMENT] option to the route:
64
65   article:
66     class: sfPropelRouteCollection
67     options:
68       model:                Article
69       with_wildcard_routes: true
70 EOF;
71   }
72
73   /**
74    * @see sfTask
75    */
76   protected function execute($arguments = array(), $options = array())
77   {
78     // get configuration for the given route
79     if (false !== ($route = $this->getRouteFromName($arguments['route_or_model'])))
80     {
81       $arguments['route'] = $route;
82       $arguments['route_name'] = $arguments['route_or_model'];
83
84       return $this->generateForRoute($arguments, $options);
85     }
86
87     // is it a model class name
88     if (!class_exists($arguments['route_or_model']))
89     {
90       throw new sfCommandException(sprintf('The route "%s" does not exist and there is no "%s" class.', $arguments['route_or_model'], $arguments['route_or_model']));
91     }
92
93     $r = new ReflectionClass($arguments['route_or_model']);
94     if (!$r->isSubclassOf('BaseObject'))
95     {
96       throw new sfCommandException(sprintf('"%s" is not a Propel class.', $arguments['route_or_model']));
97     }
98
99     // create a route
100     $model = $arguments['route_or_model'];
101     $name = strtolower(preg_replace(array('/([A-Z]+)([A-Z][a-z])/', '/([a-z\d])([A-Z])/'), '\\1_\\2', $model));
102
103     $routing = sfConfig::get('sf_app_config_dir').'/routing.yml';
104     $content = file_get_contents($routing);
105     $routesArray = sfYaml::load($content);
106
107     if (!isset($routesArray[$name]))
108     {
109       $class = $model.'MapBuilder';
110       $map = new $class();
111       if (!$map->isBuilt())
112       {
113         $map->doBuild();
114       }
115
116       $primaryKey = 'id';
117       foreach ($map->getDatabaseMap()->getTable(constant(constant($model.'::PEER').'::TABLE_NAME'))->getColumns() as $column)
118       {
119         if ($column->isPrimaryKey())
120         {
121           $primaryKey = call_user_func(array(constant($model.'::PEER'), 'translateFieldName'), $column->getPhpName(), BasePeer::TYPE_PHPNAME, BasePeer::TYPE_FIELDNAME);
122           break;
123         }
124       }
125
126       $module = $options['module'] ? $options['module'] : $name;
127       $content = sprintf(<<<EOF
128 %s:
129   class: sfPropelRouteCollection
130   options:
131     model:                %s
132     module:               %s
133     prefix_path:          %s
134     column:               %s
135     with_wildcard_routes: true
136
137
138 EOF
139       , $name, $model, $module, $module, $primaryKey).$content;
140
141       file_put_contents($routing, $content);
142     }
143
144     $arguments['route'] = $this->getRouteFromName($name);
145     $arguments['route_name'] = $name;
146
147     return $this->generateForRoute($arguments, $options);
148   }
149
150   protected function generateForRoute($arguments, $options)
151   {
152     $routeOptions = $arguments['route']->getOptions();
153
154     if (!$arguments['route'] instanceof sfPropelRouteCollection)
155     {
156       throw new sfCommandException(sprintf('The route "%s" is not a Propel collection route.', $arguments['route_name']));
157     }
158
159     $module = $routeOptions['module'];
160     $model = $routeOptions['model'];
161
162     // execute the propel:generate-module task
163     $task = new sfPropelGenerateModuleTask($this->dispatcher, $this->formatter);
164     $task->setCommandApplication($this->commandApplication);
165
166     $taskOptions = array(
167       '--theme='.$options['theme'],
168       '--env='.$options['env'],
169       '--route-prefix='.$routeOptions['name'],
170       '--with-propel-route',
171       '--generate-in-cache',
172       '--non-verbose-templates',
173     );
174
175     if (!is_null($options['singular']))
176     {
177       $taskOptions[] = '--singular='.$options['singular'];
178     }
179
180     if (!is_null($options['plural']))
181     {
182       $taskOptions[] = '--plural='.$options['plural'];
183     }
184
185     $this->logSection('app', sprintf('Generating admin module "%s" for model "%s"', $module, $model));
186
187     return $task->run(array($arguments['application'], $module, $model), $taskOptions);
188   }
189
190   protected function getRouteFromName($name)
191   {
192     $config = new sfRoutingConfigHandler();
193     $routes = $config->evaluate($this->configuration->getConfigPaths('config/routing.yml'));
194
195     if (isset($routes[$name]))
196     {
197       return $routes[$name];
198     }
199
200     return false;
201   }
202 }
203
Note: See TracBrowser for help on using the browser.