Source of file ControllerGenerator.php
Size: 7,003 Bytes - Last Modified: 2013-07-04T14:20:59+02:00
/home/theseer/Downloads/Symfony/vendor/sensio/generator-bundle/Sensio/Bundle/GeneratorBundle/Generator/ControllerGenerator.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195 | <?php /* * This file is part of the Symfony package. * * (c) Fabien Potencier <fabien@symfony.com> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Sensio\Bundle\GeneratorBundle\Generator; use Symfony\Component\Filesystem\Filesystem; use Symfony\Component\HttpKernel\Bundle\BundleInterface; /** * Generates a Controller inside a bundle. * * @author Wouter J <wouter@wouterj.nl> */ class ControllerGenerator extends Generator { private $filesystem; /** * Constructor. * * @param Filesystem $filesystem A Filesystem instance */ public function __construct(Filesystem $filesystem) { $this->filesystem = $filesystem; } public function generate(BundleInterface $bundle, $controller, $routeFormat, $templateFormat, array $actions = array()) { $dir = $bundle->getPath(); $controllerFile = $dir.'/Controller/'.$controller.'Controller.php'; if (file_exists($controllerFile)) { throw new \RuntimeException(sprintf('Controller "%s" already exists', $controller)); } $parameters = array( 'namespace' => $bundle->getNamespace(), 'bundle' => $bundle->getName(), 'format' => array( 'routing' => $routeFormat, 'templating' => $templateFormat, ), 'controller' => $controller, ); foreach ($actions as $i => $action) { // get the actioname without the sufix Action (for the template logical name) $actions[$i]['basename'] = $basename = substr($action['name'], 0, -6); $params = $parameters; $params['action'] = $actions[$i]; // create a template $template = $actions[$i]['template']; if ('default' == $template) { $template = $bundle->getName().':'.$controller.':'.substr($action['name'], 0, -6).'.html.'.$templateFormat; } if ('twig' == $templateFormat) { $this->renderFile('controller/Template.html.twig', $dir.'/Resources/views/'.$this->parseTemplatePath($template), $params); } else { $this->renderFile('controller/Template.html.php', $dir.'/Resources/views/'.$this->parseTemplatePath($template), $params); } $this->generateRouting($bundle, $controller, $actions[$i], $routeFormat); } $parameters['actions'] = $actions; $this->renderFile('controller/Controller.php', $controllerFile, $parameters); $this->renderFile('controller/ControllerTest.php', $dir.'/Tests/Controller/'.$controller.'ControllerTest.php', $parameters); } public function generateRouting(BundleInterface $bundle, $controller, array $action, $format) { // annotation is generated in the templates if ('annotation' == $format) { return true; } $file = $bundle->getPath().'/Resources/config/routing.'.$format; if (file_exists($file)) { $content = file_get_contents($file); } elseif (!is_dir($dir = $bundle->getPath().'/Resources/config')) { mkdir($dir); } $controller = $bundle->getName().':'.$controller.':'.$action['basename']; $name = strtolower(preg_replace('/([A-Z])/', '_\\1', $action['basename'])); if ('yml' == $format) { // yaml if (!isset($content)) { $content = ''; } $content .= sprintf( "\n%s:\n pattern: %s\n defaults: { _controller: %s }\n", $name, $action['route'], $controller ); } elseif ('xml' == $format) { // xml if (!isset($content)) { // new file $content = <<<EOT <?xml version="1.0" encoding="UTF-8" ?> <routes xmlns="http://symfony.com/schema/routing" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd"> </routes> EOT; } $sxe = simplexml_load_string($content); $route = $sxe->addChild('route'); $route->addAttribute('id', $name); $route->addAttribute('pattern', $action['route']); $default = $route->addChild('default', $controller); $default->addAttribute('key', '_controller'); $dom = new \DOMDocument('1.0'); $dom->preserveWhiteSpace = false; $dom->formatOutput = true; $dom->loadXML($sxe->asXML()); $content = $dom->saveXML(); } elseif ('php' == $format) { // php if (isset($content)) { // edit current file $pointer = strpos($content, 'return'); if (!preg_match('/(\$[^ ]*).*?new RouteCollection\(\)/', $content, $collection) || false === $pointer) { throw new \RunTimeException('Routing.php file is not correct, please initialize RouteCollection.'); } $content = substr($content, 0, $pointer); $content .= sprintf("%s->add('%s', new Route('%s', array(", $collection[1], $name, $action['route']); $content .= sprintf("\n '_controller' => '%s',", $controller); $content .= "\n)));\n\nreturn ".$collection[1]; } else { // new file $content = <<<EOT <?php use Symfony\Component\Routing\RouteCollection; use Symfony\Component\Routing\Route; \$collection = new RouteCollection(); EOT; $content .= sprintf("\n\$collection->add('%s', new Route('%s', array(", $name, $action['route']); $content .= sprintf("\n '_controller' => '%s',", $controller); $content .= "\n)));\n\nreturn \$collection"; } } $flink = fopen($file, 'w'); if ($flink) { $write = fwrite($flink, $content); if ($write) { fclose($flink); } else { throw new \RunTimeException(sprintf('We cannot write into file "%s", has that file the correct access level?', $file)); } } else { throw new \RunTimeException(sprintf('Problems with generating file "%s", did you gave write access to that directory?', $file)); } } protected function parseTemplatePath($template) { $data = $this->parseLogicalTemplateName($template); return $data['controller'].'/'.$data['template']; } protected function parseLogicalTemplateName($logicalname, $part = '') { $data = array(); list($data['bundle'], $data['controller'], $data['template']) = explode(':', $logicalname); return ($part ? $data[$part] : $data); } } |