Completed
Push — feature/EVO-5985-real-upload-c... ( f8846f...273dbd )
by
unknown
11:15
created

BundleGenerator::generateBundle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
rs 9.6666
ccs 0
cts 7
cp 0
cc 1
eloc 6
nc 1
nop 1
crap 2
1
<?php
2
/**
3
 * various code generators
4
 */
5
6
namespace Graviton\GeneratorBundle\Generator;
7
8
use Symfony\Component\DependencyInjection\Container;
9
use Sensio\Bundle\GeneratorBundle\Model\Bundle;
10
11
/**
12
 * bundle containing various code generators
13
 *
14
 * This code is more or less loosley based on SensioBundleGenerator. It could
15
 * use some refactoring to duplicate less for that, but this is how i finally
16
 * got a working version.
17
 *
18
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
19
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
20
 * @link     http://swisscom.ch
21
 */
22
class BundleGenerator extends AbstractGenerator
23
{
24
    /**
25
     * generate a bundle from a Bundle model
26
     *
27
     * @param Bundle $bundle bundle model
28
     *
29
     * @return void
30
     */
31
    public function generateBundle(Bundle $bundle)
32
    {
33
        return $this->generate(
34
            $bundle->getNamespace(),
35
            $bundle->getName(),
36
            $bundle->getTargetDirectory().'/../../',
37
            $bundle->getConfigurationFormat()
38
        );
39
    }
40
41
    /**
42
     * generate bundle code
43
     *
44
     * @param string $namespace namspace name
45
     * @param string $bundle    bundle name
46
     * @param string $dir       bundle dir
47
     * @param string $format    bundle condfig file format
48
     *
49
     * @return void
50
     */
51
    public function generate($namespace, $bundle, $dir, $format)
52
    {
53
        $dir .= '/' . strtr($namespace, '\\', '/');
54
        if (file_exists($dir)) {
55
            if (!is_dir($dir)) {
56
                throw new \RuntimeException(
57
                    sprintf(
58
                        'Unable to generate the bundle as the target directory "%s" exists but is a file.',
59
                        realpath($dir)
60
                    )
61
                );
62
            }
63
            $files = scandir($dir);
64
            if ($files != array('.', '..')) {
65
                throw new \RuntimeException(
66
                    sprintf(
67
                        'Unable to generate the bundle as the target directory "%s" is not empty.',
68
                        realpath($dir)
69
                    )
70
                );
71
            }
72
            if (!is_writable($dir)) {
73
                throw new \RuntimeException(
74
                    sprintf(
75
                        'Unable to generate the bundle as the target directory "%s" is not writable.',
76
                        realpath($dir)
77
                    )
78
                );
79
            }
80
        }
81
82
        $basename = $this->getBundleBaseName($bundle);
83
        $parameters = array(
84
            'namespace' => $namespace,
85
            'bundle' => $bundle,
86
            'format' => $format,
87
            'bundle_basename' => $basename,
88
            'extension_alias' => Container::underscore($basename),
89
        );
90
91
        $this->renderFile('bundle/Bundle.php.twig', $dir . '/' . $bundle . '.php', $parameters);
92
        $this->renderFile(
93
            'bundle/Extension.php.twig',
94
            $dir . '/DependencyInjection/' . $basename . 'Extension.php',
95
            $parameters
96
        );
97
98
        if ('xml' === $format || 'annotation' === $format) {
99
            // @todo make this leave doctrine alone and move doctrine to a Manipulator in generate:resource
100
            $this->renderFile('bundle/services.xml.twig', $dir . '/Resources/config/services.xml', $parameters);
101
            mkdir($dir . '/Resources/config/doctrine');
102
            $this->renderFile('bundle/config.xml.twig', $dir . '/Resources/config/config.xml', $parameters);
103
        } else {
104
            $this->renderFile(
105
                'bundle/services.' . $format . '.twig',
106
                $dir . '/Resources/config/services.' . $format,
107
                $parameters
108
            );
109
            mkdir($dir . '/Resources/config/doctrine');
110
            $this->renderFile(
111
                'bundle/config.' . $format . '.twig',
112
                $dir . '/Resources/config/config.' . $format,
113
                $parameters
114
            );
115
        }
116
117
        if ('annotation' != $format) {
118
            $this->renderFile(
119
                'bundle/routing.' . $format . '.twig',
120
                $dir . '/Resources/config/routing.' . $format,
121
                $parameters
122
            );
123
        }
124
    }
125
}
126