Completed
Pull Request — master (#125)
by Grégoire
01:17
created

BundleGenerator::generate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 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): void
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): void
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): void
72
    {
73
        $application = explode('\\', $bundleMetadata->getExtendedNamespace())[0];
74
        $file = sprintf(
75
            '%s/%s%s.php',
76
            $bundleMetadata->getExtendedDirectory(),
77
            $application,
78
            $bundleMetadata->getName()
79
        );
80
81
        if (is_file($file)) {
82
            return;
83
        }
84
85
        $output->writeln(sprintf('  > generating bundle file <comment>%s</comment>', $file));
86
87
        $string = Mustache::replace($this->getBundleTemplate(), array(
88
            'application' => $application,
89
            'bundle' => $bundleMetadata->getName(),
90
            'namespace' => $bundleMetadata->getExtendedNamespace(),
91
        ));
92
93
        file_put_contents($file, $string);
94
    }
95
96
    /**
97
     * @return string
98
     */
99
    protected function getBundleTemplate(): string
100
    {
101
        return $this->bundleTemplate;
102
    }
103
}
104