Configuration.php 12.3 KB
Newer Older
1
<?php
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 *  $Id$
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * This software consists of voluntary contributions made by many individuals
 * and is licensed under the LGPL. For more information, see
19
 * <http://www.doctrine-project.org>.
20
 */
21

22
namespace Doctrine\ORM;
23 24

/**
25
 * Configuration container for all configuration options of Doctrine.
26
 * It combines all configuration options from DBAL & ORM.
27
 *
28
 * @author Roman Borschel <roman@code-factory.org>
29
 * @since 2.0
30 31
 * @internal When adding a new configuration option just write a getter/setter
 * pair and add the option to the _attributes array with a proper default value.
32
 */
33
class Configuration extends \Doctrine\DBAL\Configuration
34
{
35 36 37
    /**
     * Creates a new configuration that can be used for Doctrine.
     */
38
    public function __construct()
39
    {
40
        parent::__construct();
41 42 43
        $this->_attributes = array_merge($this->_attributes, array(
            'resultCacheImpl' => null,
            'queryCacheImpl' => null,
44
            'metadataCacheImpl' => null,
45
            'metadataDriverImpl' => null,
46
            'proxyDir' => null,
47
            'useCExtension' => false,
48
            'autoGenerateProxyClasses' => true,
49
            'proxyNamespace' => null
50
        ));
51
    }
52

53
    /**
54
     * Sets the directory where Doctrine generates any necessary proxy class files.
55 56 57
     *
     * @param string $dir
     */
58
    public function setProxyDir($dir)
59
    {
60
        $this->_attributes['proxyDir'] = $dir;
61 62
    }

63
    /**
64
     * Gets the directory where Doctrine generates any necessary proxy class files.
65 66 67
     *
     * @return string
     */
68
    public function getProxyDir()
69
    {
70 71
        return $this->_attributes['proxyDir'];
    }
72

73 74 75 76 77 78 79 80 81 82
    /**
     * Gets a boolean flag that indicates whether proxy classes should always be regenerated
     * during each script execution.
     *
     * @return boolean
     */
    public function getAutoGenerateProxyClasses()
    {
        return $this->_attributes['autoGenerateProxyClasses'];
    }
83

84 85 86 87 88 89 90 91 92 93
    /**
     * Sets a boolean flag that indicates whether proxy classes should always be regenerated
     * during each script execution.
     *
     * @param boolean $bool
     */
    public function setAutoGenerateProxyClasses($bool)
    {
        $this->_attributes['autoGenerateProxyClasses'] = $bool;
    }
94

95 96 97 98 99
    /**
     * Gets the namespace where proxy classes reside.
     * 
     * @return string
     */
100 101 102 103
    public function getProxyNamespace()
    {
        return $this->_attributes['proxyNamespace'];
    }
104

105 106 107 108 109
    /**
     * Sets the namespace where proxy classes reside.
     * 
     * @param string $ns
     */
110 111 112
    public function setProxyNamespace($ns)
    {
        $this->_attributes['proxyNamespace'] = $ns;
113 114
    }

115 116 117 118
    /**
     * Sets the cache driver implementation that is used for metadata caching.
     *
     * @param object $driverImpl
119 120
     * @todo Force parameter to be a Closure to ensure lazy evaluation
     *       (as soon as a metadata cache is in effect, the driver never needs to initialize).
121
     */
122 123 124 125 126
    public function setMetadataDriverImpl($driverImpl)
    {
        $this->_attributes['metadataDriverImpl'] = $driverImpl;
    }

127
    /**
128
     * Adds a namespace under a certain alias.
129 130
     *
     * @param string $alias
131
     * @param string $namespace
132
     */
133
    public function addEntityNamespace($alias, $namespace)
134
    {
135
        $this->_attributes['entityNamespaces'][$alias] = $namespace;
136 137 138
    }

    /**
139
     * Resolves a registered namespace alias to the full namespace.
140
     *
141 142 143
     * @param string $entityNamespaceAlias 
     * @return string
     * @throws MappingException
144
     */
145
    public function getEntityNamespace($entityNamespaceAlias)
146
    {
147 148
        if ( ! isset($this->_attributes['entityNamespaces'][$entityNamespaceAlias])) {
            throw ORMException::unknownEntityNamespace($entityNamespaceAlias);
149 150
        }

151
        return trim($this->_attributes['entityNamespaces'][$entityNamespaceAlias], '\\');
152 153 154 155 156
    }

    /**
     * Set the entity alias map
     *
157
     * @param array $entityAliasMap
158 159
     * @return void
     */
160
    public function setEntityNamespaces(array $entityNamespaces)
161
    {
162
        $this->_attributes['entityNamespaces'] = $entityNamespaces;
163 164
    }

165 166 167 168 169
    /**
     * Gets the cache driver implementation that is used for the mapping metadata.
     *
     * @return object
     */
170 171
    public function getMetadataDriverImpl()
    {
172
        if ($this->_attributes['metadataDriverImpl'] == null) {
173 174 175 176 177
            $reader = new \Doctrine\Common\Annotations\AnnotationReader(new \Doctrine\Common\Cache\ArrayCache);
            $reader->setDefaultAnnotationNamespace('Doctrine\ORM\Mapping\\');
            $this->_attributes['metadataDriverImpl'] = new \Doctrine\ORM\Mapping\Driver\AnnotationDriver($reader);
        }

178 179
        return $this->_attributes['metadataDriverImpl'];
    }
180 181 182 183 184 185

    /**
     * Gets the cache driver implementation that is used for query result caching.
     *
     * @return object
     */
186 187 188 189
    public function getResultCacheImpl()
    {
        return $this->_attributes['resultCacheImpl'];
    }
190 191 192 193 194 195

    /**
     * Sets the cache driver implementation that is used for query result caching.
     *
     * @param object $cacheImpl
     */
196
    public function setResultCacheImpl($cacheImpl)
197 198 199
    {
        $this->_attributes['resultCacheImpl'] = $cacheImpl;
    }
200 201 202 203 204 205

    /**
     * Gets the cache driver implementation that is used for the query cache (SQL cache).
     *
     * @return object
     */
206 207 208 209
    public function getQueryCacheImpl()
    {
        return $this->_attributes['queryCacheImpl'];
    }
210 211 212 213 214 215

    /**
     * Sets the cache driver implementation that is used for the query cache (SQL cache).
     *
     * @param object $cacheImpl
     */
216
    public function setQueryCacheImpl($cacheImpl)
217 218 219
    {
        $this->_attributes['queryCacheImpl'] = $cacheImpl;
    }
220 221 222 223 224 225

    /**
     * Gets the cache driver implementation that is used for metadata caching.
     *
     * @return object
     */
226 227 228 229
    public function getMetadataCacheImpl()
    {
        return $this->_attributes['metadataCacheImpl'];
    }
230 231 232 233 234 235

    /**
     * Sets the cache driver implementation that is used for metadata caching.
     *
     * @param object $cacheImpl
     */
236
    public function setMetadataCacheImpl($cacheImpl)
237 238 239
    {
        $this->_attributes['metadataCacheImpl'] = $cacheImpl;
    }
240

241 242 243
    /**
     * Gets a boolean flag that indicates whether Doctrine should make use of the
     * C extension.
244
     *
245 246
     * @return boolean TRUE if Doctrine is configured to use the C extension, FALSE otherwise.
     */
247 248 249 250
    public function getUseCExtension()
    {
        return $this->_attributes['useCExtension'];
    }
251

252 253 254
    /**
     * Sets a boolean flag that indicates whether Doctrine should make use of the
     * C extension.
255
     *
256 257
     * @param boolean $boolean Whether to make use of the C extension or not.
     */
258 259 260 261
    public function setUseCExtension($boolean)
    {
        $this->_attributes['useCExtension'] = $boolean;
    }
262

263 264
    /**
     * Adds a named DQL query to the configuration.
265
     *
266 267 268 269 270 271 272
     * @param string $name The name of the query.
     * @param string $dql The DQL query string.
     */
    public function addNamedQuery($name, $dql)
    {
        $this->_attributes['namedQueries'][$name] = $dql;
    }
273

274 275
    /**
     * Gets a previously registered named DQL query.
276
     *
277 278 279 280 281
     * @param string $name The name of the query.
     * @return string The DQL query.
     */
    public function getNamedQuery($name)
    {
282 283 284
        if ( ! isset($this->_attributes['namedQueries'][$name])) {
            throw ORMException::namedQueryNotFound($name);
        }
285 286
        return $this->_attributes['namedQueries'][$name];
    }
287

288 289
    /**
     * Adds a named native query to the configuration.
290
     *
291
     * @param string $name The name of the query.
292
     * @param string $sql The native SQL query string.
293 294 295 296 297 298
     * @param ResultSetMapping $rsm The ResultSetMapping used for the results of the SQL query.
     */
    public function addNamedNativeQuery($name, $sql, Query\ResultSetMapping $rsm)
    {
        $this->_attributes['namedNativeQueries'][$name] = array($sql, $rsm);
    }
299

300 301
    /**
     * Gets the components of a previously registered named native query.
302
     *
303 304 305 306 307 308
     * @param string $name The name of the query.
     * @return array A tuple with the first element being the SQL string and the second
     *          element being the ResultSetMapping.
     */
    public function getNamedNativeQuery($name)
    {
309 310 311
        if ( ! isset($this->_attributes['namedNativeQueries'][$name])) {
            throw ORMException::namedNativeQueryNotFound($name);
        }
312 313
        return $this->_attributes['namedNativeQueries'][$name];
    }
314

315 316 317
    /**
     * Ensures that this Configuration instance contains settings that are
     * suitable for a production environment.
318 319 320
     *
     * @throws ORMException If a configuration setting has a value that is not
     *                      suitable for a production environment.
321 322 323 324
     */
    public function ensureProductionSettings()
    {
        if ( ! $this->_attributes['queryCacheImpl']) {
325
            throw ORMException::queryCacheNotConfigured();
326 327
        }
        if ( ! $this->_attributes['metadataCacheImpl']) {
328
            throw ORMException::metadataCacheNotConfigured();
329 330
        }
        if ($this->_attributes['autoGenerateProxyClasses']) {
331
            throw ORMException::proxyClassesAlwaysRegenerating();
332 333
        }
    }
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408

    /**
     * Registers a custom DQL function that produces a string value.
     * Such a function can then be used in any DQL statement in any place where string
     * functions are allowed.
     *
     * @param string $name
     * @param string $className
     */
    public function addCustomStringFunction($name, $className)
    {
        $this->_attributes['customStringFunctions'][strtolower($name)] = $className;
    }

    /**
     * Gets the implementation class name of a registered custom string DQL function.
     * 
     * @param string $name
     * @return string
     */
    public function getCustomStringFunction($name)
    {
        return isset($this->_attributes['customStringFunctions'][$name]) ?
                $this->_attributes['customStringFunctions'][$name] : null;
    }

    /**
     * Registers a custom DQL function that produces a numeric value.
     * Such a function can then be used in any DQL statement in any place where numeric
     * functions are allowed.
     *
     * @param string $name
     * @param string $className
     */
    public function addCustomNumericFunction($name, $className)
    {
        $this->_attributes['customNumericFunctions'][strtolower($name)] = $className;
    }

    /**
     * Gets the implementation class name of a registered custom numeric DQL function.
     * 
     * @param string $name
     * @return string
     */
    public function getCustomNumericFunction($name)
    {
        return isset($this->_attributes['customNumericFunctions'][$name]) ?
                $this->_attributes['customNumericFunctions'][$name] : null;
    }

    /**
     * Registers a custom DQL function that produces a date/time value.
     * Such a function can then be used in any DQL statement in any place where date/time
     * functions are allowed.
     *
     * @param string $name
     * @param string $className
     */
    public function addCustomDatetimeFunction($name, $className)
    {
        $this->_attributes['customDatetimeFunctions'][strtolower($name)] = $className;
    }

    /**
     * Gets the implementation class name of a registered custom date/time DQL function.
     * 
     * @param string $name
     * @return string
     */
    public function getCustomDatetimeFunction($name)
    {
        return isset($this->_attributes['customDatetimeFunctions'][$name]) ?
                $this->_attributes['customDatetimeFunctions'][$name] : null;
    }
409
}