Failed Conditions
Pull Request — 2.7 (#7901)
by Luís
06:54
created

EntityRepositoryGenerator::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
/*
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the MIT license. For more information, see
17
 * <http://www.doctrine-project.org>.
18
 */
19
20
namespace Doctrine\ORM\Tools;
21
22
use Doctrine\ORM\EntityRepository;
23
use function trigger_error;
24
25
/**
26
 * Class to generate entity repository classes
27
 *
28
 *
29
 * @link    www.doctrine-project.org
30
 * @since   2.0
31
 * @author  Benjamin Eberlei <[email protected]>
32
 * @author  Guilherme Blanco <[email protected]>
33
 * @author  Jonathan Wage <[email protected]>
34
 * @author  Roman Borschel <[email protected]>
35
 *
36
 * @deprecated 3.0 This class is being removed from the ORM and won't have any replacement
37
 */
38
class EntityRepositoryGenerator
39
{
40
    private $repositoryName;
41
42
    protected static $_template =
43
'<?php
44
45
<namespace>
46
47
/**
48
 * <className>
49
 *
50
 * This class was generated by the Doctrine ORM. Add your own custom
51
 * repository methods below.
52
 */
53
class <className> extends <repositoryName>
54
{
55
}
56
';
57
58 4
    public function __construct()
59
    {
60 4
        @trigger_error(self::class . ' is deprecated and will be removed in Doctrine 3.0', E_USER_DEPRECATED);
61 4
    }
62
63
    /**
64
     * @param string $fullClassName
65
     *
66
     * @return string
67
     */
68 4
    public function generateEntityRepositoryClass($fullClassName)
69
    {
70
        $variables = [
71 4
            '<namespace>'       => $this->generateEntityRepositoryNamespace($fullClassName),
72 4
            '<repositoryName>'  => $this->generateEntityRepositoryName($fullClassName),
73 4
            '<className>'       => $this->generateClassName($fullClassName)
74
        ];
75
76 4
        return str_replace(array_keys($variables), array_values($variables), self::$_template);
77
    }
78
79
    /**
80
     * Generates the namespace, if class do not have namespace, return empty string instead.
81
     *
82
     * @param string $fullClassName
83
     *
84
     * @return string $namespace
85
     */
86 4
    private function getClassNamespace($fullClassName)
87
    {
88 4
        $namespace = substr($fullClassName, 0, strrpos($fullClassName, '\\'));
89
90 4
        return $namespace;
91
    }
92
93
    /**
94
     * Generates the class name
95
     *
96
     * @param string $fullClassName
97
     *
98
     * @return string
99
     */
100 4
    private function generateClassName($fullClassName)
101
    {
102 4
        $namespace = $this->getClassNamespace($fullClassName);
103
104 4
        $className = $fullClassName;
105
106 4
        if ($namespace) {
107 4
            $className = substr($fullClassName, strrpos($fullClassName, '\\') + 1, strlen($fullClassName));
108
        }
109
110 4
        return $className;
111
    }
112
113
    /**
114
     * Generates the namespace statement, if class do not have namespace, return empty string instead.
115
     *
116
     * @param string $fullClassName The full repository class name.
117
     *
118
     * @return string $namespace
119
     */
120 4
    private function generateEntityRepositoryNamespace($fullClassName)
121
    {
122 4
        $namespace = $this->getClassNamespace($fullClassName);
123
124 4
        return $namespace ? 'namespace ' . $namespace . ';' : '';
125
    }
126
127
    /**
128
     * @param string $fullClassName
129
     *
130
     * @return string $repositoryName
131
     */
132 4
    private function generateEntityRepositoryName($fullClassName)
133
    {
134 4
        $namespace = $this->getClassNamespace($fullClassName);
135
136 4
        $repositoryName = $this->repositoryName ?: EntityRepository::class;
137
138 4
        if ($namespace && $repositoryName[0] !== '\\') {
139 4
            $repositoryName = '\\' . $repositoryName;
140
        }
141
142 4
        return $repositoryName;
143
    }
144
145
    /**
146
     * @param string $fullClassName
147
     * @param string $outputDirectory
148
     *
149
     * @return void
150
     */
151 4
    public function writeEntityRepositoryClass($fullClassName, $outputDirectory)
152
    {
153 4
        $code = $this->generateEntityRepositoryClass($fullClassName);
154
155 4
        $path = $outputDirectory . DIRECTORY_SEPARATOR
156 4
              . str_replace('\\', \DIRECTORY_SEPARATOR, $fullClassName) . '.php';
157 4
        $dir = dirname($path);
158
159 4
        if ( ! is_dir($dir)) {
160 2
            mkdir($dir, 0775, true);
161
        }
162
163 4
        if ( ! file_exists($path)) {
164 4
            file_put_contents($path, $code);
165 4
            chmod($path, 0664);
166
        }
167 4
    }
168
169
    /**
170
     * @param string $repositoryName
171
     *
172
     * @return \Doctrine\ORM\Tools\EntityRepositoryGenerator
173
     */
174 4
    public function setDefaultRepositoryName($repositoryName)
175
    {
176 4
        $this->repositoryName = $repositoryName;
177
178 4
        return $this;
179
    }
180
181
}
182