Configuration.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  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, '2.2.0-DEV', '>=')) {
  118. // Register the ORM Annotations in the AnnotationRegistry
  119. AnnotationRegistry::registerFile(__DIR__ . '/Mapping/Driver/DoctrineAnnotations.php');
  120. $reader = new \Doctrine\Common\Annotations\SimpleAnnotationReader();
  121. $reader->addNamespace('Doctrine\ORM\Mapping');
  122. $reader = new \Doctrine\Common\Annotations\CachedReader($reader, new ArrayCache());
  123. } else if (version_compare(\Doctrine\Common\Version::VERSION, '2.1.0-DEV', '>=')) {
  124. // Register the ORM Annotations in the AnnotationRegistry
  125. AnnotationRegistry::registerFile(__DIR__ . '/Mapping/Driver/DoctrineAnnotations.php');
  126. $reader = new AnnotationReader();
  127. $reader->setDefaultAnnotationNamespace('Doctrine\ORM\Mapping\\');
  128. $reader->setIgnoreNotImportedAnnotations(true);
  129. $reader->setEnableParsePhpImports(false);
  130. $reader = new \Doctrine\Common\Annotations\CachedReader(
  131. new \Doctrine\Common\Annotations\IndexedReader($reader), new ArrayCache()
  132. );
  133. } else {
  134. $reader = new AnnotationReader();
  135. $reader->setDefaultAnnotationNamespace('Doctrine\ORM\Mapping\\');
  136. }
  137. return new AnnotationDriver($reader, (array)$paths);
  138. }
  139. /**
  140. * Adds a namespace under a certain alias.
  141. *
  142. * @param string $alias
  143. * @param string $namespace
  144. */
  145. public function addEntityNamespace($alias, $namespace)
  146. {
  147. $this->_attributes['entityNamespaces'][$alias] = $namespace;
  148. }
  149. /**
  150. * Resolves a registered namespace alias to the full namespace.
  151. *
  152. * @param string $entityNamespaceAlias
  153. * @return string
  154. * @throws MappingException
  155. */
  156. public function getEntityNamespace($entityNamespaceAlias)
  157. {
  158. if ( ! isset($this->_attributes['entityNamespaces'][$entityNamespaceAlias])) {
  159. throw ORMException::unknownEntityNamespace($entityNamespaceAlias);
  160. }
  161. return trim($this->_attributes['entityNamespaces'][$entityNamespaceAlias], '\\');
  162. }
  163. /**
  164. * Set the entity alias map
  165. *
  166. * @param array $entityAliasMap
  167. * @return void
  168. */
  169. public function setEntityNamespaces(array $entityNamespaces)
  170. {
  171. $this->_attributes['entityNamespaces'] = $entityNamespaces;
  172. }
  173. /**
  174. * Retrieves the list of registered entity namespace aliases.
  175. *
  176. * @return array
  177. */
  178. public function getEntityNamespaces()
  179. {
  180. return $this->_attributes['entityNamespaces'];
  181. }
  182. /**
  183. * Gets the cache driver implementation that is used for the mapping metadata.
  184. *
  185. * @throws ORMException
  186. * @return Mapping\Driver\Driver
  187. */
  188. public function getMetadataDriverImpl()
  189. {
  190. return isset($this->_attributes['metadataDriverImpl']) ?
  191. $this->_attributes['metadataDriverImpl'] : null;
  192. }
  193. /**
  194. * Gets the cache driver implementation that is used for the query cache (SQL cache).
  195. *
  196. * @return \Doctrine\Common\Cache\Cache
  197. */
  198. public function getQueryCacheImpl()
  199. {
  200. return isset($this->_attributes['queryCacheImpl']) ?
  201. $this->_attributes['queryCacheImpl'] : null;
  202. }
  203. /**
  204. * Sets the cache driver implementation that is used for the query cache (SQL cache).
  205. *
  206. * @param \Doctrine\Common\Cache\Cache $cacheImpl
  207. */
  208. public function setQueryCacheImpl(Cache $cacheImpl)
  209. {
  210. $this->_attributes['queryCacheImpl'] = $cacheImpl;
  211. }
  212. /**
  213. * Gets the cache driver implementation that is used for the hydration cache (SQL cache).
  214. *
  215. * @return \Doctrine\Common\Cache\Cache
  216. */
  217. public function getHydrationCacheImpl()
  218. {
  219. return isset($this->_attributes['hydrationCacheImpl'])
  220. ? $this->_attributes['hydrationCacheImpl']
  221. : null;
  222. }
  223. /**
  224. * Sets the cache driver implementation that is used for the hydration cache (SQL cache).
  225. *
  226. * @param \Doctrine\Common\Cache\Cache $cacheImpl
  227. */
  228. public function setHydrationCacheImpl(Cache $cacheImpl)
  229. {
  230. $this->_attributes['hydrationCacheImpl'] = $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. /**
  479. * Add a filter to the list of possible filters.
  480. *
  481. * @param string $name The name of the filter.
  482. * @param string $className The class name of the filter.
  483. */
  484. public function addFilter($name, $className)
  485. {
  486. $this->_attributes['filters'][$name] = $className;
  487. }
  488. /**
  489. * Gets the class name for a given filter name.
  490. *
  491. * @param string $name The name of the filter.
  492. *
  493. * @return string The class name of the filter, or null of it is not
  494. * defined.
  495. */
  496. public function getFilterClassName($name)
  497. {
  498. return isset($this->_attributes['filters'][$name]) ?
  499. $this->_attributes['filters'][$name] : null;
  500. }
  501. /**
  502. * Set default repository class.
  503. *
  504. * @since 2.2
  505. * @param string $className
  506. * @throws ORMException If not is a \Doctrine\ORM\EntityRepository
  507. */
  508. public function setDefaultRepositoryClassName($className)
  509. {
  510. if ($className != "Doctrine\ORM\EntityRepository" &&
  511. !is_subclass_of($className, 'Doctrine\ORM\EntityRepository')){
  512. throw ORMException::invalidEntityRepository($className);
  513. }
  514. $this->_attributes['defaultRepositoryClassName'] = $className;
  515. }
  516. /**
  517. * Get default repository class.
  518. *
  519. * @since 2.2
  520. * @return string
  521. */
  522. public function getDefaultRepositoryClassName()
  523. {
  524. return isset($this->_attributes['defaultRepositoryClassName']) ?
  525. $this->_attributes['defaultRepositoryClassName'] : 'Doctrine\ORM\EntityRepository';
  526. }
  527. }