Completed
Push — master ( 11b317...37df4d )
by Lucas
09:27
created

BundleGenerator   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 8
lcom 0
cbo 2
dl 0
loc 87
ccs 0
cts 59
cp 0
rs 10
c 0
b 0
f 0

1 Method

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