Development

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

You must first sign up to be able to contribute.

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

Revision 28809, 6.9 kB (checked in by Jonathan.Wage, 4 years ago)

[1.3, 1.4] Throw exception when routing file cannot be written in doctrine:generate-admin task (fixes #8356)

  • 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__).'/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$
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       new sfCommandOption('actions-base-class', null, sfCommandOption::PARAMETER_REQUIRED, 'The base class for the actions', 'sfActions'),
40     ));
41
42     $this->namespace = 'doctrine';
43     $this->name = 'generate-admin';
44     $this->briefDescription = 'Generates a Doctrine admin module';
45
46     $this->detailedDescription = <<<EOF
47 The [doctrine:generate-admin|INFO] task generates a Doctrine admin module:
48
49   [./symfony doctrine:generate-admin frontend Article|INFO]
50
51 The task creates a module in the [%frontend%|COMMENT] application for the
52 [%Article%|COMMENT] model.
53
54 The task creates a route for you in the application [routing.yml|COMMENT].
55
56 You can also generate a Doctrine admin module by passing a route name:
57
58   [./symfony doctrine:generate-admin frontend article|INFO]
59
60 The task creates a module in the [%frontend%|COMMENT] application for the
61 [%article%|COMMENT] route definition found in [routing.yml|COMMENT].
62
63 For the filters and batch actions to work properly, you need to add
64 the [with_wildcard_routes|COMMENT] option to the route:
65
66   article:
67     class: sfDoctrineRouteCollection
68     options:
69       model:                Article
70       with_wildcard_routes: true
71 EOF;
72   }
73
74   /**
75    * @see sfTask
76    */
77   protected function execute($arguments = array(), $options = array())
78   {
79     // get configuration for the given route
80     if (false !== ($route = $this->getRouteFromName($arguments['route_or_model'])))
81     {
82       $arguments['route'] = $route;
83       $arguments['route_name'] = $arguments['route_or_model'];
84
85       return $this->generateForRoute($arguments, $options);
86     }
87
88     // is it a model class name
89     if (!class_exists($arguments['route_or_model']))
90     {
91       throw new sfCommandException(sprintf('The route "%s" does not exist and there is no "%s" class.', $arguments['route_or_model'], $arguments['route_or_model']));
92     }
93
94     $r = new ReflectionClass($arguments['route_or_model']);
95     if (!$r->isSubclassOf('Doctrine_Record'))
96     {
97       throw new sfCommandException(sprintf('"%s" is not a Doctrine class.', $arguments['route_or_model']));
98     }
99
100     // create a route
101     $model = $arguments['route_or_model'];
102     $name = strtolower(preg_replace(array('/([A-Z]+)([A-Z][a-z])/', '/([a-z\d])([A-Z])/'), '\\1_\\2', $model));
103
104     if (isset($options['module']))
105     {
106       $route = $this->getRouteFromName($name);
107       if ($route && !$this->checkRoute($route, $model, $options['module']))
108       {
109         $name .= '_'.$options['module'];
110       }
111     }
112
113     $routing = sfConfig::get('sf_app_config_dir').'/routing.yml';
114     $content = file_get_contents($routing);
115     $routesArray = sfYaml::load($content);
116
117     if (!isset($routesArray[$name]))
118     {
119       $databaseManager = new sfDatabaseManager($this->configuration);
120       $primaryKey = Doctrine_Core::getTable($model)->getIdentifier();
121       $module = $options['module'] ? $options['module'] : $name;
122       $content = sprintf(<<<EOF
123 %s:
124   class: sfDoctrineRouteCollection
125   options:
126     model:                %s
127     module:               %s
128     prefix_path:          /%s
129     column:               %s
130     with_wildcard_routes: true
131
132
133 EOF
134       , $name, $model, $module, isset($options['plural']) ? $options['plural'] : $module, $primaryKey).$content;
135
136       $this->logSection('file+', $routing);
137
138       if (false === file_put_contents($routing, $content))
139       {
140         throw new sfCommandException(sprintf('Unable to write to file, %s.', $routing));
141       }
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 sfDoctrineRouteCollection)
155     {
156       throw new sfCommandException(sprintf('The route "%s" is not a Doctrine collection route.', $arguments['route_name']));
157     }
158
159     $module = $routeOptions['module'];
160     $model = $routeOptions['model'];
161
162     // execute the doctrine:generate-module task
163     $task = new sfDoctrineGenerateModuleTask($this->dispatcher, $this->formatter);
164     $task->setCommandApplication($this->commandApplication);
165     $task->setConfiguration($this->configuration);
166
167     $this->logSection('app', sprintf('Generating admin module "%s" for model "%s"', $module, $model));
168
169     return $task->run(array($arguments['application'], $module, $model), array(
170       'theme'                 => $options['theme'],
171       'route-prefix'          => $routeOptions['name'],
172       'with-doctrine-route'   => true,
173       'generate-in-cache'     => true,
174       'non-verbose-templates' => true,
175       'singular'              => $options['singular'],
176       'plural'                => $options['plural'],
177       'actions-base-class'    => $options['actions-base-class'],
178     ));
179   }
180
181   protected function getRouteFromName($name)
182   {
183     $config = new sfRoutingConfigHandler();
184     $routes = $config->evaluate($this->configuration->getConfigPaths('config/routing.yml'));
185
186     if (isset($routes[$name]))
187     {
188       return $routes[$name];
189     }
190
191     return false;
192   }
193
194   /**
195    * Checks whether a route references a model and module.
196    *
197    * @param mixed  $route  A route collection
198    * @param string $model  A model name
199    * @param string $module A module name
200    *
201    * @return boolean
202    */
203   protected function checkRoute($route, $model, $module)
204   {
205     if ($route instanceof sfDoctrineRouteCollection)
206     {
207       $options = $route->getOptions();
208       return $model == $options['model'] && $module == $options['module'];
209     }
210
211     return false;
212   }
213 }
214
Note: See TracBrowser for help on using the browser.