BundleGenerator::generateBundleDirectory()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

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