/protected/components/ezcomponents/DatabaseSchema/src/handlers/persistent/writer.php

https://github.com/kamarulismail/kamarul-playground · PHP · 272 lines · 131 code · 18 blank · 123 comment · 10 complexity · a904fddfb6dda7e655f9aa3cd30a706a MD5 · raw file

  1. <?php
  2. /**
  3. * File containing the ezcDbSchemaPersistentWriter class.
  4. *
  5. * @package DatabaseSchema
  6. * @version 1.4.4
  7. * @copyright Copyright (C) 2005-2010 eZ Systems AS. All rights reserved.
  8. * @license http://ez.no/licenses/new_bsd New BSD License
  9. */
  10. /**
  11. * This handler creates PHP classes to be used with PersistentObject from a
  12. * DatabaseSchema.
  13. *
  14. * @package DatabaseSchema
  15. * @version 1.4.4
  16. */
  17. class ezcDbSchemaPersistentWriter implements ezcDbSchemaFileWriter
  18. {
  19. /**
  20. * If files should be overwritten.
  21. *
  22. * @var boolean
  23. */
  24. private $overwrite;
  25. /**
  26. * Class prefix.
  27. *
  28. * @var string
  29. */
  30. private $prefix;
  31. /**
  32. * Creates a new writer instance
  33. *
  34. * @param bool $overwrite Overwrite existsing files?
  35. * @param string $classPrefix Prefix for class names.
  36. * @return void
  37. */
  38. public function __construct( $overwrite = false, $classPrefix = null )
  39. {
  40. $this->overwrite = $overwrite;
  41. $this->prefix = ( $classPrefix === null ) ? "" : $classPrefix;
  42. }
  43. /**
  44. * Returns what type of schema writer this class implements.
  45. * This method always returns ezcDbSchema::FILE
  46. *
  47. * @return int The type of this schema writer.
  48. */
  49. public function getWriterType()
  50. {
  51. return ezcDbSchema::FILE;
  52. }
  53. /**
  54. * Writes the schema definition in $dbSchema to files located in $dir.
  55. * This method dumps the given schema to PersistentObject definitions, which
  56. * will be located in the given directory.
  57. *
  58. * @param string $dir The directory to store definitions in.
  59. * @param ezcDbSchema $dbSchema The schema object to create defs for.
  60. *
  61. * @throws ezcBaseFileNotFoundException If the given directory could not be
  62. * found.
  63. * @throws ezcBaseFilePermissionException If the given directory is not
  64. * writable.
  65. */
  66. public function saveToFile( $dir, ezcDbSchema $dbSchema )
  67. {
  68. if ( !is_dir( $dir ) )
  69. {
  70. throw new ezcBaseFileNotFoundException( $dir, 'directory' );
  71. }
  72. if ( !is_writable( $dir ) )
  73. {
  74. throw new ezcBaseFilePermissionException( $dir, ezcBaseFileException::WRITE );
  75. }
  76. $schema = $dbSchema->getSchema();
  77. foreach ( $schema as $tableName => $table )
  78. {
  79. $this->writeTable( $dir, $tableName, $table );
  80. }
  81. }
  82. /**
  83. * Write a field of the schema to the PersistentObject definition.
  84. * This method writes a database field to the PersistentObject definition
  85. * file.
  86. *
  87. * @param resource(file) $file The file to write to.
  88. * @param string $fieldName The name of the field.
  89. * @param ezcDbSchemaField $field The field object.
  90. * @param bool $isPrimary Whether the field is the primary key.
  91. */
  92. private function writeField( $file, $fieldName, $field, $isPrimary )
  93. {
  94. fwrite( $file, "\n" );
  95. if ( $isPrimary )
  96. {
  97. fwrite( $file, "\$def->idProperty = new ezcPersistentObjectIdProperty();\n" );
  98. fwrite( $file, "\$def->idProperty->columnName = '$fieldName';\n" );
  99. fwrite( $file, "\$def->idProperty->propertyName = '$fieldName';\n" );
  100. if ( $field->autoIncrement )
  101. {
  102. fwrite( $file, "\$def->idProperty->generator = new ezcPersistentGeneratorDefinition( 'ezcPersistentSequenceGenerator' );\n" );
  103. }
  104. else
  105. {
  106. fwrite( $file, "\$def->idProperty->generator = new ezcPersistentGeneratorDefinition( 'ezcPersistentManualGenerator' );\n" );
  107. fwrite( $file, "\$def->idProperty->propertyType = ezcPersistentObjectProperty::PHP_TYPE_STRING;\n" );
  108. }
  109. }
  110. else
  111. {
  112. fwrite( $file, "\$def->properties['$fieldName'] = new ezcPersistentObjectProperty();\n" );
  113. fwrite( $file, "\$def->properties['$fieldName']->columnName = '$fieldName';\n" );
  114. fwrite( $file, "\$def->properties['$fieldName']->propertyName = '$fieldName';\n" );
  115. fwrite( $file, "\$def->properties['$fieldName']->propertyType = {$this->translateType($field->type)};\n" );
  116. }
  117. fwrite( $file, "\n" );
  118. }
  119. /**
  120. * Translates eZ DatabaseSchema data types to eZ PersistentObject types.
  121. * This method receives a type string from a ezcDbSchemaField object and
  122. * returns the corresponding type value from PersistentObject.
  123. *
  124. * @todo Why does PersistentObject not support "boolean" types?
  125. *
  126. * @see ezcPersistentObjectProperty::TYPE_INT
  127. * @see ezcPersistentObjectProperty::TYPE_FLOAT
  128. * @see ezcPersistentObjectProperty::TYPE_STRING
  129. *
  130. * @param string $dbType The DatabaseSchema type string.
  131. * @return int The ezcPersistentObjectProperty::TYPE_* value.
  132. */
  133. private function translateType( $dbType )
  134. {
  135. switch ( $dbType )
  136. {
  137. case 'integer':
  138. case 'timestamp':
  139. case 'boolean':
  140. return 'ezcPersistentObjectProperty::PHP_TYPE_INT';
  141. case 'float':
  142. case 'decimal':
  143. return 'ezcPersistentObjectProperty::PHP_TYPE_FLOAT';
  144. case 'text':
  145. case 'time':
  146. case 'date':
  147. case 'blob':
  148. case 'clob':
  149. default:
  150. return 'ezcPersistentObjectProperty::PHP_TYPE_STRING';
  151. }
  152. }
  153. /**
  154. * Writes the PersistentObject defintion for a table.
  155. *
  156. * This method writes the PersistentObject definition for a single database
  157. * table. It creates a new file in the given directory, named in the format
  158. * <table_name>.php, writes the start of the definition to it and calls the
  159. * {@link ezcDbschemaPersistentWriter::writeField()} method for each of the
  160. * database fields.
  161. *
  162. * The defition files always contain an object instance $def, which is
  163. * returned in the end.
  164. *
  165. * @param string $dir The directory to write the defititions to.
  166. * @param string $tableName Name of the database table.
  167. * @param ezcDbSchemaTable $table The table definition.
  168. */
  169. private function writeTable( $dir, $tableName, ezcDbSchemaTable $table )
  170. {
  171. $file = $this->openFile( $dir, $tableName );
  172. fwrite( $file, "\$def = new ezcPersistentObjectDefinition();\n" );
  173. fwrite( $file, "\$def->table = '$tableName';\n" );
  174. fwrite( $file, "\$def->class = '{$this->prefix}$tableName';\n" );
  175. $primaries = $this->determinePrimaries( $table->indexes );
  176. // fields
  177. foreach ( $table->fields as $fieldName => $field )
  178. {
  179. $this->writeField( $file, $fieldName, $field, isset( $primaries[$fieldName] ) );
  180. }
  181. $this->closeFile( $file );
  182. }
  183. /**
  184. * Open a file for writing a PersistentObject definition to.
  185. * This method opens a file for writing a PersistentObject definition to
  186. * and writes the basic PHP open tag to it.
  187. *
  188. * @param string $dir The diretory to open the file in.
  189. * @param string $name The table name.
  190. * @return resource(file) The file resource used for writing.
  191. *
  192. * @throws ezcBaseFileIoException
  193. * if the file to write to already exists.
  194. * @throws ezcBaseFilePermissionException
  195. * if the file could not be opened for writing.
  196. */
  197. private function openFile( $dir, $name )
  198. {
  199. $filename = $dir . DIRECTORY_SEPARATOR . strtolower( $this->prefix ) . strtolower( $name ) . '.php';
  200. // We do not want to overwrite files
  201. if ( file_exists( $filename ) && ( $this->overwrite === false || is_writable( $filename ) === false ) )
  202. {
  203. throw new ezcBaseFileIoException( $filename, ezcBaseFileException::WRITE, "File already exists or is not writeable. Use --overwrite to ignore existance." );
  204. }
  205. $file = @fopen( $filename, 'w' );
  206. if ( $file === false )
  207. {
  208. throw new ezcBaseFilePermissionException( $file, ezcBaseFileException::WRITE );
  209. }
  210. fwrite( $file, "<?php\n" );
  211. fwrite( $file, "// Autogenerated PersistentObject definition\n" );
  212. fwrite( $file, "\n" );
  213. return $file;
  214. }
  215. /**
  216. * Close a file where a PersistentObject definition has been written to.
  217. * This method closes a file after writing a PersistentObject definition to
  218. * it and writes the PHP closing tag to it.
  219. *
  220. * @param resource(file) $file The file resource to close.
  221. * @return void
  222. */
  223. private function closeFile( $file )
  224. {
  225. fwrite( $file, "return \$def;\n" );
  226. fwrite( $file, "\n" );
  227. fwrite( $file, "?>\n" );
  228. fclose( $file );
  229. }
  230. /**
  231. * Extract primary keys from an index definition.
  232. * This method extracts the names of all primary keys from the index
  233. * defintions of a table.
  234. *
  235. * @param array(string=>ezcDbSchemaIndex) $indexes Indices.
  236. * @return array(string=>bool) The primary keys.
  237. */
  238. private function determinePrimaries( $indexes )
  239. {
  240. $primaries = array();
  241. foreach ( $indexes as $index )
  242. {
  243. if ( $index->primary )
  244. {
  245. foreach ( $index->indexFields as $field => $definiton )
  246. {
  247. $primaries[$field] = true;
  248. }
  249. }
  250. }
  251. return $primaries;
  252. }
  253. }
  254. ?>