/DatabaseSchema/src/handlers/persistent/class_writer.php

https://github.com/F5/zetacomponents · PHP · 290 lines · 124 code · 20 blank · 146 comment · 7 complexity · 53b8e5f2317a89bc7f65b0a354cc603f MD5 · raw file

  1. <?php
  2. /**
  3. * File containing the ezcDbSchemaPersistentClassWriter class.
  4. *
  5. * Licensed to the Apache Software Foundation (ASF) under one
  6. * or more contributor license agreements. See the NOTICE file
  7. * distributed with this work for additional information
  8. * regarding copyright ownership. The ASF licenses this file
  9. * to you under the Apache License, Version 2.0 (the
  10. * "License"); you may not use this file except in compliance
  11. * with the License. You may obtain a copy of the License at
  12. *
  13. * http://www.apache.org/licenses/LICENSE-2.0
  14. *
  15. * Unless required by applicable law or agreed to in writing,
  16. * software distributed under the License is distributed on an
  17. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  18. * KIND, either express or implied. See the License for the
  19. * specific language governing permissions and limitations
  20. * under the License.
  21. *
  22. * @package DatabaseSchema
  23. * @version //autogentag//
  24. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
  25. */
  26. /**
  27. * This handler creates PHP classes to be used with PersistentObject from a
  28. * DatabaseSchema.
  29. *
  30. * @package DatabaseSchema
  31. * @version //autogentag//
  32. */
  33. class ezcDbSchemaPersistentClassWriter implements ezcDbSchemaFileWriter
  34. {
  35. /**
  36. * If files should be overwritten.
  37. *
  38. * @var boolean
  39. */
  40. private $overwrite;
  41. /**
  42. * Class prefix.
  43. *
  44. * @var string
  45. */
  46. private $prefix;
  47. /**
  48. * Creates a new writer instance
  49. *
  50. * @param bool $overwrite Overwrite existsing files?
  51. * @param string $classPrefix Prefix for class names.
  52. * @return void
  53. */
  54. public function __construct( $overwrite = false, $classPrefix = null )
  55. {
  56. $this->overwrite = $overwrite;
  57. $this->prefix = ( $classPrefix === null ) ? "" : $classPrefix;
  58. }
  59. /**
  60. * Returns what type of schema writer this class implements.
  61. * This method always returns ezcDbSchema::FILE
  62. *
  63. * @return int The type of this schema writer.
  64. */
  65. public function getWriterType()
  66. {
  67. return ezcDbSchema::FILE;
  68. }
  69. /**
  70. * Writes the schema definition in $dbSchema to files located in $dir.
  71. * This method dumps the given schema to PersistentObject definitions, which
  72. * will be located in the given directory.
  73. *
  74. * @param string $dir The directory to store definitions in.
  75. * @param ezcDbSchema $dbSchema The schema object to create defs for.
  76. *
  77. * @throws ezcBaseFileNotFoundException If the given directory could not be
  78. * found.
  79. * @throws ezcBaseFilePermissionException If the given directory is not
  80. * writable.
  81. */
  82. public function saveToFile( $dir, ezcDbSchema $dbSchema )
  83. {
  84. if ( !is_dir( $dir ) )
  85. {
  86. throw new ezcBaseFileNotFoundException( $dir, 'directory' );
  87. }
  88. if ( !is_writable( $dir ) )
  89. {
  90. throw new ezcBaseFilePermissionException( $dir, ezcBaseFileException::WRITE );
  91. }
  92. $schema = $dbSchema->getSchema();
  93. foreach ( $schema as $tableName => $table )
  94. {
  95. $this->writeClass( $dir, $tableName, $table );
  96. }
  97. }
  98. /**
  99. * Writes the list of attributes.
  100. *
  101. * @param resource $file
  102. * @param array $fields
  103. * @return void
  104. */
  105. private function writeAttributes( $file, $fields )
  106. {
  107. foreach ( $fields as $fieldName => $field )
  108. {
  109. fwrite( $file, " /**\n" );
  110. fwrite( $file, " * $fieldName\n" );
  111. fwrite( $file, " *\n" );
  112. fwrite( $file, " * @var {$this->translateType($field->type)}\n" );
  113. fwrite( $file, " */\n" );
  114. fwrite( $file, " private \$$fieldName;\n" );
  115. }
  116. }
  117. /**
  118. * Writes the setState() method for the class.
  119. *
  120. * @param resource $file
  121. * @return void
  122. */
  123. private function writeSetState( $file )
  124. {
  125. fwrite( $file, " /**\n" );
  126. fwrite( $file, " * Set the PersistentObject state.\n" );
  127. fwrite( $file, " *\n" );
  128. fwrite( $file, " * @param array(string=>mixed) \$state The state to set.\n" );
  129. fwrite( $file, " * @return void\n" );
  130. fwrite( $file, " */\n" );
  131. fwrite( $file, " public function setState( array \$state )\n" );
  132. fwrite( $file, " {\n" );
  133. fwrite( $file, " foreach ( \$state as \$attribute => \$value )\n" );
  134. fwrite( $file, " {\n" );
  135. fwrite( $file, " \$this->\$attribute = \$value;\n" );
  136. fwrite( $file, " }\n" );
  137. fwrite( $file, " }\n" );
  138. }
  139. /**
  140. * Writes the getState() method for the class.
  141. *
  142. * @param resource $file
  143. * @param array $fields The table fields.
  144. * @return void
  145. */
  146. private function writeGetState( $file, $fields )
  147. {
  148. fwrite( $file, " /**\n" );
  149. fwrite( $file, " * Get the PersistentObject state.\n" );
  150. fwrite( $file, " *\n" );
  151. fwrite( $file, " * @return array(string=>mixed) The state of the object.\n" );
  152. fwrite( $file, " */\n" );
  153. fwrite( $file, " public function getState()\n" );
  154. fwrite( $file, " {\n" );
  155. fwrite( $file, " return array(\n" );
  156. foreach ( $fields as $fieldName => $field )
  157. {
  158. fwrite( $file, " '$fieldName' => \$this->$fieldName,\n" );
  159. }
  160. fwrite( $file, " );\n" );
  161. fwrite( $file, " }\n" );
  162. }
  163. /**
  164. * Writes a PHP class.
  165. * This method writes a PHP class from a table definition.
  166. *
  167. * @param string $dir The directory to write the defititions to.
  168. * @param string $tableName Name of the database table.
  169. * @param ezcDbSchemaTable $table The table definition.
  170. */
  171. private function writeClass( $dir, $tableName, ezcDbSchemaTable $table )
  172. {
  173. $file = $this->openFile( $dir, $tableName );
  174. fwrite( $file, "/**\n" );
  175. fwrite( $file, " * Data class $tableName.\n" );
  176. fwrite( $file, " * Class to be used with eZ Components PersistentObject.\n" );
  177. fwrite( $file, " */\n" );
  178. fwrite( $file, "class {$this->prefix}$tableName\n" );
  179. fwrite( $file, "{\n" );
  180. // attributes
  181. $this->writeAttributes( $file, $table->fields );
  182. fwrite( $file, "\n" );
  183. // methods
  184. $this->writeSetState( $file );
  185. fwrite( $file, "\n" );
  186. $this->writeGetState( $file, $table->fields );
  187. fwrite( $file, "}\n" );
  188. $this->closeFile( $file );
  189. }
  190. /**
  191. * Open a file for writing a PersistentObject definition to.
  192. * This method opens a file for writing a PersistentObject definition to
  193. * and writes the basic PHP open tag to it.
  194. *
  195. * @param string $dir The diretory to open the file in.
  196. * @param string $name The table name.
  197. * @return resource(file) The file resource used for writing.
  198. *
  199. * @throws ezcBaseFileIoException
  200. * if the file to write to already exists.
  201. * @throws ezcBaseFilePermissionException
  202. * if the file could not be opened for writing.
  203. */
  204. private function openFile( $dir, $name )
  205. {
  206. $filename = $dir . DIRECTORY_SEPARATOR . strtolower( $this->prefix ) . strtolower( $name ) . '.php';
  207. // We do not want to overwrite files
  208. if ( file_exists( $filename ) && ( $this->overwrite === false || is_writable( $filename ) === false ) )
  209. {
  210. throw new ezcBaseFileIoException( $filename, ezcBaseFileException::WRITE, "File already exists or is not writeable. Use --overwrite to ignore existance." );
  211. }
  212. $file = @fopen( $filename, 'w' );
  213. if ( $file === false )
  214. {
  215. throw new ezcBaseFilePermissionException( $file, ezcBaseFileException::WRITE );
  216. }
  217. fwrite( $file, "<?php\n" );
  218. fwrite( $file, "// Autogenerated class file\n" );
  219. fwrite( $file, "\n" );
  220. return $file;
  221. }
  222. /**
  223. * Close a file where a PersistentObject definition has been written to.
  224. * This method closes a file after writing a PersistentObject definition to
  225. * it and writes the PHP closing tag to it.
  226. *
  227. * @param resource(file) $file The file resource to close.
  228. * @return void
  229. */
  230. private function closeFile( $file )
  231. {
  232. fwrite( $file, "?>\n" );
  233. fclose( $file );
  234. }
  235. /**
  236. * Translates eZ DatabaseSchema data types to eZ PersistentObject types.
  237. * This method receives a type string from a ezcDbSchemaField object and
  238. * returns the corresponding type value from PersistentObject.
  239. *
  240. * @todo Why does PersistentObject not support "boolean" types?
  241. *
  242. * @see ezcPersistentObjectProperty::TYPE_INT
  243. * @see ezcPersistentObjectProperty::TYPE_FLOAT
  244. * @see ezcPersistentObjectProperty::TYPE_STRING
  245. *
  246. * @param string $dbType The DatabaseSchema type string.
  247. * @return int The ezcPersistentObjectProperty::TYPE_* value.
  248. */
  249. private function translateType( $dbType )
  250. {
  251. switch ( $dbType )
  252. {
  253. case 'integer':
  254. case 'timestamp':
  255. return 'int';
  256. case 'float':
  257. case 'decimal':
  258. return 'float';
  259. case 'text':
  260. case 'blob':
  261. case 'clob':
  262. return 'string';
  263. case 'time':
  264. case 'date':
  265. return 'mixed';
  266. default:
  267. }
  268. }
  269. }
  270. ?>