Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

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