Completed
Push — 2.x ( dc834d...91b37b )
by
unknown
02:07
created

BundleGenerator   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 9
Bugs 4 Features 2
Metric Value
wmc 8
c 9
b 4
f 2
lcom 1
cbo 3
dl 0
loc 80
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getBundleTemplate() 0 4 1
A __construct() 0 4 1
A generate() 0 5 1
B generateBundleDirectory() 0 24 3
A generateBundleFile() 0 17 2
1
<?php
2
3
/*
4
 * This file is part of the Sonata Project package.
5
 *
6
 * (c) Thomas Rabaix <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sonata\EasyExtendsBundle\Generator;
13
14
use Sonata\EasyExtendsBundle\Bundle\BundleMetadata;
15
use Symfony\Component\Console\Output\OutputInterface;
16
17
class BundleGenerator implements GeneratorInterface
18
{
19
    /**
20
     * @var string
21
     */
22
    protected $bundleTemplate;
23
24
    public function __construct()
25
    {
26
        $this->bundleTemplate = file_get_contents(__DIR__.'/../Resources/skeleton/bundle/bundle.mustache');
27
    }
28
29
    /**
30
     * {@inheritdoc}
31
     */
32
    public function generate(OutputInterface $output, BundleMetadata $bundleMetadata)
33
    {
34
        $this->generateBundleDirectory($output, $bundleMetadata);
35
        $this->generateBundleFile($output, $bundleMetadata);
36
    }
37
38
    /**
39
     * @param OutputInterface $output
40
     * @param BundleMetadata  $bundleMetadata
41
     */
42
    protected function generateBundleDirectory(OutputInterface $output, BundleMetadata $bundleMetadata)
43
    {
44
        $directories = array(
45
            '',
46
            'Resources/config/serializer',
47
            'Resources/config/doctrine',
48
            'Resources/config/routing',
49
            'Resources/views',
50
            'Command',
51
            'DependencyInjection',
52
            'Entity',
53
            'Document',
54
            'PHPCR',
55
            'Controller',
56
        );
57
58
        foreach ($directories as $directory) {
59
            $dir = sprintf('%s/%s', $bundleMetadata->getExtendedDirectory(), $directory);
60
            if (!is_dir($dir)) {
61
                $output->writeln(sprintf('  > generating bundle directory <comment>%s</comment>', $dir));
62
                mkdir($dir, 0755, true);
63
            }
64
        }
65
    }
66
67
    /**
68
     * @param OutputInterface $output
69
     * @param BundleMetadata  $bundleMetadata
70
     */
71
    protected function generateBundleFile(OutputInterface $output, BundleMetadata $bundleMetadata)
72
    {
73
        $file = sprintf('%s/Application%s.php', $bundleMetadata->getExtendedDirectory(), $bundleMetadata->getName());
74
75
        if (is_file($file)) {
76
            return;
77
        }
78
79
        $output->writeln(sprintf('  > generating bundle file <comment>%s</comment>', $file));
80
81
        $string = Mustache::replace($this->getBundleTemplate(), array(
82
            'bundle' => $bundleMetadata->getName(),
83
            'namespace' => $bundleMetadata->getExtendedNamespace(),
84
        ));
85
86
        file_put_contents($file, $string);
87
    }
88
89
    /**
90
     * @return string
91
     */
92
    protected function getBundleTemplate()
93
    {
94
        return $this->bundleTemplate;
95
    }
96
}
97