1: <?php
2:
3: namespace Mapbender\CoreBundle\Command;
4:
5: use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
6: use Sensio\Bundle\GeneratorBundle\Generator\Generator;
7: use Symfony\Component\Console\Input\InputArgument;
8: use Symfony\Component\Console\Input\InputOption;
9: use Symfony\Component\Console\Input\InputInterface;
10: use Symfony\Component\Console\Output\OutputInterface;
11: use Symfony\Component\Console\Output\Output;
12:
13: class GenerateTemplateCommand extends ContainerAwareCommand {
14: protected function getGenerator() {
15: if($this->generator === null) {
16: $this->generator = new TemplateGenerator();
17: }
18: return $this->generator;
19: }
20: protected function configure() {
21: $this->setDefinition(array(
22: new InputArgument('bundle', InputArgument::REQUIRED, 'The bundle namespace of the Template to create'),
23: new InputArgument('classname', InputArgument::REQUIRED, 'The classname of the Template to create'),
24: new InputArgument('dir', InputArgument::REQUIRED, 'The directory where to find the bundle'),
25: ))
26: ->setHelp(<<<EOT
27: The <info>mapbender:generate:template</info> command generates a new Mapbender application template.
28:
29: <info>./app/console/ mapbender:generate:template "Vendor\HelloBundle" MyTemplate src </info>
30:
31: The generated Element class will be Vendor\HelloBundle\Template\MyElement.
32: EOT
33: )
34: ->setName('mapbender:generate:template')
35: ->setDescription('Generates a Mapbender application template');
36: }
37:
38: protected function execute(InputInterface $input, OutputInterface $output) {
39: $bundleNamespace = $input->getArgument('bundle');
40: $className = $input->getArgument('classname');
41: $type = $input->getOption('type');
42:
43:
44: //TODO: Does this work?
45: if (preg_match('[^A-Za-z0-9]', $className)) {
46: throw new \InvalidArgumentException('The classname contains invalid characters.');
47: }
48: // validate namespace
49: $bundleNamespace = strtr($bundleNamespace, '/', '\\');
50: if (preg_match('/[^A-Za-z0-9_\\\-]/', $bundleNamespace)) {
51: throw new \InvalidArgumentException('The bundle namespace contains invalid characters.');
52: }
53:
54: // validate that the namespace is at least one level deep
55: if (false === strpos($bundleNamespace, '\\')) {
56: $msg = array();
57: $msg[] = sprintf('The namespace must contain a vendor namespace (e.g. "VendorName\%s" instead of simply "%s").', $bundleNamespace, $bundleNamespace);
58: $msg[] = 'If you\'ve specified a vendor namespace, did you forget to surround it with quotes (mapbender:generate:element "Acme\BlogBundle")?';
59:
60: throw new \InvalidArgumentException(implode("\n\n", $msg));
61: }
62:
63: $dir = $input->getArgument('dir');
64:
65: // add trailing / if necessary
66: $dir = '/' === substr($dir, -1, 1) ? $dir : $dir.'/';
67: $bundleDir = $dir.strtr($bundleNamespace, '\\', '/');
68: $bundle = strtr($bundleNamespace, array('\\' => ''));
69:
70: if (!file_exists($bundleDir)) {
71: throw new \RuntimeException(sprintf('Bundle directory "%s" does not exist.', $bundleDir));
72: }
73:
74: $files = $this->getGenerator()->create($this->getContainer(),
75: $bundle, $bundleDir, $bundleNamespace, $className);
76:
77: $output->writeln('<comment>Summary of actions</comment>');
78: $output->writeln(sprintf('- Your element %s\Template\%s has been created.', $bundle, $className));
79: $output->writeln('- The following files have been created:');
80: foreach($files as $k => $v) {
81: $output->writeLn(sprintf(' - %s (%s)', $k, $v));
82: }
83: $output->writeln('');
84: $output->writeln('<comment>Follow up actions</comment>');
85: $output->writeln('Read about adapting your bare-bone element at <info>http://mapbender.org/3/cookbook/template-from-skeleton</info>');
86: }
87: }
88:
89: