-
Notifications
You must be signed in to change notification settings - Fork 181
/
Copy pathsfGeneratorConfigHandler.class.php
84 lines (71 loc) · 2.99 KB
/
sfGeneratorConfigHandler.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<?php
/*
* This file is part of the symfony package.
* (c) 2004-2006 Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* sfGeneratorConfigHandler.
*
* @author Fabien Potencier <[email protected]>
*/
class sfGeneratorConfigHandler extends sfYamlConfigHandler
{
/**
* Executes this configuration handler.
*
* @param array $configFiles An array of absolute filesystem path to a configuration file
*
* @return string Data to be written to a cache file
*
* @throws sfConfigurationException If a requested configuration file does not exist or is not readable
* @throws sfParseException If a requested configuration file is improperly formatted
* @throws sfInitializationException If a generator.yml key check fails
*/
public function execute($configFiles)
{
// parse the yaml
$config = static::getConfiguration($configFiles);
if (!$config) {
return '';
}
if (!isset($config['generator'])) {
throw new sfParseException(sprintf('Configuration file "%s" must specify a generator section.', $configFiles[1] ?? $configFiles[0]));
}
$config = $config['generator'];
if (!isset($config['class'])) {
throw new sfParseException(sprintf('Configuration file "%s" must specify a generator class section under the generator section.', $configFiles[1] ?? $configFiles[0]));
}
foreach (['fields', 'list', 'edit'] as $section) {
if (isset($config[$section])) {
throw new sfParseException(sprintf('Configuration file "%s" can specify a "%s" section but only under the param section.', $configFiles[1] ?? $configFiles[0], $section));
}
}
// generate class and add a reference to it
$generatorManager = new sfGeneratorManager(sfContext::getInstance()->getConfiguration());
// generator parameters
$generatorParam = ($config['param'] ?? []);
// hack to find the module name (look for the last /modules/ in path)
preg_match('#.*/modules/([^/]+)/#', str_replace('\\', '/', $configFiles[0]), $match);
$generatorParam['moduleName'] = $match[1];
// compile data
$retval = "<?php\n".
"// auto-generated by sfGeneratorConfigHandler\n".
"// date: %s\n%s\n";
$retval = sprintf($retval, date('Y/m/d H:i:s'), static::getContent($generatorManager, $config['class'], $generatorParam));
return $retval;
}
public static function getContent(sfGeneratorManager $generatorManager, $class, $parameters)
{
return $generatorManager->generate($class, $parameters);
}
/**
* @see sfConfigHandler
*/
public static function getConfiguration(array $configFiles)
{
return static::parseYamls($configFiles);
}
}