CakePHP
  • Documentation
    • Book
    • API
    • Videos
    • Reporting Security Issues
    • Privacy Policy
    • Logos & Trademarks
  • Business Solutions
  • Swag
  • Road Trip
  • Team
  • Community
    • Community
    • Get Involved
    • Issues (GitHub)
    • Bakery
    • Featured Resources
    • Training
    • Meetups
    • My CakePHP
    • CakeFest
    • Newsletter
    • Linkedin
    • YouTube
    • Facebook
    • Twitter
    • Mastodon
    • Help & Support
    • Forum
    • Stack Overflow
    • Slack
    • Paid Support
CakePHP

C CakePHP 2.10 API

  • Overview
  • Tree
  • Deprecated
  • Version:
    • 2.10
      • 4.2
      • 4.1
      • 4.0
      • 3.9
      • 3.8
      • 3.7
      • 3.6
      • 3.5
      • 3.4
      • 3.3
      • 3.2
      • 3.1
      • 3.0
      • 2.10
      • 2.9
      • 2.8
      • 2.7
      • 2.6
      • 2.5
      • 2.4
      • 2.3
      • 2.2
      • 2.1
      • 2.0
      • 1.3
      • 1.2

Packages

  • Cake
    • Cache
      • Engine
    • Configure
    • Console
      • Command
        • Task
    • Controller
      • Component
        • Acl
        • Auth
    • Core
    • Error
    • Event
    • I18n
    • Log
      • Engine
    • Model
      • Behavior
      • Datasource
        • Database
        • Session
      • Validator
    • Network
      • Email
      • Http
    • Routing
      • Filter
      • Route
    • TestSuite
      • Coverage
      • Fixture
      • Reporter
    • Utility
    • View
      • Helper
  • None

Classes

  • App
  • CakeObject
  • CakePlugin
  • Configure

Interfaces

  • ConfigReaderInterface
  1: <?php
  2: /**
  3:  * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4:  * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  5:  *
  6:  * Licensed under The MIT License
  7:  * For full copyright and license information, please see the LICENSE.txt
  8:  * Redistributions of files must retain the above copyright notice.
  9:  *
 10:  * @copyright     Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
 11:  * @link          https://cakephp.org CakePHP(tm) Project
 12:  * @package       Cake.Core
 13:  * @since         CakePHP(tm) v 1.0.0.2363
 14:  * @license       https://opensource.org/licenses/mit-license.php MIT License
 15:  */
 16: 
 17: App::uses('Hash', 'Utility');
 18: App::uses('ConfigReaderInterface', 'Configure');
 19: 
 20: /**
 21:  * Compatibility with 2.1, which expects Configure to load Set.
 22:  */
 23: App::uses('Set', 'Utility');
 24: 
 25: /**
 26:  * Configuration class. Used for managing runtime configuration information.
 27:  *
 28:  * Provides features for reading and writing to the runtime configuration, as well
 29:  * as methods for loading additional configuration files or storing runtime configuration
 30:  * for future use.
 31:  *
 32:  * @package       Cake.Core
 33:  * @link          https://book.cakephp.org/2.0/en/development/configuration.html#configure-class
 34:  */
 35: class Configure {
 36: 
 37: /**
 38:  * Array of values currently stored in Configure.
 39:  *
 40:  * @var array
 41:  */
 42:     protected static $_values = array(
 43:         'debug' => 0
 44:     );
 45: 
 46: /**
 47:  * Configured reader classes, used to load config files from resources
 48:  *
 49:  * @var array
 50:  * @see Configure::load()
 51:  */
 52:     protected static $_readers = array();
 53: 
 54: /**
 55:  * Initializes configure and runs the bootstrap process.
 56:  * Bootstrapping includes the following steps:
 57:  *
 58:  * - Setup App array in Configure.
 59:  * - Include app/Config/core.php.
 60:  * - Configure core cache configurations.
 61:  * - Load App cache files.
 62:  * - Include app/Config/bootstrap.php.
 63:  * - Setup error/exception handlers.
 64:  *
 65:  * @param bool $boot Whether to do bootstrapping.
 66:  * @return void
 67:  */
 68:     public static function bootstrap($boot = true) {
 69:         if ($boot) {
 70:             static::_appDefaults();
 71: 
 72:             if (!include CONFIG . 'core.php') {
 73:                 trigger_error(__d('cake_dev',
 74:                         "Can't find application core file. Please create %s, and make sure it is readable by PHP.",
 75:                         CONFIG . 'core.php'),
 76:                     E_USER_ERROR
 77:                 );
 78:             }
 79:             App::init();
 80:             App::$bootstrapping = false;
 81:             App::build();
 82: 
 83:             $exception = array(
 84:                 'handler' => 'ErrorHandler::handleException',
 85:             );
 86:             $error = array(
 87:                 'handler' => 'ErrorHandler::handleError',
 88:                 'level' => E_ALL & ~E_DEPRECATED,
 89:             );
 90:             if (PHP_SAPI === 'cli') {
 91:                 App::uses('ConsoleErrorHandler', 'Console');
 92:                 $console = new ConsoleErrorHandler();
 93:                 $exception['handler'] = array($console, 'handleException');
 94:                 $error['handler'] = array($console, 'handleError');
 95:             }
 96:             static::_setErrorHandlers($error, $exception);
 97: 
 98:             if (!include CONFIG . 'bootstrap.php') {
 99:                 trigger_error(__d('cake_dev',
100:                         "Can't find application bootstrap file. Please create %s, and make sure it is readable by PHP.",
101:                         CONFIG . 'bootstrap.php'),
102:                     E_USER_ERROR
103:                 );
104:             }
105:             restore_error_handler();
106: 
107:             static::_setErrorHandlers(
108:                 static::$_values['Error'],
109:                 static::$_values['Exception']
110:             );
111: 
112:             // Preload Debugger + CakeText in case of E_STRICT errors when loading files.
113:             if (static::$_values['debug'] > 0) {
114:                 class_exists('Debugger');
115:                 class_exists('CakeText');
116:             }
117:             if (!defined('TESTS')) {
118:                 define('TESTS', APP . 'Test' . DS);
119:             }
120:         }
121:     }
122: 
123: /**
124:  * Set app's default configs
125:  *
126:  * @return void
127:  */
128:     protected static function _appDefaults() {
129:         static::write('App', (array)static::read('App') + array(
130:             'base' => false,
131:             'baseUrl' => false,
132:             'dir' => APP_DIR,
133:             'webroot' => WEBROOT_DIR,
134:             'www_root' => WWW_ROOT
135:         ));
136:     }
137: 
138: /**
139:  * Used to store a dynamic variable in Configure.
140:  *
141:  * Usage:
142:  * ```
143:  * Configure::write('One.key1', 'value of the Configure::One[key1]');
144:  * Configure::write(array('One.key1' => 'value of the Configure::One[key1]'));
145:  * Configure::write('One', array(
146:  *     'key1' => 'value of the Configure::One[key1]',
147:  *     'key2' => 'value of the Configure::One[key2]'
148:  * );
149:  *
150:  * Configure::write(array(
151:  *     'One.key1' => 'value of the Configure::One[key1]',
152:  *     'One.key2' => 'value of the Configure::One[key2]'
153:  * ));
154:  * ```
155:  *
156:  * @param string|array $config The key to write, can be a dot notation value.
157:  * Alternatively can be an array containing key(s) and value(s).
158:  * @param mixed $value Value to set for var
159:  * @return bool True if write was successful
160:  * @link https://book.cakephp.org/2.0/en/development/configuration.html#Configure::write
161:  */
162:     public static function write($config, $value = null) {
163:         if (!is_array($config)) {
164:             $config = array($config => $value);
165:         }
166: 
167:         foreach ($config as $name => $value) {
168:             static::$_values = Hash::insert(static::$_values, $name, $value);
169:         }
170: 
171:         if (isset($config['debug']) && function_exists('ini_set')) {
172:             if (static::$_values['debug']) {
173:                 ini_set('display_errors', 1);
174:             } else {
175:                 ini_set('display_errors', 0);
176:             }
177:         }
178:         return true;
179:     }
180: 
181: /**
182:  * Used to read information stored in Configure. It's not
183:  * possible to store `null` values in Configure.
184:  *
185:  * Usage:
186:  * ```
187:  * Configure::read('Name'); will return all values for Name
188:  * Configure::read('Name.key'); will return only the value of Configure::Name[key]
189:  * ```
190:  *
191:  * @param string|null $var Variable to obtain. Use '.' to access array elements.
192:  * @return mixed value stored in configure, or null.
193:  * @link https://book.cakephp.org/2.0/en/development/configuration.html#Configure::read
194:  */
195:     public static function read($var = null) {
196:         if ($var === null) {
197:             return static::$_values;
198:         }
199:         return Hash::get(static::$_values, $var);
200:     }
201: 
202: /**
203:  * Used to read and delete a variable from Configure.
204:  *
205:  * This is primarily used during bootstrapping to move configuration data
206:  * out of configure into the various other classes in CakePHP.
207:  *
208:  * @param string $var The key to read and remove.
209:  * @return array|null
210:  */
211:     public static function consume($var) {
212:         $simple = strpos($var, '.') === false;
213:         if ($simple && !isset(static::$_values[$var])) {
214:             return null;
215:         }
216:         if ($simple) {
217:             $value = static::$_values[$var];
218:             unset(static::$_values[$var]);
219:             return $value;
220:         }
221:         $value = Hash::get(static::$_values, $var);
222:         static::$_values = Hash::remove(static::$_values, $var);
223:         return $value;
224:     }
225: 
226: /**
227:  * Returns true if given variable is set in Configure.
228:  *
229:  * @param string $var Variable name to check for
230:  * @return bool True if variable is there
231:  */
232:     public static function check($var) {
233:         if (empty($var)) {
234:             return false;
235:         }
236:         return Hash::get(static::$_values, $var) !== null;
237:     }
238: 
239: /**
240:  * Used to delete a variable from Configure.
241:  *
242:  * Usage:
243:  * ```
244:  * Configure::delete('Name'); will delete the entire Configure::Name
245:  * Configure::delete('Name.key'); will delete only the Configure::Name[key]
246:  * ```
247:  *
248:  * @param string $var the var to be deleted
249:  * @return void
250:  * @link https://book.cakephp.org/2.0/en/development/configuration.html#Configure::delete
251:  */
252:     public static function delete($var) {
253:         static::$_values = Hash::remove(static::$_values, $var);
254:     }
255: 
256: /**
257:  * Add a new reader to Configure. Readers allow you to read configuration
258:  * files in various formats/storage locations. CakePHP comes with two built-in readers
259:  * PhpReader and IniReader. You can also implement your own reader classes in your application.
260:  *
261:  * To add a new reader to Configure:
262:  *
263:  * `Configure::config('ini', new IniReader());`
264:  *
265:  * @param string $name The name of the reader being configured. This alias is used later to
266:  *   read values from a specific reader.
267:  * @param ConfigReaderInterface $reader The reader to append.
268:  * @return void
269:  */
270:     public static function config($name, ConfigReaderInterface $reader) {
271:         static::$_readers[$name] = $reader;
272:     }
273: 
274: /**
275:  * Gets the names of the configured reader objects.
276:  *
277:  * @param string|null $name Name to check. If null returns all configured reader names.
278:  * @return array Array of the configured reader objects.
279:  */
280:     public static function configured($name = null) {
281:         if ($name) {
282:             return isset(static::$_readers[$name]);
283:         }
284:         return array_keys(static::$_readers);
285:     }
286: 
287: /**
288:  * Remove a configured reader. This will unset the reader
289:  * and make any future attempts to use it cause an Exception.
290:  *
291:  * @param string $name Name of the reader to drop.
292:  * @return bool Success
293:  */
294:     public static function drop($name) {
295:         if (!isset(static::$_readers[$name])) {
296:             return false;
297:         }
298:         unset(static::$_readers[$name]);
299:         return true;
300:     }
301: 
302: /**
303:  * Loads stored configuration information from a resource. You can add
304:  * config file resource readers with `Configure::config()`.
305:  *
306:  * Loaded configuration information will be merged with the current
307:  * runtime configuration. You can load configuration files from plugins
308:  * by preceding the filename with the plugin name.
309:  *
310:  * `Configure::load('Users.user', 'default')`
311:  *
312:  * Would load the 'user' config file using the default config reader. You can load
313:  * app config files by giving the name of the resource you want loaded.
314:  *
315:  * `Configure::load('setup', 'default');`
316:  *
317:  * If using `default` config and no reader has been configured for it yet,
318:  * one will be automatically created using PhpReader
319:  *
320:  * @param string $key name of configuration resource to load.
321:  * @param string $config Name of the configured reader to use to read the resource identified by $key.
322:  * @param bool $merge if config files should be merged instead of simply overridden
323:  * @return bool False if file not found, true if load successful.
324:  * @throws ConfigureException Will throw any exceptions the reader raises.
325:  * @link https://book.cakephp.org/2.0/en/development/configuration.html#Configure::load
326:  */
327:     public static function load($key, $config = 'default', $merge = true) {
328:         $reader = static::_getReader($config);
329:         if (!$reader) {
330:             return false;
331:         }
332:         $values = $reader->read($key);
333: 
334:         if ($merge) {
335:             $keys = array_keys($values);
336:             foreach ($keys as $key) {
337:                 if (($c = static::read($key)) && is_array($values[$key]) && is_array($c)) {
338:                     $values[$key] = Hash::merge($c, $values[$key]);
339:                 }
340:             }
341:         }
342: 
343:         return static::write($values);
344:     }
345: 
346: /**
347:  * Dump data currently in Configure into $key. The serialization format
348:  * is decided by the config reader attached as $config. For example, if the
349:  * 'default' adapter is a PhpReader, the generated file will be a PHP
350:  * configuration file loadable by the PhpReader.
351:  *
352:  * ## Usage
353:  *
354:  * Given that the 'default' reader is an instance of PhpReader.
355:  * Save all data in Configure to the file `my_config.php`:
356:  *
357:  * `Configure::dump('my_config.php', 'default');`
358:  *
359:  * Save only the error handling configuration:
360:  *
361:  * `Configure::dump('error.php', 'default', array('Error', 'Exception');`
362:  *
363:  * @param string $key The identifier to create in the config adapter.
364:  *   This could be a filename or a cache key depending on the adapter being used.
365:  * @param string $config The name of the configured adapter to dump data with.
366:  * @param array $keys The name of the top-level keys you want to dump.
367:  *   This allows you save only some data stored in Configure.
368:  * @return bool success
369:  * @throws ConfigureException if the adapter does not implement a `dump` method.
370:  */
371:     public static function dump($key, $config = 'default', $keys = array()) {
372:         $reader = static::_getReader($config);
373:         if (!$reader) {
374:             throw new ConfigureException(__d('cake_dev', 'There is no "%s" adapter.', $config));
375:         }
376:         if (!method_exists($reader, 'dump')) {
377:             throw new ConfigureException(__d('cake_dev', 'The "%s" adapter, does not have a %s method.', $config, 'dump()'));
378:         }
379:         $values = static::$_values;
380:         if (!empty($keys) && is_array($keys)) {
381:             $values = array_intersect_key($values, array_flip($keys));
382:         }
383:         return (bool)$reader->dump($key, $values);
384:     }
385: 
386: /**
387:  * Get the configured reader. Internally used by `Configure::load()` and `Configure::dump()`
388:  * Will create new PhpReader for default if not configured yet.
389:  *
390:  * @param string $config The name of the configured adapter
391:  * @return mixed Reader instance or false
392:  */
393:     protected static function _getReader($config) {
394:         if (!isset(static::$_readers[$config])) {
395:             if ($config !== 'default') {
396:                 return false;
397:             }
398:             App::uses('PhpReader', 'Configure');
399:             static::config($config, new PhpReader());
400:         }
401:         return static::$_readers[$config];
402:     }
403: 
404: /**
405:  * Used to determine the current version of CakePHP.
406:  *
407:  * Usage `Configure::version();`
408:  *
409:  * @return string Current version of CakePHP
410:  */
411:     public static function version() {
412:         if (!isset(static::$_values['Cake']['version'])) {
413:             require CAKE . 'Config' . DS . 'config.php';
414:             static::write($config);
415:         }
416:         return static::$_values['Cake']['version'];
417:     }
418: 
419: /**
420:  * Used to write runtime configuration into Cache. Stored runtime configuration can be
421:  * restored using `Configure::restore()`. These methods can be used to enable configuration managers
422:  * frontends, or other GUI type interfaces for configuration.
423:  *
424:  * @param string $name The storage name for the saved configuration.
425:  * @param string $cacheConfig The cache configuration to save into. Defaults to 'default'
426:  * @param array $data Either an array of data to store, or leave empty to store all values.
427:  * @return bool Success
428:  */
429:     public static function store($name, $cacheConfig = 'default', $data = null) {
430:         if ($data === null) {
431:             $data = static::$_values;
432:         }
433:         return Cache::write($name, $data, $cacheConfig);
434:     }
435: 
436: /**
437:  * Restores configuration data stored in the Cache into configure. Restored
438:  * values will overwrite existing ones.
439:  *
440:  * @param string $name Name of the stored config file to load.
441:  * @param string $cacheConfig Name of the Cache configuration to read from.
442:  * @return bool Success.
443:  */
444:     public static function restore($name, $cacheConfig = 'default') {
445:         $values = Cache::read($name, $cacheConfig);
446:         if ($values) {
447:             return static::write($values);
448:         }
449:         return false;
450:     }
451: 
452: /**
453:  * Clear all values stored in Configure.
454:  *
455:  * @return bool Success.
456:  */
457:     public static function clear() {
458:         static::$_values = array();
459:         return true;
460:     }
461: 
462: /**
463:  * Set the error and exception handlers.
464:  *
465:  * @param array $error The Error handling configuration.
466:  * @param array $exception The exception handling configuration.
467:  * @return void
468:  */
469:     protected static function _setErrorHandlers($error, $exception) {
470:         $level = -1;
471:         if (isset($error['level'])) {
472:             error_reporting($error['level']);
473:             $level = $error['level'];
474:         }
475:         if (!empty($error['handler'])) {
476:             set_error_handler($error['handler'], $level);
477:         }
478:         if (!empty($exception['handler'])) {
479:             set_exception_handler($exception['handler']);
480:         }
481:     }
482: 
483: }
484: 
OpenHub
Rackspace
Rackspace
  • Business Solutions
  • Showcase
  • Documentation
  • Book
  • API
  • Videos
  • Reporting Security Issues
  • Privacy Policy
  • Logos & Trademarks
  • Community
  • Get Involved
  • Issues (GitHub)
  • Bakery
  • Featured Resources
  • Training
  • Meetups
  • My CakePHP
  • CakeFest
  • Newsletter
  • Linkedin
  • YouTube
  • Facebook
  • Twitter
  • Mastodon
  • Help & Support
  • Forum
  • Stack Overflow
  • Slack
  • Paid Support

Generated using CakePHP API Docs