Configuration.php 17KB

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