Completed
Push — support/v1.1 ( 3f002f )
by
unknown
21:42
created

AbstractGenerator   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 26.67%

Importance

Changes 0
Metric Value
wmc 5
c 0
b 0
f 0
lcom 1
cbo 3
dl 0
loc 67
ccs 4
cts 15
cp 0.2667
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setSkeletonDirs() 0 8 2
A getBundleBaseName() 0 8 2
A render() 0 14 1
1
<?php
2
/**
3
 * shared stuff for generators
4
 */
5
6
namespace Graviton\GeneratorBundle\Generator;
7
8
use Sensio\Bundle\GeneratorBundle\Generator\Generator;
9
10
/**
11
 * shared stuff for generators
12
 *
13
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
14
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
15
 * @link     http://swisscom.ch
16
 */
17
abstract class AbstractGenerator extends Generator
18
{
19
    /**
20
     * @private string[]
21
     */
22
    private $gravitonSkeletons;
23
24
    /**
25
     * Sets an array of directories to look for templates.
26
     *
27
     * The directories must be sorted from the most specific to the most generic
28
     * directory.
29
     *
30
     * @param array $gravitonSkeletons An array of skeleton dirs
31
     *
32
     * @return void
33
     */
34
    public function setSkeletonDirs($gravitonSkeletons)
35
    {
36
        $gravitonSkeletons = array_merge(
37
            array(__DIR__ . '/../Resources/skeleton'),
38
            $gravitonSkeletons
39
        );
40
        $this->gravitonSkeletons = is_array($gravitonSkeletons) ? $gravitonSkeletons : array($gravitonSkeletons);
41
    }
42
43
    /**
44
     * Check for the occurence of "Bundle" in the given name and remove it
45
     *
46
     * @param String $name Bundle name
47
     *
48
     * @return string $name Bundle base name
49
     */
50 6
    public function getBundleBaseName($name)
51
    {
52 6
        if ('bundle' === strtolower(substr($name, -6))) {
53 4
            $name = substr($name, 0, -6);
54
        }
55
56 6
        return $name;
57
    }
58
59
    /**
60
     * {@inheritDoc}
61
     *
62
     * render a new object using twig
63
     *
64
     * @param string $template   template to use
65
     * @param array  $parameters info used in creating the object
66
     *
67
     * @return string
68
     */
69
    protected function render($template, $parameters)
70
    {
71
        $twig = new \Twig_Environment(
72
            new \Twig_Loader_Filesystem($this->gravitonSkeletons),
73
            array(
74
                'debug' => true,
75
                'cache' => false,
76
                'strict_variables' => true,
77
                'autoescape' => false,
78
            )
79
        );
80
81
        return $twig->render($template, $parameters);
82
    }
83
}
84