PageRenderTime 70ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/sbweb/sbweb_logica/lib/symfony/plugins/sfPropelPlugin/lib/vendor/propel-generator/classes/propel/engine/GeneratorConfig.php

http://opac-sbweb.googlecode.com/
PHP | 211 lines | 94 code | 21 blank | 96 comment | 11 complexity | 09cfce438bf41fde5d7d9f686225262e MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-3.0
  1. <?php
  2. /*
  3. * $Id: GeneratorConfig.php 989 2008-03-11 14:29:30Z heltem $
  4. *
  5. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  6. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  7. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  8. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  9. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  10. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  11. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  12. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  13. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  14. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  15. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16. *
  17. * This software consists of voluntary contributions made by many individuals
  18. * and is licensed under the LGPL. For more information please see
  19. * <http://propel.phpdb.org>.
  20. */
  21. /**
  22. * A class that holds build properties and provide a class loading mechanism for the generator.
  23. *
  24. * @author Hans Lellelid <hans@xmpl.org>
  25. * @package propel.engine
  26. */
  27. class GeneratorConfig {
  28. /**
  29. * The build properties.
  30. *
  31. * @var array
  32. */
  33. private $buildProperties = array();
  34. /**
  35. * Construct a new GeneratorConfig.
  36. * @param mixed $props Array or Iterator
  37. */
  38. public function __construct($props = null)
  39. {
  40. if ($props) $this->setBuildProperties($props);
  41. }
  42. /**
  43. * Gets the build properties.
  44. * @return array
  45. */
  46. public function getBuildProperties()
  47. {
  48. return $this->buildProperties;
  49. }
  50. /**
  51. * Parses the passed-in properties, renaming and saving eligible properties in this object.
  52. *
  53. * Renames the propel.xxx properties to just xxx and renames any xxx.yyy properties
  54. * to xxxYyy as PHP doesn't like the xxx.yyy syntax.
  55. *
  56. * @param mixed $props Array or Iterator
  57. */
  58. public function setBuildProperties($props)
  59. {
  60. $this->buildProperties = array();
  61. $renamedPropelProps = array();
  62. foreach ($props as $key => $propValue) {
  63. if (strpos($key, "propel.") === 0) {
  64. $newKey = substr($key, strlen("propel."));
  65. $j = strpos($newKey, '.');
  66. while ($j !== false) {
  67. $newKey = substr($newKey, 0, $j) . ucfirst(substr($newKey, $j + 1));
  68. $j = strpos($newKey, '.');
  69. }
  70. $this->setBuildProperty($newKey, $propValue);
  71. }
  72. }
  73. }
  74. /**
  75. * Gets a specific propel (renamed) property from the build.
  76. *
  77. * @param string $name
  78. * @return mixed
  79. */
  80. public function getBuildProperty($name)
  81. {
  82. return isset($this->buildProperties[$name]) ? $this->buildProperties[$name] : null;
  83. }
  84. /**
  85. * Sets a specific propel (renamed) property from the build.
  86. *
  87. * @param string $name
  88. * @param mixed $value
  89. */
  90. public function setBuildProperty($name, $value)
  91. {
  92. $this->buildProperties[$name] = $value;
  93. }
  94. /**
  95. * Resolves and returns the class name based on the specified property value.
  96. *
  97. * @param string $propname The name of the property that holds the class path (dot-path notation).
  98. * @return string The class name.
  99. * @throws BuildException If the classname cannot be determined or class cannot be loaded.
  100. */
  101. public function getClassname($propname)
  102. {
  103. $classpath = $this->getBuildProperty($propname);
  104. if (empty($classpath)) {
  105. throw new BuildException("Unable to find class path for '$propname' property.");
  106. }
  107. // This is a slight hack to workaround camel case inconsistencies for the DDL classes.
  108. // Basically, we want to turn ?.?.?.sqliteDDLBuilder into ?.?.?.SqliteDDLBuilder
  109. $lastdotpos = strrpos($classpath, '.');
  110. if ($lastdotpos !== null) {
  111. $classpath{$lastdotpos+1} = strtoupper($classpath{$lastdotpos+1});
  112. } else {
  113. $classpath = ucfirst($classpath);
  114. }
  115. if (empty($classpath)) {
  116. throw new BuildException("Unable to find class path for '$propname' property.");
  117. }
  118. $clazz = Phing::import($classpath);
  119. return $clazz;
  120. }
  121. /**
  122. * Resolves and returns the builder class name.
  123. *
  124. * @param string $type
  125. * @return string The class name.
  126. */
  127. public function getBuilderClassname($type)
  128. {
  129. $propname = 'builder' . ucfirst(strtolower($type)) . 'Class';
  130. return $this->getClassname($propname);
  131. }
  132. /**
  133. * Creates and configures a new Platform class.
  134. *
  135. * @param PDO $con
  136. * @return Platform
  137. */
  138. public function getConfiguredPlatform(PDO $con = null)
  139. {
  140. $clazz = $this->getClassname("platformClass");
  141. $platform = new $clazz();
  142. if (!$platform instanceof Platform) {
  143. throw new BuildException("Specified platform class ($clazz) does not implement Platform interface.", $this->getLocation());
  144. }
  145. $platform->setConnection($con);
  146. $platform->setGeneratorConfig($this);
  147. return $platform;
  148. }
  149. /**
  150. * Creates and configures a new SchemaParser class for specified platform.
  151. * @param PDO $con
  152. * @return SchemaParser
  153. */
  154. public function getConfiguredSchemaParser(PDO $con = null)
  155. {
  156. $clazz = $this->getClassname("reverseParserClass");
  157. $parser = new $clazz();
  158. if (!$parser instanceof SchemaParser) {
  159. throw new BuildException("Specified platform class ($clazz) does implement SchemaParser interface.", $this->getLocation());
  160. }
  161. $parser->setConnection($con);
  162. $parser->setGeneratorConfig($this);
  163. return $parser;
  164. }
  165. /**
  166. * Gets a configured data model builder class for specified table and based on type.
  167. *
  168. * @param Table $table
  169. * @param string $type The type of builder ('ddl', 'sql', etc.)
  170. * @return DataModelBuilder
  171. */
  172. public function getConfiguredBuilder(Table $table, $type, $cache = true)
  173. {
  174. $classname = $this->getBuilderClassname($type);
  175. $builder = new $classname($table);
  176. $builder->setGeneratorConfig($this);
  177. return $builder;
  178. }
  179. /**
  180. * Gets a configured Pluralizer class.
  181. *
  182. * @return Pluralizer
  183. */
  184. public function getConfiguredPluralizer()
  185. {
  186. $classname = $this->getBuilderClassname('pluralizer');
  187. $pluralizer = new $classname();
  188. return $pluralizer;
  189. }
  190. }