AbstractGenerator::setClassToExtend()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
/**
3
 * Sake
4
 *
5
 * @link      http://github.com/sandrokeil/CodeGenerator for the canonical source repository
6
 * @copyright Copyright (c) 2014 Sandro Keil
7
 * @license   http://github.com/sandrokeil/CodeGenerator/blob/master/LICENSE.txt New BSD License
8
 */
9
10
namespace Sake\CodeGenerator\Code\Generator;
11
12
use Sake\CodeGenerator\Code\Metadata\MetadataInfo;
13
use Zend\Code\Generator\FileGenerator;
14
15
/**
16
 * Abstract generator
17
 *
18
 * Provides common generator functions
19
 */
20
abstract class AbstractGenerator
21
{
22
    /**
23
     * FCQN of class to extends
24
     *
25
     * @var string
26
     */
27
    protected $classToExtend;
28
29
    /**
30
     * Namespace
31
     *
32
     * @var string
33
     */
34
    protected $namespace = 'CodeGenerator';
35
36
    /**
37
     * Number of spaces
38
     *
39
     * @var int
40
     */
41
    protected $numSpaces = 4;
42
43
    /**
44
     * Generates and writes classes for the given array of ClassMetadataInfo instances.
45
     *
46
     * @param array  $metadatas
47
     * @param string $outputDirectory
48
     */
49
    public function generate(array $metadatas, $outputDirectory)
50
    {
51
        foreach ($metadatas as $metadata) {
52
            $this->writeClass($metadata, $outputDirectory);
53
        }
54
    }
55
56
    /**
57
     * Generates and writes class to disk for the given ClassMetadataInfo instance.
58
     *
59
     * @param MetadataInfo $metadata
60
     * @param string            $outputDirectory
61
     */
62
    public function writeClass(MetadataInfo $metadata, $outputDirectory)
63
    {
64
        $path = $outputDirectory . '/'
65
            . str_replace('\\', DIRECTORY_SEPARATOR, $this->getName($metadata))
66
            . '.php';
67
68
        $dir = dirname($path);
69
70
        if (!is_dir($dir)) {
71
            mkdir($dir, 0777, true);
72
        }
73
        file_put_contents($path, $this->generateClass($metadata)->generate());
74
    }
75
76
    /**
77
     * Sets class which generated class should extend
78
     *
79
     * @param string $classToExtend
80
     */
81
    public function setClassToExtend($classToExtend)
82
    {
83
        $this->classToExtend = $classToExtend;
84
    }
85
86
    /**
87
     * Sets namespace for generated class
88
     *
89
     * @param string $namespace
90
     */
91
    public function setNamespace($namespace)
92
    {
93
        $this->namespace = $namespace;
94
    }
95
96
    /**
97
     * Converts name
98
     *
99
     * @param string $name
100
     * @return string
101
     */
102
    protected function convertName($name)
103
    {
104
        return ucfirst($name);
105
    }
106
107
    /**
108
     * Sets intendation
109
     *
110
     * @param int $numSpaces
111
     */
112
    public function setNumSpaces($numSpaces)
113
    {
114
        $this->numSpaces = $numSpaces;
115
    }
116
117
    /**
118
     * Returns intendation depending on loops
119
     *
120
     * @param int $number Number of loops
121
     * @return string
122
     */
123
    protected function getIntendation($number = 1)
124
    {
125
        $indentation = '';
126
127
        for ($i = 0; $i < $number; $i++) {
128
            $indentation .= str_pad('', $this->numSpaces, ' ');
129
        }
130
        return $indentation;
131
    }
132
133
    /**
134
     * Generates class
135
     *
136
     * @param MetadataInfo $metadata
137
     * @return FileGenerator
138
     */
139
    abstract public function generateClass(MetadataInfo $metadata);
140
141
    /**
142
     * Returns file name for specifc generated type
143
     *
144
     * @param MetadataInfo $metadata
145
     * @return string
146
     */
147
    abstract public function getName(MetadataInfo $metadata);
148
}
149