8000 [Config][DependencyInjection] Add configuration builder for writing P… · symfony/symfony@460b46f · GitHub
[go: up one dir, main page]

Skip to content

Commit 460b46f

Browse files
Nyholmnicolas-grekas
authored andcommitted
[Config][DependencyInjection] Add configuration builder for writing PHP config
1 parent 9092d5b commit 460b46f

28 files changed

+1290
-3
lines changed
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
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 Symfony\Component\Config\Builder;
13+
14+
/**
15+
* Build PHP classes to generate config.
16+
*
17+
* @internal
18+
*
19+
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
20+
*/
21+
class ClassBuilder
22+
{
23+
/** @var string */
24+
private $namespace;
25+
26+
/** @var string */
27+
private $name;
28+
29+
/** @var Property[] */
30+
private $properties = [];
31+
32+
/** @var Method[] */
33+
private $methods = [];
34+
private $require = [];
35+
private $implements = [];
36+
37+
public function __construct(string $namespace, string $name)
38+
{
39+
$this->namespace = $namespace;
40+
$this->name = ucfirst($this->camelCase($name)).'Config';
41+
}
42+
43+
public function getDirectory(): string
44+
{
45+
return str_replace('\\', \DIRECTORY_SEPARATOR, $this->namespace);
46+
}
47+
48+
public function getFilename(): string
49+
{
50+
return $this->name.'.php';
51+
}
52+
53+
public function build(): string
54+
{
55+
$rootPath = explode(\DIRECTORY_SEPARATOR, $this->getDirectory());
56+
$require = '';
57+
foreach ($this->require as $class) {
58< 2934 /td>+
// figure out relative path.
59+
$path = explode(\DIRECTORY_SEPARATOR, $class->getDirectory());
60+
$path[] = $class->getFilename();
61+
foreach ($rootPath as $key => $value) {
62+
if ($path[$key] !== $value) {
63+
break;
64+
}
65+
unset($path[$key]);
66+
}
67+
$require .= sprintf('require_once __DIR__.\'%s\';', \DIRECTORY_SEPARATOR.implode(\DIRECTORY_SEPARATOR, $path))."\n";
68+
}
69+
70+
$implements = [] === $this->implements ? '' : 'implements '.implode(', ', $this->implements);
71+
$body = '';
72+
foreach ($this->properties as $property) {
73+
$body .= ' '.$property->getContent()."\n";
74+
}
75+
foreach ($this->methods as $method) {
76+
$lines = explode("\n", $method->getContent());
77+
foreach ($lines as $i => $line) {
78+
$body .= ' '.$line."\n";
79+
}
80+
}
81+
82+
$content = strtr('<?php
83+
84+
namespace NAMESPACE;
85+
86+
REQUIRE
87+
88+
/**
89+
* This class is automatically generated to help creating config.
90+
*
91+
* @experimental in 5.3
92+
*/
93+
class CLASS IMPLEMENTS
94+
{
95+
BODY
96+
}
97+
', ['NAMESPACE' => $this->namespace, 'REQUIRE' => $require, 'CLASS' => $this->getName(), 'IMPLEMENTS' => $implements, 'BODY' => $body]);
98+
99+
return $content;
100+
}
101+
102+
public function addRequire(self $class)
103+
{
104+
$this->require[] = $class;
105+
}
106+
107+
public function addImplements(string $interface)
108+
{
109+
$this->implements[] = '\\'.ltrim($interface, '\\');
110+
}
111+
112+
public function addMethod(string $name, string $body, array $params = []): void
113+
{
114+
$this->methods[] = new Method(strtr($body, ['NAME' => $this->camelCase($name)] + $params));
115+
}
116+
117+
public function addProperty(string $name, string $classType = null): Property
118+
{
119+
$property = new Property($name, $this->camelCase($name));
120+
if (null !== $classType) {
121+
$property->setType($classType);
122+
}
123+
$this->properties[] = $property;
124+
$property->setContent(sprintf('private $%s;', $property->getName()));
125+
126+
return $property;
127+
}
128+
129+
public function getProperties(): array
130+
{
131+
return $this->properties;
132+
}
133+
134+
private function camelCase(string $input): string
135+
{
136+
$output = lcfirst(str_replace(' ', '', ucwords(str_replace('_', ' ', $input))));
137+
138+
return preg_replace('#\W#', '', $output);
139+
}
140+
141+
public function getName(): string
142+
{
143+
return $this->name;
144+
}
145+
146+
public function getNamespace(): string
147+
{
148+
return $this->namespace;
149+
}
150+
151+
public function getFqcn()
152+
{
153+
return '\\'.$this->namespace.'\\'.$this->name;
154+
}
155+
}

0 commit comments

Comments
 (0)
0