/DatabaseSchema/src/handler_manager.php

https://github.com/F5/zetacomponents · PHP · 289 lines · 122 code · 18 blank · 149 comment · 12 complexity · 1a4ef72aacc9f9616e8f5318f1717f12 MD5 · raw file

  1. <?php
  2. /**
  3. * File containing the ezcDbSchemaHandlerManager 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. * Deals with schema handlers for a ezcDbSchema object.
  28. *
  29. * Determines which handlers to use for the specified storage type.
  30. *
  31. * @package DatabaseSchema
  32. * @version //autogentag//
  33. * @mainclass
  34. */
  35. class ezcDbSchemaHandlerManager
  36. {
  37. /**
  38. * Set of standard read handlers.
  39. *
  40. * The property is an array where the key is the name of the format and the
  41. * value the classname that implements the read handler.
  42. *
  43. * @var array(string=>string)
  44. */
  45. static public $readHandlers = array(
  46. 'array' => 'ezcDbSchemaPhpArrayReader',
  47. 'mysql' => 'ezcDbSchemaMysqlReader',
  48. 'oracle' => 'ezcDbSchemaOracleReader',
  49. 'pgsql' => 'ezcDbSchemaPgsqlReader',
  50. 'sqlite' => 'ezcDbSchemaSqliteReader',
  51. 'xml' => 'ezcDbSchemaXmlReader',
  52. );
  53. /**
  54. * Set of standard write handlers.
  55. *
  56. * The property is an array where the key is the name of the format and the
  57. * value the classname that implements the write handler.
  58. *
  59. * @var array(string=>string)
  60. */
  61. static public $writeHandlers = array(
  62. 'array' => 'ezcDbSchemaPhpArrayWriter',
  63. 'mysql' => 'ezcDbSchemaMysqlWriter',
  64. 'oracle' => 'ezcDbSchemaOracleWriter',
  65. 'pgsql' => 'ezcDbSchemaPgsqlWriter',
  66. 'sqlite' => 'ezcDbSchemaSqliteWriter',
  67. 'xml' => 'ezcDbSchemaXmlWriter',
  68. 'persistent' => 'ezcDbSchemaPersistentWriter',
  69. );
  70. /**
  71. * Set of standard difference read handlers.
  72. *
  73. * The property is an array where the key is the name of the format and the
  74. * value the classname that implements the read handler.
  75. *
  76. * @var array(string=>string)
  77. */
  78. static public $diffReadHandlers = array(
  79. 'array' => 'ezcDbSchemaPhpArrayReader',
  80. 'xml' => 'ezcDbSchemaXmlReader',
  81. );
  82. /**
  83. * Set of standard difference write handlers.
  84. *
  85. * The property is an array where the key is the name of the format and the
  86. * value the classname that implements the write handler.
  87. *
  88. * @var array(string=>string)
  89. */
  90. static public $diffWriteHandlers = array(
  91. 'array' => 'ezcDbSchemaPhpArrayWriter',
  92. 'mysql' => 'ezcDbSchemaMysqlWriter',
  93. 'oracle' => 'ezcDbSchemaOracleWriter',
  94. 'pgsql' => 'ezcDbSchemaPgsqlWriter',
  95. 'sqlite' => 'ezcDbSchemaSqliteWriter',
  96. 'xml' => 'ezcDbSchemaXmlWriter',
  97. );
  98. /**
  99. * Returns the class name of the read handler for format $format.
  100. *
  101. * @param string $format
  102. * @return string
  103. */
  104. static public function getReaderByFormat( $format )
  105. {
  106. if ( !isset( self::$readHandlers[$format] ) )
  107. {
  108. throw new ezcDbSchemaUnknownFormatException( $format, 'read' );
  109. }
  110. return self::$readHandlers[$format];
  111. }
  112. /**
  113. * Returns the class name of the write handler for format $format.
  114. *
  115. * @param string $format
  116. * @return string
  117. */
  118. static public function getWriterByFormat( $format )
  119. {
  120. if ( !isset( self::$writeHandlers[$format] ) )
  121. {
  122. throw new ezcDbSchemaUnknownFormatException( $format, 'write' );
  123. }
  124. return self::$writeHandlers[$format];
  125. }
  126. /**
  127. * Returns the class name of the differences read handler for format $format.
  128. *
  129. * @param string $format
  130. * @return string
  131. */
  132. static public function getDiffReaderByFormat( $format )
  133. {
  134. if ( !isset( self::$diffReadHandlers[$format] ) )
  135. {
  136. throw new ezcDbSchemaUnknownFormatException( $format, 'difference read' );
  137. }
  138. return self::$diffReadHandlers[$format];
  139. }
  140. /**
  141. * Returns the class name of the differences write handler for format $format.
  142. *
  143. * @param string $format
  144. * @return string
  145. */
  146. static public function getDiffWriterByFormat( $format )
  147. {
  148. if ( !isset( self::$diffWriteHandlers[$format] ) )
  149. {
  150. throw new ezcDbSchemaUnknownFormatException( $format, 'difference write' );
  151. }
  152. return self::$diffWriteHandlers[$format];
  153. }
  154. /**
  155. * Returns list of schema types supported by all known handlers.
  156. *
  157. * Goes through the list of known handlers and gathers information of which
  158. * schema types do they support.
  159. *
  160. * @return array
  161. */
  162. static public function getSupportedFormats()
  163. {
  164. return array_keys( array_merge ( self::$readHandlers, self::$writeHandlers ) );
  165. }
  166. /**
  167. * Returns list of schema types supported by all known difference handlers.
  168. *
  169. * Goes through the list of known difference handlers and gathers
  170. * information of which schema types do they support.
  171. *
  172. * @return array
  173. */
  174. static public function getSupportedDiffFormats()
  175. {
  176. return array_keys( array_merge( self::$diffReadHandlers, self::$diffWriteHandlers ) );
  177. }
  178. /**
  179. * Adds the read handler class $readerClass to the manager for $type
  180. *
  181. * @throws ezcDbSchemaInvalidReaderClassException if the $readerClass
  182. * doesn't exist or doesn't extend the abstract class
  183. * ezcDbSchemaReader.
  184. * @param string $type
  185. * @param string $readerClass
  186. */
  187. static public function addReader( $type, $readerClass )
  188. {
  189. // Check if the passed classname actually exists
  190. if ( !ezcBaseFeatures::classExists( $readerClass, true ) )
  191. {
  192. throw new ezcDbSchemaInvalidReaderClassException( $readerClass );
  193. }
  194. // Check if the passed classname actually implements the interface.
  195. if ( !in_array( 'ezcDbSchemaReader', class_implements( $readerClass ) ) )
  196. {
  197. throw new ezcDbSchemaInvalidReaderClassException( $readerClass );
  198. }
  199. self::$readHandlers[$type] = $readerClass;
  200. }
  201. /**
  202. * Adds the write handler class $writerClass to the manager for $type
  203. *
  204. * @throws ezcDbSchemaInvalidWriterClassException if the $writerClass
  205. * doesn't exist or doesn't extend the abstract class
  206. * ezcDbSchemaWriter.
  207. * @param string $type
  208. * @param string $writerClass
  209. */
  210. static public function addWriter( $type, $writerClass )
  211. {
  212. // Check if the passed classname actually exists
  213. if ( !ezcBaseFeatures::classExists( $writerClass, true ) )
  214. {
  215. throw new ezcDbSchemaInvalidWriterClassException( $writerClass );
  216. }
  217. // Check if the passed classname actually implements the interface.
  218. if ( !in_array( 'ezcDbSchemaWriter', class_implements( $writerClass ) ) )
  219. {
  220. throw new ezcDbSchemaInvalidWriterClassException( $writerClass );
  221. }
  222. self::$writeHandlers[$type] = $writerClass;
  223. }
  224. /**
  225. * Adds the difference read handler class $readerClass to the manager for $type
  226. *
  227. * @throws ezcDbSchemaInvalidReaderClassException if the $readerClass
  228. * doesn't exist or doesn't extend the abstract class
  229. * ezcDbSchemaDiffReader.
  230. * @param string $type
  231. * @param string $readerClass
  232. */
  233. static public function addDiffReader( $type, $readerClass )
  234. {
  235. // Check if the passed classname actually exists
  236. if ( !ezcBaseFeatures::classExists( $readerClass, true ) )
  237. {
  238. throw new ezcDbSchemaInvalidDiffReaderClassException( $readerClass );
  239. }
  240. // Check if the passed classname actually implements the interface.
  241. if ( !in_array( 'ezcDbSchemaDiffReader', class_implements( $readerClass ) ) )
  242. {
  243. throw new ezcDbSchemaInvalidDiffReaderClassException( $readerClass );
  244. }
  245. self::$diffReadHandlers[$type] = $readerClass;
  246. }
  247. /**
  248. * Adds the difference write handler class $writerClass to the manager for $type
  249. *
  250. * @throws ezcDbSchemaInvalidWriterClassException if the $writerClass
  251. * doesn't exist or doesn't extend the abstract class
  252. * ezcDbSchemaDiffWriter.
  253. * @param string $type
  254. * @param string $writerClass
  255. */
  256. static public function addDiffWriter( $type, $writerClass )
  257. {
  258. // Check if the passed classname actually exists
  259. if ( !ezcBaseFeatures::classExists( $writerClass, true ) )
  260. {
  261. throw new ezcDbSchemaInvalidDiffWriterClassException( $writerClass );
  262. }
  263. // Check if the passed classname actually implements the interface.
  264. if ( !in_array( 'ezcDbSchemaDiffWriter', class_implements( $writerClass ) ) )
  265. {
  266. throw new ezcDbSchemaInvalidDiffWriterClassException( $writerClass );
  267. }
  268. self::$diffWriteHandlers[$type] = $writerClass;
  269. }
  270. }
  271. ?>