Configuration.php 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. <?php
  2. /*
  3. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  4. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  5. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  6. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  7. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  8. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  9. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  10. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  11. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  12. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  13. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14. *
  15. * This software consists of voluntary contributions made by many individuals
  16. * and is licensed under the LGPL. For more information, see
  17. * <http://www.doctrine-project.org>.
  18. */
  19. namespace Doctrine\ORM;
  20. use Doctrine\Common\Cache\Cache,
  21. Doctrine\Common\Cache\ArrayCache,
  22. Doctrine\Common\Annotations\AnnotationRegistry,
  23. Doctrine\Common\Annotations\AnnotationReader,
  24. Doctrine\Common\Annotations\SimpleAnnotationReader,
  25. Doctrine\ORM\Mapping\Driver\Driver,
  26. Doctrine\ORM\Mapping\Driver\AnnotationDriver;
  27. /**
  28. * Configuration container for all configuration options of Doctrine.
  29. * It combines all configuration options from DBAL & ORM.
  30. *
  31. * @since 2.0
  32. * @internal When adding a new configuration option just write a getter/setter pair.
  33. * @author Benjamin Eberlei <kontakt@beberlei.de>
  34. * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
  35. * @author Jonathan Wage <jonwage@gmail.com>
  36. * @author Roman Borschel <roman@code-factory.org>
  37. */
  38. class Configuration extends \Doctrine\DBAL\Configuration
  39. {
  40. /**
  41. * Sets the directory where Doctrine generates any necessary proxy class files.
  42. *
  43. * @param string $dir
  44. */
  45. public function setProxyDir($dir)
  46. {
  47. $this->_attributes['proxyDir'] = $dir;
  48. }
  49. /**
  50. * Gets the directory where Doctrine generates any necessary proxy class files.
  51. *
  52. * @return string
  53. */
  54. public function getProxyDir()
  55. {
  56. return isset($this->_attributes['proxyDir']) ?
  57. $this->_attributes['proxyDir'] : null;
  58. }
  59. /**
  60. * Gets a boolean flag that indicates whether proxy classes should always be regenerated
  61. * during each script execution.
  62. *
  63. * @return boolean
  64. */
  65. public function getAutoGenerateProxyClasses()
  66. {
  67. return isset($this->_attributes['autoGenerateProxyClasses']) ?
  68. $this->_attributes['autoGenerateProxyClasses'] : true;
  69. }
  70. /**
  71. * Sets a boolean flag that indicates whether proxy classes should always be regenerated
  72. * during each script execution.
  73. *
  74. * @param boolean $bool
  75. */
  76. public function setAutoGenerateProxyClasses($bool)
  77. {
  78. $this->_attributes['autoGenerateProxyClasses'] = $bool;
  79. }
  80. /**
  81. * Gets the namespace where proxy classes reside.
  82. *
  83. * @return string
  84. */
  85. public function getProxyNamespace()
  86. {
  87. return isset($this->_attributes['proxyNamespace']) ?
  88. $this->_attributes['proxyNamespace'] : null;
  89. }
  90. /**
  91. * Sets the namespace where proxy classes reside.
  92. *
  93. * @param string $ns
  94. */
  95. public function setProxyNamespace($ns)
  96. {
  97. $this->_attributes['proxyNamespace'] = $ns;
  98. }
  99. /**
  100. * Sets the cache driver implementation that is used for metadata caching.
  101. *
  102. * @param Driver $driverImpl
  103. * @todo Force parameter to be a Closure to ensure lazy evaluation
  104. * (as soon as a metadata cache is in effect, the driver never needs to initialize).
  105. */
  106. public function setMetadataDriverImpl(Driver $driverImpl)
  107. {
  108. $this->_attributes['metadataDriverImpl'] = $driverImpl;
  109. }
  110. /**
  111. * Add a new default annotation driver with a correctly configured annotation reader.
  112. *
  113. * @param array $paths
  114. * @return Mapping\Driver\AnnotationDriver
  115. */
  116. public function newDefaultAnnotationDriver($paths = array())
  117. {
  118. if (version_compare(\Doctrine\Common\Version::VERSION, '2.2.0-DEV', '>=')) {
  119. // Register the ORM Annotations in the AnnotationRegistry
  120. AnnotationRegistry::registerFile(__DIR__ . '/Mapping/Driver/DoctrineAnnotations.php');
  121. $reader = new SimpleAnnotationReader();
  122. $reader->addNamespace('Doctrine\ORM\Mapping');
  123. $reader = new \Doctrine\Common\Annotations\CachedReader($reader, new ArrayCache());
  124. } else if (version_compare(\Doctrine\Common\Version::VERSION, '2.1.0-DEV', '>=')) {
  125. // Register the ORM Annotations in the AnnotationRegistry
  126. AnnotationRegistry::registerFile(__DIR__ . '/Mapping/Driver/DoctrineAnnotations.php');
  127. $reader = new AnnotationReader();
  128. $reader->setDefaultAnnotationNamespace('Doctrine\ORM\Mapping\\');
  129. $reader->setIgnoreNotImportedAnnotations(true);
  130. $reader->setEnableParsePhpImports(false);
  131. $reader = new \Doctrine\Common\Annotations\CachedReader(
  132. new \Doctrine\Common\Annotations\IndexedReader($reader), new ArrayCache()
  133. );
  134. } else {
  135. $reader = new AnnotationReader();
  136. $reader->setDefaultAnnotationNamespace('Doctrine\ORM\Mapping\\');
  137. }
  138. return new AnnotationDriver($reader, (array)$paths);
  139. }
  140. /**
  141. * Adds a namespace under a certain alias.
  142. *
  143. * @param string $alias
  144. * @param string $namespace
  145. */
  146. public function addEntityNamespace($alias, $namespace)
  147. {
  148. $this->_attributes['entityNamespaces'][$alias] = $namespace;
  149. }
  150. /**
  151. * Resolves a registered namespace alias to the full namespace.
  152. *
  153. * @param string $entityNamespaceAlias
  154. * @return string
  155. * @throws MappingException
  156. */
  157. public function getEntityNamespace($entityNamespaceAlias)
  158. {
  159. if ( ! isset($this->_attributes['entityNamespaces'][$entityNamespaceAlias])) {
  160. throw ORMException::unknownEntityNamespace($entityNamespaceAlias);
  161. }
  162. return trim($this->_attributes['entityNamespaces'][$entityNamespaceAlias], '\\');
  163. }
  164. /**
  165. * Set the entity alias map
  166. *
  167. * @param array $entityAliasMap
  168. * @return void
  169. */
  170. public function setEntityNamespaces(array $entityNamespaces)
  171. {
  172. $this->_attributes['entityNamespaces'] = $entityNamespaces;
  173. }
  174. /**
  175. * Retrieves the list of registered entity namespace aliases.
  176. *
  177. * @return array
  178. */
  179. public function getEntityNamespaces()
  180. {
  181. return $this->_attributes['entityNamespaces'];
  182. }
  183. /**
  184. * Gets the cache driver implementation that is used for the mapping metadata.
  185. *
  186. * @throws ORMException
  187. * @return Mapping\Driver\Driver
  188. */
  189. public function getMetadataDriverImpl()
  190. {
  191. return isset($this->_attributes['metadataDriverImpl']) ?
  192. $this->_attributes['metadataDriverImpl'] : null;
  193. }
  194. /**
  195. * Gets the cache driver implementation that is used for query result caching.
  196. *
  197. * @return \Doctrine\Common\Cache\Cache
  198. */
  199. public function getResultCacheImpl()
  200. {
  201. return isset($this->_attributes['resultCacheImpl']) ?
  202. $this->_attributes['resultCacheImpl'] : null;
  203. }
  204. /**
  205. * Sets the cache driver implementation that is used for query result caching.
  206. *
  207. * @param \Doctrine\Common\Cache\Cache $cacheImpl
  208. */
  209. public function setResultCacheImpl(Cache $cacheImpl)
  210. {
  211. $this->_attributes['resultCacheImpl'] = $cacheImpl;
  212. }
  213. /**
  214. * Gets the cache driver implementation that is used for the query cache (SQL cache).
  215. *
  216. * @return \Doctrine\Common\Cache\Cache
  217. */
  218. public function getQueryCacheImpl()
  219. {
  220. return isset($this->_attributes['queryCacheImpl']) ?
  221. $this->_attributes['queryCacheImpl'] : null;
  222. }
  223. /**
  224. * Sets the cache driver implementation that is used for the query cache (SQL cache).
  225. *
  226. * @param \Doctrine\Common\Cache\Cache $cacheImpl
  227. */
  228. public function setQueryCacheImpl(Cache $cacheImpl)
  229. {
  230. $this->_attributes['queryCacheImpl'] = $cacheImpl;
  231. }
  232. /**
  233. * Gets the cache driver implementation that is used for metadata caching.
  234. *
  235. * @return \Doctrine\Common\Cache\Cache
  236. */
  237. public function getMetadataCacheImpl()
  238. {
  239. return isset($this->_attributes['metadataCacheImpl']) ?
  240. $this->_attributes['metadataCacheImpl'] : null;
  241. }
  242. /**
  243. * Sets the cache driver implementation that is used for metadata caching.
  244. *
  245. * @param \Doctrine\Common\Cache\Cache $cacheImpl
  246. */
  247. public function setMetadataCacheImpl(Cache $cacheImpl)
  248. {
  249. $this->_attributes['metadataCacheImpl'] = $cacheImpl;
  250. }
  251. /**
  252. * Adds a named DQL query to the configuration.
  253. *
  254. * @param string $name The name of the query.
  255. * @param string $dql The DQL query string.
  256. */
  257. public function addNamedQuery($name, $dql)
  258. {
  259. $this->_attributes['namedQueries'][$name] = $dql;
  260. }
  261. /**
  262. * Gets a previously registered named DQL query.
  263. *
  264. * @param string $name The name of the query.
  265. * @return string The DQL query.
  266. */
  267. public function getNamedQuery($name)
  268. {
  269. if ( ! isset($this->_attributes['namedQueries'][$name])) {
  270. throw ORMException::namedQueryNotFound($name);
  271. }
  272. return $this->_attributes['namedQueries'][$name];
  273. }
  274. /**
  275. * Adds a named native query to the configuration.
  276. *
  277. * @param string $name The name of the query.
  278. * @param string $sql The native SQL query string.
  279. * @param ResultSetMapping $rsm The ResultSetMapping used for the results of the SQL query.
  280. */
  281. public function addNamedNativeQuery($name, $sql, Query\ResultSetMapping $rsm)
  282. {
  283. $this->_attributes['namedNativeQueries'][$name] = array($sql, $rsm);
  284. }
  285. /**
  286. * Gets the components of a previously registered named native query.
  287. *
  288. * @param string $name The name of the query.
  289. * @return array A tuple with the first element being the SQL string and the second
  290. * element being the ResultSetMapping.
  291. */
  292. public function getNamedNativeQuery($name)
  293. {
  294. if ( ! isset($this->_attributes['namedNativeQueries'][$name])) {
  295. throw ORMException::namedNativeQueryNotFound($name);
  296. }
  297. return $this->_attributes['namedNativeQueries'][$name];
  298. }
  299. /**
  300. * Ensures that this Configuration instance contains settings that are
  301. * suitable for a production environment.
  302. *
  303. * @throws ORMException If a configuration setting has a value that is not
  304. * suitable for a production environment.
  305. */
  306. public function ensureProductionSettings()
  307. {
  308. if ( !$this->getQueryCacheImpl()) {
  309. throw ORMException::queryCacheNotConfigured();
  310. }
  311. if ( !$this->getMetadataCacheImpl()) {
  312. throw ORMException::metadataCacheNotConfigured();
  313. }
  314. if ($this->getAutoGenerateProxyClasses()) {
  315. throw ORMException::proxyClassesAlwaysRegenerating();
  316. }
  317. }
  318. /**
  319. * Registers a custom DQL function that produces a string value.
  320. * Such a function can then be used in any DQL statement in any place where string
  321. * functions are allowed.
  322. *
  323. * DQL function names are case-insensitive.
  324. *
  325. * @param string $name
  326. * @param string $className
  327. */
  328. public function addCustomStringFunction($name, $className)
  329. {
  330. $this->_attributes['customStringFunctions'][strtolower($name)] = $className;
  331. }
  332. /**
  333. * Gets the implementation class name of a registered custom string DQL function.
  334. *
  335. * @param string $name
  336. * @return string
  337. */
  338. public function getCustomStringFunction($name)
  339. {
  340. $name = strtolower($name);
  341. return isset($this->_attributes['customStringFunctions'][$name]) ?
  342. $this->_attributes['customStringFunctions'][$name] : null;
  343. }
  344. /**
  345. * Sets a map of custom DQL string functions.
  346. *
  347. * Keys must be function names and values the FQCN of the implementing class.
  348. * The function names will be case-insensitive in DQL.
  349. *
  350. * Any previously added string functions are discarded.
  351. *
  352. * @param array $functions The map of custom DQL string functions.
  353. */
  354. public function setCustomStringFunctions(array $functions)
  355. {
  356. $this->_attributes['customStringFunctions'] = array_change_key_case($functions);
  357. }
  358. /**
  359. * Registers a custom DQL function that produces a numeric value.
  360. * Such a function can then be used in any DQL statement in any place where numeric
  361. * functions are allowed.
  362. *
  363. * DQL function names are case-insensitive.
  364. *
  365. * @param string $name
  366. * @param string $className
  367. */
  368. public function addCustomNumericFunction($name, $className)
  369. {
  370. $this->_attributes['customNumericFunctions'][strtolower($name)] = $className;
  371. }
  372. /**
  373. * Gets the implementation class name of a registered custom numeric DQL function.
  374. *
  375. * @param string $name
  376. * @return string
  377. */
  378. public function getCustomNumericFunction($name)
  379. {
  380. $name = strtolower($name);
  381. return isset($this->_attributes['customNumericFunctions'][$name]) ?
  382. $this->_attributes['customNumericFunctions'][$name] : null;
  383. }
  384. /**
  385. * Sets a map of custom DQL numeric functions.
  386. *
  387. * Keys must be function names and values the FQCN of the implementing class.
  388. * The function names will be case-insensitive in DQL.
  389. *
  390. * Any previously added numeric functions are discarded.
  391. *
  392. * @param array $functions The map of custom DQL numeric functions.
  393. */
  394. public function setCustomNumericFunctions(array $functions)
  395. {
  396. $this->_attributes['customNumericFunctions'] = array_change_key_case($functions);
  397. }
  398. /**
  399. * Registers a custom DQL function that produces a date/time value.
  400. * Such a function can then be used in any DQL statement in any place where date/time
  401. * functions are allowed.
  402. *
  403. * DQL function names are case-insensitive.
  404. *
  405. * @param string $name
  406. * @param string $className
  407. */
  408. public function addCustomDatetimeFunction($name, $className)
  409. {
  410. $this->_attributes['customDatetimeFunctions'][strtolower($name)] = $className;
  411. }
  412. /**
  413. * Gets the implementation class name of a registered custom date/time DQL function.
  414. *
  415. * @param string $name
  416. * @return string
  417. */
  418. public function getCustomDatetimeFunction($name)
  419. {
  420. $name = strtolower($name);
  421. return isset($this->_attributes['customDatetimeFunctions'][$name]) ?
  422. $this->_attributes['customDatetimeFunctions'][$name] : null;
  423. }
  424. /**
  425. * Sets a map of custom DQL date/time functions.
  426. *
  427. * Keys must be function names and values the FQCN of the implementing class.
  428. * The function names will be case-insensitive in DQL.
  429. *
  430. * Any previously added date/time functions are discarded.
  431. *
  432. * @param array $functions The map of custom DQL date/time functions.
  433. */
  434. public function setCustomDatetimeFunctions(array $functions)
  435. {
  436. $this->_attributes['customDatetimeFunctions'] = array_change_key_case($functions);
  437. }
  438. /**
  439. * Get the hydrator class for the given hydration mode name.
  440. *
  441. * @param string $modeName The hydration mode name.
  442. * @return string $hydrator The hydrator class name.
  443. */
  444. public function getCustomHydrationMode($modeName)
  445. {
  446. return isset($this->_attributes['customHydrationModes'][$modeName]) ?
  447. $this->_attributes['customHydrationModes'][$modeName] : null;
  448. }
  449. /**
  450. * Add a custom hydration mode.
  451. *
  452. * @param string $modeName The hydration mode name.
  453. * @param string $hydrator The hydrator class name.
  454. */
  455. public function addCustomHydrationMode($modeName, $hydrator)
  456. {
  457. $this->_attributes['customHydrationModes'][$modeName] = $hydrator;
  458. }
  459. /**
  460. * Set a class metadata factory.
  461. *
  462. * @param string $cmf
  463. */
  464. public function setClassMetadataFactoryName($cmfName)
  465. {
  466. $this->_attributes['classMetadataFactoryName'] = $cmfName;
  467. }
  468. /**
  469. * @return string
  470. */
  471. public function getClassMetadataFactoryName()
  472. {
  473. if (!isset($this->_attributes['classMetadataFactoryName'])) {
  474. $this->_attributes['classMetadataFactoryName'] = 'Doctrine\ORM\Mapping\ClassMetadataFactory';
  475. }
  476. return $this->_attributes['classMetadataFactoryName'];
  477. }
  478. }