Development

/branches/1.2/lib/plugins/sfDoctrinePlugin/lib/task/sfDoctrineGenerateAdminTask.class.php

You must first sign up to be able to contribute.

root/branches/1.2/lib/plugins/sfDoctrinePlugin/lib/task/sfDoctrineGenerateAdminTask.class.php

Revision 15470, 6.0 kB (checked in by Kris.Wallsmith, 5 years ago)

[1.2] cleaned formatting of generated route collections

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__).'/sfDoctrineBaseTask.class.php');
12
13 /**
14  * Generates a Doctrine admin module.
15  *
16  * @package    symfony
17  * @subpackage doctrine
18  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
19  * @version    SVN: $Id: sfDoctrineGenerateAdminTask.class.php 12474 2008-10-31 10:41:27Z fabien $
20  */
21 class sfDoctrineGenerateAdminTask extends sfDoctrineBaseTask
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 = 'doctrine';
42     $this->name = 'generate-admin';
43     $this->briefDescription = 'Generates a Doctrine admin module';
44
45     $this->detailedDescription = <<<EOF
46 The [doctrine:generate-admin|INFO] task generates a Doctrine admin module:
47
48   [./symfony doctrine: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 Doctrine admin module by passing a route name:
56
57   [./symfony doctrine: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: sfDoctrineRouteCollection
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('Doctrine_Record'))
95     {
96       throw new sfCommandException(sprintf('"%s" is not a Doctrine 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     $name = $options['module'] ? $name . '_' . $options['module'] : $name;
103
104     $routing = sfConfig::get('sf_app_config_dir').'/routing.yml';
105     $content = file_get_contents($routing);
106     $routesArray = sfYaml::load($content);
107
108     if (!isset($routesArray[$name]))
109     {
110       $databaseManager = new sfDatabaseManager($this->configuration);
111       $primaryKey = Doctrine::getTable($model)->getIdentifier();
112       $module = $options['module'] ? $options['module'] : $name;
113       $content = sprintf(<<<EOF
114 %s:
115   class: sfDoctrineRouteCollection
116   options:
117     model:                %s
118     module:               %s
119     prefix_path:          %s
120     column:               %s
121     with_wildcard_routes: true
122
123
124 EOF
125       , $name, $model, $module, $module, $primaryKey).$content;
126
127       file_put_contents($routing, $content);
128     }
129
130     $arguments['route'] = $this->getRouteFromName($name);
131     $arguments['route_name'] = $name;
132
133     return $this->generateForRoute($arguments, $options);
134   }
135
136   protected function generateForRoute($arguments, $options)
137   {
138     $routeOptions = $arguments['route']->getOptions();
139
140     if (!$arguments['route'] instanceof sfDoctrineRouteCollection)
141     {
142       throw new sfCommandException(sprintf('The route "%s" is not a Doctrine collection route.', $arguments['route_name']));
143     }
144
145     $module = $routeOptions['module'];
146     $model = $routeOptions['model'];
147
148     // execute the doctrine:generate-module task
149     $task = new sfDoctrineGenerateModuleTask($this->dispatcher, $this->formatter);
150     $task->setCommandApplication($this->commandApplication);
151
152     $taskOptions = array(
153       '--theme='.$options['theme'],
154       '--env='.$options['env'],
155       '--route-prefix='.$routeOptions['name'],
156       '--with-doctrine-route',
157       '--generate-in-cache',
158       '--non-verbose-templates',
159     );
160
161     if (!is_null($options['singular']))
162     {
163       $taskOptions[] = '--singular='.$options['singular'];
164     }
165
166     if (!is_null($options['plural']))
167     {
168       $taskOptions[] = '--plural='.$options['plural'];
169     }
170
171     $this->logSection('app', sprintf('Generating admin module "%s" for model "%s"', $module, $model));
172
173     return $task->run(array($arguments['application'], $module, $model), $taskOptions);
174   }
175
176   protected function getRouteFromName($name)
177   {
178     $config = new sfRoutingConfigHandler();
179     $routes = $config->evaluate($this->configuration->getConfigPaths('config/routing.yml'));
180
181     if (isset($routes[$name]))
182     {
183       return $routes[$name];
184     }
185
186     return false;
187   }
188 }
189
Note: See TracBrowser for help on using the browser.