Development

/branches/1.4/lib/task/app/sfAppRoutesTask.class.php

You must first sign up to be able to contribute.

root/branches/1.4/lib/task/app/sfAppRoutesTask.class.php

Revision 23549, 5.2 kB (checked in by fabien, 4 years ago)

[1.3] refactored the app:routes task to give a getRouting() method for all tasks

  • 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 /**
12  * Displays the current routes for an application.
13  *
14  * @package    symfony
15  * @subpackage task
16  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
17  * @version    SVN: $Id$
18  */
19 class sfAppRoutesTask extends sfBaseTask
20 {
21   protected
22     $routes = array();
23
24   /**
25    * @see sfTask
26    */
27   protected function configure()
28   {
29     $this->addArguments(array(
30       new sfCommandArgument('application', sfCommandArgument::REQUIRED, 'The application name'),
31       new sfCommandArgument('name', sfCommandArgument::OPTIONAL, 'A route name'),
32     ));
33
34     $this->namespace = 'app';
35     $this->name = 'routes';
36     $this->briefDescription = 'Displays current routes for an application';
37
38     $this->detailedDescription = <<<EOF
39 The [app:routes|INFO] displays the current routes for a given application:
40
41   [./symfony app:routes frontend|INFO]
42 EOF;
43   }
44
45   /**
46    * @see sfTask
47    */
48   protected function execute($arguments = array(), $options = array())
49   {
50     $this->routes = $this->getRouting()->getRoutes();
51
52     // display
53     $arguments['name'] ? $this->outputRoute($arguments['application'], $arguments['name']) : $this->outputRoutes($arguments['application']);
54   }
55
56   protected function outputRoutes($application)
57   {
58     $this->logSection('app', sprintf('Current routes for application "%s"', $application));
59
60     $maxName = 4;
61     $maxMethod = 6;
62     foreach ($this->routes as $name => $route)
63     {
64       $requirements = $route->getRequirements();
65       $method = isset($requirements['sf_method']) ? strtoupper(is_array($requirements['sf_method']) ? implode(', ', $requirements['sf_method']) : $requirements['sf_method']) : 'ANY';
66
67       if (strlen($name) > $maxName)
68       {
69         $maxName = strlen($name);
70       }
71
72       if (strlen($method) > $maxMethod)
73       {
74         $maxMethod = strlen($method);
75       }
76     }
77     $format  = '%-'.$maxName.'s %-'.$maxMethod.'s %s';
78
79     // displays the generated routes
80     $format1  = '%-'.($maxName + 9).'s %-'.($maxMethod + 9).'s %s';
81     $this->log(sprintf($format1, $this->formatter->format('Name', 'COMMENT'), $this->formatter->format('Method', 'COMMENT'), $this->formatter->format('Pattern', 'COMMENT')));
82     foreach ($this->routes as $name => $route)
83     {
84       $requirements = $route->getRequirements();
85       $method = isset($requirements['sf_method']) ? strtoupper(is_array($requirements['sf_method']) ? implode(', ', $requirements['sf_method']) : $requirements['sf_method']) : 'ANY';
86       $this->log(sprintf($format, $name, $method, $route->getPattern()));
87     }
88   }
89
90   protected function outputRoute($application, $name)
91   {
92     $this->logSection('app', sprintf('Route "%s" for application "%s"', $name, $application));
93
94     if (!isset($this->routes[$name]))
95     {
96       throw new sfCommandException(sprintf('The route "%s" does not exist.', $name));
97     }
98
99     $route = $this->routes[$name];
100     $this->log(sprintf('%s         %s', $this->formatter->format('Name', 'COMMENT'), $name));
101     $this->log(sprintf('%s      %s', $this->formatter->format('Pattern', 'COMMENT'), $route->getPattern()));
102     $this->log(sprintf('%s        %s', $this->formatter->format('Class', 'COMMENT'), get_class($route)));
103
104     $defaults = '';
105     $d = $route->getDefaults();
106     ksort($d);
107     foreach ($d as $name => $value)
108     {
109       $defaults .= ($defaults ? "\n".str_repeat(' ', 13) : '').$name.': '.$this->formatValue($value);
110     }
111     $this->log(sprintf('%s     %s', $this->formatter->format('Defaults', 'COMMENT'), $defaults));
112
113     $requirements = '';
114     $r = $route->getRequirements();
115     ksort($r);
116     foreach ($r as $name => $value)
117     {
118       $requirements .= ($requirements ? "\n".str_repeat(' ', 13) : '').$name.': '.$this->formatValue($value);
119     }
120     $this->log(sprintf('%s %s', $this->formatter->format('Requirements', 'COMMENT'), $requirements));
121
122     $options = '';
123     $o = $route->getOptions();
124     ksort($o);
125     foreach ($o as $name => $value)
126     {
127       $options .= ($options ? "\n".str_repeat(' ', 13) : '').$name.': '.$this->formatValue($value);
128     }
129     $this->log(sprintf('%s      %s', $this->formatter->format('Options', 'COMMENT'), $options));
130     $this->log(sprintf('%s        %s', $this->formatter->format('Regex', 'COMMENT'), preg_replace('/^             /', '', preg_replace('/^/m', '             ', $route->getRegex()))));
131
132     $tokens = '';
133     foreach ($route->getTokens() as $token)
134     {
135       if (!$tokens)
136       {
137         $tokens = $this->displayToken($token);
138       }
139       else
140       {
141         $tokens .= "\n".str_repeat(' ', 13).$this->displayToken($token);
142       }
143     }
144     $this->log(sprintf('%s       %s', $this->formatter->format('Tokens', 'COMMENT'), $tokens));
145   }
146
147   protected function displayToken($token)
148   {
149     $type = array_shift($token);
150     array_shift($token);
151
152     return sprintf('%-10s %s', $type, $this->formatValue($token));
153   }
154
155   protected function formatValue($value)
156   {
157     if (is_object($value))
158     {
159       return sprintf('object(%s)', get_class($value));
160     }
161     else
162     {
163       return preg_replace("/\n\s*/s", '', var_export($value, true));
164     }
165   }
166 }
167
Note: See TracBrowser for help on using the browser.