Configuration.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866
  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 MIT license. For more information, see
  17. * <http://www.doctrine-project.org>.
  18. */
  19. namespace Doctrine\ORM;
  20. use Doctrine\Common\Annotations\AnnotationReader;
  21. use Doctrine\Common\Annotations\AnnotationRegistry;
  22. use Doctrine\Common\Annotations\CachedReader;
  23. use Doctrine\Common\Annotations\SimpleAnnotationReader;
  24. use Doctrine\Common\Cache\ArrayCache;
  25. use Doctrine\Common\Cache\Cache as CacheDriver;
  26. use Doctrine\Common\Proxy\AbstractProxyFactory;
  27. use Doctrine\ORM\Cache\CacheConfiguration;
  28. use Doctrine\ORM\Mapping\ClassMetadataFactory;
  29. use Doctrine\ORM\Mapping\DefaultEntityListenerResolver;
  30. use Doctrine\ORM\Mapping\DefaultNamingStrategy;
  31. use Doctrine\ORM\Mapping\DefaultQuoteStrategy;
  32. use Doctrine\ORM\Mapping\Driver\AnnotationDriver;
  33. use Doctrine\ORM\Mapping\EntityListenerResolver;
  34. use Doctrine\ORM\Mapping\NamingStrategy;
  35. use Doctrine\ORM\Mapping\QuoteStrategy;
  36. use Doctrine\ORM\Query\ResultSetMapping;
  37. use Doctrine\ORM\Repository\DefaultRepositoryFactory;
  38. use Doctrine\ORM\Repository\RepositoryFactory;
  39. use Doctrine\Persistence\Mapping\Driver\MappingDriver;
  40. use Doctrine\Persistence\ObjectRepository;
  41. use ReflectionClass;
  42. use function strtolower;
  43. use function trim;
  44. /**
  45. * Configuration container for all configuration options of Doctrine.
  46. * It combines all configuration options from DBAL & ORM.
  47. *
  48. * Internal note: When adding a new configuration option just write a getter/setter pair.
  49. */
  50. class Configuration extends \Doctrine\DBAL\Configuration
  51. {
  52. /**
  53. * Sets the directory where Doctrine generates any necessary proxy class files.
  54. *
  55. * @param string $dir
  56. *
  57. * @return void
  58. */
  59. public function setProxyDir($dir)
  60. {
  61. $this->_attributes['proxyDir'] = $dir;
  62. }
  63. /**
  64. * Gets the directory where Doctrine generates any necessary proxy class files.
  65. *
  66. * @deprecated 2.7 We're switch to `ocramius/proxy-manager` and this method isn't applicable any longer
  67. *
  68. * @see https://github.com/Ocramius/ProxyManager
  69. *
  70. * @return string|null
  71. */
  72. public function getProxyDir()
  73. {
  74. return $this->_attributes['proxyDir'] ?? null;
  75. }
  76. /**
  77. * Gets the strategy for automatically generating proxy classes.
  78. *
  79. * @deprecated 2.7 We're switch to `ocramius/proxy-manager` and this method isn't applicable any longer
  80. *
  81. * @see https://github.com/Ocramius/ProxyManager
  82. *
  83. * @return int Possible values are constants of Doctrine\Common\Proxy\AbstractProxyFactory.
  84. */
  85. public function getAutoGenerateProxyClasses()
  86. {
  87. return $this->_attributes['autoGenerateProxyClasses'] ?? AbstractProxyFactory::AUTOGENERATE_ALWAYS;
  88. }
  89. /**
  90. * Sets the strategy for automatically generating proxy classes.
  91. *
  92. * @param bool|int $autoGenerate Possible values are constants of Doctrine\Common\Proxy\AbstractProxyFactory.
  93. * True is converted to AUTOGENERATE_ALWAYS, false to AUTOGENERATE_NEVER.
  94. *
  95. * @return void
  96. */
  97. public function setAutoGenerateProxyClasses($autoGenerate)
  98. {
  99. $this->_attributes['autoGenerateProxyClasses'] = (int) $autoGenerate;
  100. }
  101. /**
  102. * Gets the namespace where proxy classes reside.
  103. *
  104. * @deprecated 2.7 We're switch to `ocramius/proxy-manager` and this method isn't applicable any longer
  105. *
  106. * @see https://github.com/Ocramius/ProxyManager
  107. *
  108. * @return string|null
  109. */
  110. public function getProxyNamespace()
  111. {
  112. return $this->_attributes['proxyNamespace'] ?? null;
  113. }
  114. /**
  115. * Sets the namespace where proxy classes reside.
  116. *
  117. * @param string $ns
  118. *
  119. * @return void
  120. */
  121. public function setProxyNamespace($ns)
  122. {
  123. $this->_attributes['proxyNamespace'] = $ns;
  124. }
  125. /**
  126. * Sets the cache driver implementation that is used for metadata caching.
  127. *
  128. * @return void
  129. *
  130. * @todo Force parameter to be a Closure to ensure lazy evaluation
  131. * (as soon as a metadata cache is in effect, the driver never needs to initialize).
  132. */
  133. public function setMetadataDriverImpl(MappingDriver $driverImpl)
  134. {
  135. $this->_attributes['metadataDriverImpl'] = $driverImpl;
  136. }
  137. /**
  138. * Adds a new default annotation driver with a correctly configured annotation reader. If $useSimpleAnnotationReader
  139. * is true, the notation `@Entity` will work, otherwise, the notation `@ORM\Entity` will be supported.
  140. *
  141. * @param bool $useSimpleAnnotationReader
  142. *
  143. * @return AnnotationDriver
  144. *
  145. * @psalm-param string|list<string> $paths
  146. */
  147. public function newDefaultAnnotationDriver($paths = [], $useSimpleAnnotationReader = true)
  148. {
  149. AnnotationRegistry::registerFile(__DIR__ . '/Mapping/Driver/DoctrineAnnotations.php');
  150. if ($useSimpleAnnotationReader) {
  151. // Register the ORM Annotations in the AnnotationRegistry
  152. $reader = new SimpleAnnotationReader();
  153. $reader->addNamespace('Doctrine\ORM\Mapping');
  154. $cachedReader = new CachedReader($reader, new ArrayCache());
  155. return new AnnotationDriver($cachedReader, (array) $paths);
  156. }
  157. return new AnnotationDriver(
  158. new CachedReader(new AnnotationReader(), new ArrayCache()),
  159. (array) $paths
  160. );
  161. }
  162. /**
  163. * Adds a namespace under a certain alias.
  164. *
  165. * @param string $alias
  166. * @param string $namespace
  167. *
  168. * @return void
  169. */
  170. public function addEntityNamespace($alias, $namespace)
  171. {
  172. $this->_attributes['entityNamespaces'][$alias] = $namespace;
  173. }
  174. /**
  175. * Resolves a registered namespace alias to the full namespace.
  176. *
  177. * @param string $entityNamespaceAlias
  178. *
  179. * @return string
  180. *
  181. * @throws ORMException
  182. */
  183. public function getEntityNamespace($entityNamespaceAlias)
  184. {
  185. if (! isset($this->_attributes['entityNamespaces'][$entityNamespaceAlias])) {
  186. throw ORMException::unknownEntityNamespace($entityNamespaceAlias);
  187. }
  188. return trim($this->_attributes['entityNamespaces'][$entityNamespaceAlias], '\\');
  189. }
  190. /**
  191. * Sets the entity alias map.
  192. *
  193. * @return void
  194. *
  195. * @psalm-param array<string, string> $entityNamespaces
  196. */
  197. public function setEntityNamespaces(array $entityNamespaces)
  198. {
  199. $this->_attributes['entityNamespaces'] = $entityNamespaces;
  200. }
  201. /**
  202. * Retrieves the list of registered entity namespace aliases.
  203. *
  204. * @psalm-return array<string, string>
  205. */
  206. public function getEntityNamespaces()
  207. {
  208. return $this->_attributes['entityNamespaces'];
  209. }
  210. /**
  211. * Gets the cache driver implementation that is used for the mapping metadata.
  212. *
  213. * @return MappingDriver|null
  214. *
  215. * @throws ORMException
  216. */
  217. public function getMetadataDriverImpl()
  218. {
  219. return $this->_attributes['metadataDriverImpl'] ?? null;
  220. }
  221. /**
  222. * Gets the cache driver implementation that is used for the query cache (SQL cache).
  223. *
  224. * @return CacheDriver|null
  225. */
  226. public function getQueryCacheImpl()
  227. {
  228. return $this->_attributes['queryCacheImpl'] ?? null;
  229. }
  230. /**
  231. * Sets the cache driver implementation that is used for the query cache (SQL cache).
  232. *
  233. * @return void
  234. */
  235. public function setQueryCacheImpl(CacheDriver $cacheImpl)
  236. {
  237. $this->_attributes['queryCacheImpl'] = $cacheImpl;
  238. }
  239. /**
  240. * Gets the cache driver implementation that is used for the hydration cache (SQL cache).
  241. *
  242. * @return CacheDriver|null
  243. */
  244. public function getHydrationCacheImpl()
  245. {
  246. return $this->_attributes['hydrationCacheImpl'] ?? null;
  247. }
  248. /**
  249. * Sets the cache driver implementation that is used for the hydration cache (SQL cache).
  250. *
  251. * @return void
  252. */
  253. public function setHydrationCacheImpl(CacheDriver $cacheImpl)
  254. {
  255. $this->_attributes['hydrationCacheImpl'] = $cacheImpl;
  256. }
  257. /**
  258. * Gets the cache driver implementation that is used for metadata caching.
  259. *
  260. * @return CacheDriver|null
  261. */
  262. public function getMetadataCacheImpl()
  263. {
  264. return $this->_attributes['metadataCacheImpl'] ?? null;
  265. }
  266. /**
  267. * Sets the cache driver implementation that is used for metadata caching.
  268. *
  269. * @return void
  270. */
  271. public function setMetadataCacheImpl(CacheDriver $cacheImpl)
  272. {
  273. $this->_attributes['metadataCacheImpl'] = $cacheImpl;
  274. }
  275. /**
  276. * Adds a named DQL query to the configuration.
  277. *
  278. * @param string $name The name of the query.
  279. * @param string $dql The DQL query string.
  280. *
  281. * @return void
  282. */
  283. public function addNamedQuery($name, $dql)
  284. {
  285. $this->_attributes['namedQueries'][$name] = $dql;
  286. }
  287. /**
  288. * Gets a previously registered named DQL query.
  289. *
  290. * @param string $name The name of the query.
  291. *
  292. * @return string The DQL query.
  293. *
  294. * @throws ORMException
  295. */
  296. public function getNamedQuery($name)
  297. {
  298. if (! isset($this->_attributes['namedQueries'][$name])) {
  299. throw ORMException::namedQueryNotFound($name);
  300. }
  301. return $this->_attributes['namedQueries'][$name];
  302. }
  303. /**
  304. * Adds a named native query to the configuration.
  305. *
  306. * @param string $name The name of the query.
  307. * @param string $sql The native SQL query string.
  308. * @param Query\ResultSetMapping $rsm The ResultSetMapping used for the results of the SQL query.
  309. *
  310. * @return void
  311. */
  312. public function addNamedNativeQuery($name, $sql, Query\ResultSetMapping $rsm)
  313. {
  314. $this->_attributes['namedNativeQueries'][$name] = [$sql, $rsm];
  315. }
  316. /**
  317. * Gets the components of a previously registered named native query.
  318. *
  319. * @param string $name The name of the query.
  320. *
  321. * @throws ORMException
  322. *
  323. * @psalm-return array{string, ResultSetMapping} A tuple with the first
  324. * element being the SQL
  325. * string and the second
  326. * element being the
  327. * ResultSetMapping.
  328. */
  329. public function getNamedNativeQuery($name)
  330. {
  331. if (! isset($this->_attributes['namedNativeQueries'][$name])) {
  332. throw ORMException::namedNativeQueryNotFound($name);
  333. }
  334. return $this->_attributes['namedNativeQueries'][$name];
  335. }
  336. /**
  337. * Ensures that this Configuration instance contains settings that are
  338. * suitable for a production environment.
  339. *
  340. * @return void
  341. *
  342. * @throws ORMException If a configuration setting has a value that is not
  343. * suitable for a production environment.
  344. */
  345. public function ensureProductionSettings()
  346. {
  347. $queryCacheImpl = $this->getQueryCacheImpl();
  348. if (! $queryCacheImpl) {
  349. throw ORMException::queryCacheNotConfigured();
  350. }
  351. if ($queryCacheImpl instanceof ArrayCache) {
  352. throw ORMException::queryCacheUsesNonPersistentCache($queryCacheImpl);
  353. }
  354. $metadataCacheImpl = $this->getMetadataCacheImpl();
  355. if (! $metadataCacheImpl) {
  356. throw ORMException::metadataCacheNotConfigured();
  357. }
  358. if ($metadataCacheImpl instanceof ArrayCache) {
  359. throw ORMException::metadataCacheUsesNonPersistentCache($metadataCacheImpl);
  360. }
  361. if ($this->getAutoGenerateProxyClasses()) {
  362. throw ORMException::proxyClassesAlwaysRegenerating();
  363. }
  364. }
  365. /**
  366. * Registers a custom DQL function that produces a string value.
  367. * Such a function can then be used in any DQL statement in any place where string
  368. * functions are allowed.
  369. *
  370. * DQL function names are case-insensitive.
  371. *
  372. * @param string $name Function name.
  373. * @param string|callable $className Class name or a callable that returns the function.
  374. *
  375. * @return void
  376. */
  377. public function addCustomStringFunction($name, $className)
  378. {
  379. $this->_attributes['customStringFunctions'][strtolower($name)] = $className;
  380. }
  381. /**
  382. * Gets the implementation class name of a registered custom string DQL function.
  383. *
  384. * @param string $name
  385. *
  386. * @return string|null
  387. *
  388. * @psalm-return ?class-string
  389. */
  390. public function getCustomStringFunction($name)
  391. {
  392. $name = strtolower($name);
  393. return $this->_attributes['customStringFunctions'][$name] ?? null;
  394. }
  395. /**
  396. * Sets a map of custom DQL string functions.
  397. *
  398. * Keys must be function names and values the FQCN of the implementing class.
  399. * The function names will be case-insensitive in DQL.
  400. *
  401. * Any previously added string functions are discarded.
  402. *
  403. * @return void
  404. *
  405. * @psalm-param array<string, class-string> $functions The map of custom
  406. * DQL string functions.
  407. */
  408. public function setCustomStringFunctions(array $functions)
  409. {
  410. foreach ($functions as $name => $className) {
  411. $this->addCustomStringFunction($name, $className);
  412. }
  413. }
  414. /**
  415. * Registers a custom DQL function that produces a numeric value.
  416. * Such a function can then be used in any DQL statement in any place where numeric
  417. * functions are allowed.
  418. *
  419. * DQL function names are case-insensitive.
  420. *
  421. * @param string $name Function name.
  422. * @param string|callable $className Class name or a callable that returns the function.
  423. *
  424. * @return void
  425. */
  426. public function addCustomNumericFunction($name, $className)
  427. {
  428. $this->_attributes['customNumericFunctions'][strtolower($name)] = $className;
  429. }
  430. /**
  431. * Gets the implementation class name of a registered custom numeric DQL function.
  432. *
  433. * @param string $name
  434. *
  435. * @return string|null
  436. *
  437. * @psalm-return ?class-string
  438. */
  439. public function getCustomNumericFunction($name)
  440. {
  441. $name = strtolower($name);
  442. return $this->_attributes['customNumericFunctions'][$name] ?? null;
  443. }
  444. /**
  445. * Sets a map of custom DQL numeric functions.
  446. *
  447. * Keys must be function names and values the FQCN of the implementing class.
  448. * The function names will be case-insensitive in DQL.
  449. *
  450. * Any previously added numeric functions are discarded.
  451. *
  452. * @return void
  453. *
  454. * @psalm-param array<string, class-string> $functions The map of custom
  455. * DQL numeric functions.
  456. */
  457. public function setCustomNumericFunctions(array $functions)
  458. {
  459. foreach ($functions as $name => $className) {
  460. $this->addCustomNumericFunction($name, $className);
  461. }
  462. }
  463. /**
  464. * Registers a custom DQL function that produces a date/time value.
  465. * Such a function can then be used in any DQL statement in any place where date/time
  466. * functions are allowed.
  467. *
  468. * DQL function names are case-insensitive.
  469. *
  470. * @param string $name Function name.
  471. * @param string|callable $className Class name or a callable that returns the function.
  472. *
  473. * @return void
  474. *
  475. * @psalm-param class-string|callable $className
  476. */
  477. public function addCustomDatetimeFunction($name, $className)
  478. {
  479. $this->_attributes['customDatetimeFunctions'][strtolower($name)] = $className;
  480. }
  481. /**
  482. * Gets the implementation class name of a registered custom date/time DQL function.
  483. *
  484. * @param string $name
  485. *
  486. * @return string|null
  487. *
  488. * @psalm-return ?class-string $name
  489. */
  490. public function getCustomDatetimeFunction($name)
  491. {
  492. $name = strtolower($name);
  493. return $this->_attributes['customDatetimeFunctions'][$name] ?? null;
  494. }
  495. /**
  496. * Sets a map of custom DQL date/time functions.
  497. *
  498. * Keys must be function names and values the FQCN of the implementing class.
  499. * The function names will be case-insensitive in DQL.
  500. *
  501. * Any previously added date/time functions are discarded.
  502. *
  503. * @param array $functions The map of custom DQL date/time functions.
  504. *
  505. * @return void
  506. *
  507. * @psalm-param array<string, string> $functions
  508. */
  509. public function setCustomDatetimeFunctions(array $functions)
  510. {
  511. foreach ($functions as $name => $className) {
  512. $this->addCustomDatetimeFunction($name, $className);
  513. }
  514. }
  515. /**
  516. * Sets the custom hydrator modes in one pass.
  517. *
  518. * @param array<string, class-string> $modes An array of ($modeName => $hydrator).
  519. *
  520. * @return void
  521. */
  522. public function setCustomHydrationModes($modes)
  523. {
  524. $this->_attributes['customHydrationModes'] = [];
  525. foreach ($modes as $modeName => $hydrator) {
  526. $this->addCustomHydrationMode($modeName, $hydrator);
  527. }
  528. }
  529. /**
  530. * Gets the hydrator class for the given hydration mode name.
  531. *
  532. * @param string $modeName The hydration mode name.
  533. *
  534. * @return string|null The hydrator class name.
  535. *
  536. * @psalm-return ?class-string
  537. */
  538. public function getCustomHydrationMode($modeName)
  539. {
  540. return $this->_attributes['customHydrationModes'][$modeName] ?? null;
  541. }
  542. /**
  543. * Adds a custom hydration mode.
  544. *
  545. * @param string $modeName The hydration mode name.
  546. *
  547. * @return void
  548. *
  549. * @psalm-param class-string $hydrator The hydrator class name.
  550. */
  551. public function addCustomHydrationMode($modeName, $hydrator)
  552. {
  553. $this->_attributes['customHydrationModes'][$modeName] = $hydrator;
  554. }
  555. /**
  556. * Sets a class metadata factory.
  557. *
  558. * @param string $cmfName
  559. *
  560. * @return void
  561. *
  562. * @psalm-param class-string $cmfName
  563. */
  564. public function setClassMetadataFactoryName($cmfName)
  565. {
  566. $this->_attributes['classMetadataFactoryName'] = $cmfName;
  567. }
  568. /**
  569. * @return string
  570. *
  571. * @psalm-return class-string
  572. */
  573. public function getClassMetadataFactoryName()
  574. {
  575. if (! isset($this->_attributes['classMetadataFactoryName'])) {
  576. $this->_attributes['classMetadataFactoryName'] = ClassMetadataFactory::class;
  577. }
  578. return $this->_attributes['classMetadataFactoryName'];
  579. }
  580. /**
  581. * Adds a filter to the list of possible filters.
  582. *
  583. * @param string $name The name of the filter.
  584. * @param string $className The class name of the filter.
  585. */
  586. public function addFilter($name, $className)
  587. {
  588. $this->_attributes['filters'][$name] = $className;
  589. }
  590. /**
  591. * Gets the class name for a given filter name.
  592. *
  593. * @param string $name The name of the filter.
  594. *
  595. * @return string|null The class name of the filter, or null if it is not
  596. * defined.
  597. *
  598. * @psalm-return ?class-string
  599. */
  600. public function getFilterClassName($name)
  601. {
  602. return $this->_attributes['filters'][$name] ?? null;
  603. }
  604. /**
  605. * Sets default repository class.
  606. *
  607. * @param string $className
  608. *
  609. * @return void
  610. *
  611. * @throws ORMException If $classname is not an ObjectRepository.
  612. */
  613. public function setDefaultRepositoryClassName($className)
  614. {
  615. $reflectionClass = new ReflectionClass($className);
  616. if (! $reflectionClass->implementsInterface(ObjectRepository::class)) {
  617. throw ORMException::invalidEntityRepository($className);
  618. }
  619. $this->_attributes['defaultRepositoryClassName'] = $className;
  620. }
  621. /**
  622. * Get default repository class.
  623. *
  624. * @return string
  625. *
  626. * @psalm-return class-string
  627. */
  628. public function getDefaultRepositoryClassName()
  629. {
  630. return $this->_attributes['defaultRepositoryClassName'] ?? EntityRepository::class;
  631. }
  632. /**
  633. * Sets naming strategy.
  634. *
  635. * @return void
  636. */
  637. public function setNamingStrategy(NamingStrategy $namingStrategy)
  638. {
  639. $this->_attributes['namingStrategy'] = $namingStrategy;
  640. }
  641. /**
  642. * Gets naming strategy..
  643. *
  644. * @return NamingStrategy
  645. */
  646. public function getNamingStrategy()
  647. {
  648. if (! isset($this->_attributes['namingStrategy'])) {
  649. $this->_attributes['namingStrategy'] = new DefaultNamingStrategy();
  650. }
  651. return $this->_attributes['namingStrategy'];
  652. }
  653. /**
  654. * Sets quote strategy.
  655. *
  656. * @return void
  657. */
  658. public function setQuoteStrategy(QuoteStrategy $quoteStrategy)
  659. {
  660. $this->_attributes['quoteStrategy'] = $quoteStrategy;
  661. }
  662. /**
  663. * Gets quote strategy.
  664. *
  665. * @return QuoteStrategy
  666. */
  667. public function getQuoteStrategy()
  668. {
  669. if (! isset($this->_attributes['quoteStrategy'])) {
  670. $this->_attributes['quoteStrategy'] = new DefaultQuoteStrategy();
  671. }
  672. return $this->_attributes['quoteStrategy'];
  673. }
  674. /**
  675. * Set the entity listener resolver.
  676. */
  677. public function setEntityListenerResolver(EntityListenerResolver $resolver)
  678. {
  679. $this->_attributes['entityListenerResolver'] = $resolver;
  680. }
  681. /**
  682. * Get the entity listener resolver.
  683. *
  684. * @return EntityListenerResolver
  685. */
  686. public function getEntityListenerResolver()
  687. {
  688. if (! isset($this->_attributes['entityListenerResolver'])) {
  689. $this->_attributes['entityListenerResolver'] = new DefaultEntityListenerResolver();
  690. }
  691. return $this->_attributes['entityListenerResolver'];
  692. }
  693. /**
  694. * Set the entity repository factory.
  695. */
  696. public function setRepositoryFactory(RepositoryFactory $repositoryFactory)
  697. {
  698. $this->_attributes['repositoryFactory'] = $repositoryFactory;
  699. }
  700. /**
  701. * Get the entity repository factory.
  702. *
  703. * @return RepositoryFactory
  704. */
  705. public function getRepositoryFactory()
  706. {
  707. return $this->_attributes['repositoryFactory'] ?? new DefaultRepositoryFactory();
  708. }
  709. /**
  710. * @return bool
  711. */
  712. public function isSecondLevelCacheEnabled()
  713. {
  714. return $this->_attributes['isSecondLevelCacheEnabled'] ?? false;
  715. }
  716. /**
  717. * @param bool $flag
  718. *
  719. * @return void
  720. */
  721. public function setSecondLevelCacheEnabled($flag = true)
  722. {
  723. $this->_attributes['isSecondLevelCacheEnabled'] = (bool) $flag;
  724. }
  725. /**
  726. * @return void
  727. */
  728. public function setSecondLevelCacheConfiguration(CacheConfiguration $cacheConfig)
  729. {
  730. $this->_attributes['secondLevelCacheConfiguration'] = $cacheConfig;
  731. }
  732. /**
  733. * @return CacheConfiguration|null
  734. */
  735. public function getSecondLevelCacheConfiguration()
  736. {
  737. if (! isset($this->_attributes['secondLevelCacheConfiguration']) && $this->isSecondLevelCacheEnabled()) {
  738. $this->_attributes['secondLevelCacheConfiguration'] = new CacheConfiguration();
  739. }
  740. return $this->_attributes['secondLevelCacheConfiguration'] ?? null;
  741. }
  742. /**
  743. * Returns query hints, which will be applied to every query in application
  744. *
  745. * @psalm-return array<string, mixed>
  746. */
  747. public function getDefaultQueryHints()
  748. {
  749. return $this->_attributes['defaultQueryHints'] ?? [];
  750. }
  751. /**
  752. * Sets array of query hints, which will be applied to every query in application
  753. *
  754. * @psalm-param array<string, mixed> $defaultQueryHints
  755. */
  756. public function setDefaultQueryHints(array $defaultQueryHints)
  757. {
  758. $this->_attributes['defaultQueryHints'] = $defaultQueryHints;
  759. }
  760. /**
  761. * Gets the value of a default query hint. If the hint name is not recognized, FALSE is returned.
  762. *
  763. * @param string $name The name of the hint.
  764. *
  765. * @return mixed The value of the hint or FALSE, if the hint name is not recognized.
  766. */
  767. public function getDefaultQueryHint($name)
  768. {
  769. return $this->_attributes['defaultQueryHints'][$name] ?? false;
  770. }
  771. /**
  772. * Sets a default query hint. If the hint name is not recognized, it is silently ignored.
  773. *
  774. * @param string $name The name of the hint.
  775. * @param mixed $value The value of the hint.
  776. */
  777. public function setDefaultQueryHint($name, $value)
  778. {
  779. $this->_attributes['defaultQueryHints'][$name] = $value;
  780. }
  781. }