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 1.2 API

  • Overview
  • Tree
  • Deprecated
  • Version:
    • 1.2
      • 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

Classes

  • AclBase
  • AclBehavior
  • AclComponent
  • AclNode
  • AclShell
  • Aco
  • AcoAction
  • AjaxHelper
  • ApcEngine
  • ApiShell
  • App
  • AppController
  • AppHelper
  • AppModel
  • Aro
  • AuthComponent
  • BakeShell
  • BehaviorCollection
  • Cache
  • CacheEngine
  • CacheHelper
  • CakeErrorController
  • CakeLog
  • CakeSchema
  • CakeSession
  • CakeSocket
  • ClassRegistry
  • Component
  • Configure
  • ConnectionManager
  • ConsoleShell
  • ContainableBehavior
  • Controller
  • ControllerTask
  • CookieComponent
  • DataSource
  • DbAcl
  • DbAclSchema
  • DbConfigTask
  • DboAdodb
  • DboDb2
  • DboFirebird
  • DboMssql
  • DboMysql
  • DboMysqlBase
  • DboMysqli
  • DboOdbc
  • DboOracle
  • DboPostgres
  • DboSource
  • DboSqlite
  • DboSybase
  • Debugger
  • EmailComponent
  • ErrorHandler
  • ExtractTask
  • File
  • FileEngine
  • Flay
  • Folder
  • FormHelper
  • Helper
  • HtmlHelper
  • HttpSocket
  • I18n
  • I18nModel
  • i18nSchema
  • I18nShell
  • Inflector
  • IniAcl
  • JavascriptHelper
  • JsHelper
  • JsHelperObject
  • L10n
  • MagicDb
  • MagicFileResource
  • MediaView
  • MemcacheEngine
  • Model
  • ModelBehavior
  • ModelTask
  • Multibyte
  • NumberHelper
  • Object
  • Overloadable
  • Overloadable2
  • PagesController
  • PaginatorHelper
  • Permission
  • PluginTask
  • ProjectTask
  • RequestHandlerComponent
  • Router
  • RssHelper
  • Sanitize
  • Scaffold
  • ScaffoldView
  • SchemaShell
  • Security
  • SecurityComponent
  • SessionComponent
  • SessionHelper
  • SessionsSchema
  • Set
  • Shell
  • String
  • TestSuiteShell
  • TestTask
  • TextHelper
  • ThemeView
  • TimeHelper
  • TranslateBehavior
  • TreeBehavior
  • Validation
  • View
  • ViewTask
  • XcacheEngine
  • Xml
  • XmlElement
  • XmlHelper
  • XmlManager
  • XmlNode
  • XmlTextNode

Functions

  • __enclose
  • make_clean_css
  • mb_encode_mimeheader
  • mb_stripos
  • mb_stristr
  • mb_strlen
  • mb_strpos
  • mb_strrchr
  • mb_strrichr
  • mb_strripos
  • mb_strrpos
  • mb_strstr
  • mb_strtolower
  • mb_strtoupper
  • mb_substr
  • mb_substr_count
  • write_css_cache
   1: <?php
   2: /* SVN FILE: $Id$ */
   3: /**
   4:  * Short description for file.
   5:  *
   6:  * Long description for filec
   7:  *
   8:  * PHP versions 4 and 5
   9:  *
  10:  * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  11:  * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  12:  *
  13:  * Licensed under The MIT License
  14:  * Redistributions of files must retain the above copyright notice.
  15:  *
  16:  * @copyright     Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  17:  * @link          http://cakephp.org CakePHP(tm) Project
  18:  * @package       cake
  19:  * @subpackage    cake.cake.libs
  20:  * @since         CakePHP(tm) v 1.0.0.2363
  21:  * @version       $Revision$
  22:  * @modifiedby    $LastChangedBy$
  23:  * @lastmodified  $Date$
  24:  * @license       http://www.opensource.org/licenses/mit-license.php The MIT License
  25:  */
  26: /**
  27:  * Configuration class (singleton). Used for managing runtime configuration information.
  28:  *
  29:  * @package       cake
  30:  * @subpackage    cake.cake.libs
  31:  * @link          http://book.cakephp.org/view/42/The-Configuration-Class
  32:  */
  33: class Configure extends Object {
  34: /**
  35:  * List of additional path(s) where model files reside.
  36:  *
  37:  * @var array
  38:  * @access public
  39:  */
  40:     var $modelPaths = array();
  41: /**
  42:  * List of additional path(s) where behavior files reside.
  43:  *
  44:  * @var array
  45:  * @access public
  46:  */
  47:     var $behaviorPaths = array();
  48: /**
  49:  * List of additional path(s) where controller files reside.
  50:  *
  51:  * @var array
  52:  * @access public
  53:  */
  54:     var $controllerPaths = array();
  55: /**
  56:  * List of additional path(s) where component files reside.
  57:  *
  58:  * @var array
  59:  * @access public
  60:  */
  61:     var $componentPaths = array();
  62: /**
  63:  * List of additional path(s) where view files reside.
  64:  *
  65:  * @var array
  66:  * @access public
  67:  */
  68:     var $viewPaths = array();
  69: /**
  70:  * List of additional path(s) where helper files reside.
  71:  *
  72:  * @var array
  73:  * @access public
  74:  */
  75:     var $helperPaths = array();
  76: /**
  77:  * List of additional path(s) where plugins reside.
  78:  *
  79:  * @var array
  80:  * @access public
  81:  */
  82:     var $pluginPaths = array();
  83: /**
  84:  * List of additional path(s) where vendor packages reside.
  85:  *
  86:  * @var array
  87:  * @access public
  88:  */
  89:     var $vendorPaths = array();
  90: /**
  91:  * List of additional path(s) where locale files reside.
  92:  *
  93:  * @var array
  94:  * @access public
  95:  */
  96:     var $localePaths = array();
  97: /**
  98:  * List of additional path(s) where console shell files reside.
  99:  *
 100:  * @var array
 101:  * @access public
 102:  */
 103:     var $shellPaths = array();
 104: /**
 105:  * Current debug level.
 106:  *
 107:  * @link          http://book.cakephp.org/view/44/CakePHP-Core-Configuration-Variables
 108:  * @var integer
 109:  * @access public
 110:  */
 111:     var $debug = null;
 112: /**
 113:  * Holds and key => value array of objects' types.
 114:  *
 115:  * @var array
 116:  * @access private
 117:  */
 118:     var $__objects = array();
 119: /**
 120:  * Returns a singleton instance of the Configure class.
 121:  *
 122:  * @return Configure instance
 123:  * @access public
 124:  */
 125:     function &getInstance($boot = true) {
 126:         static $instance = array();
 127:         if (!$instance) {
 128:             $instance[0] =& new Configure();
 129:             $instance[0]->__loadBootstrap($boot);
 130:         }
 131:         return $instance[0];
 132:     }
 133: /**
 134:  * Returns an index of objects of the given type, with the physical path to each object.
 135:  *
 136:  * @param string    $type Type of object, i.e. 'model', 'controller', 'helper', or 'plugin'
 137:  * @param mixed     $path Optional
 138:  * @return Configure instance
 139:  * @access public
 140:  */
 141:     function listObjects($type, $path = null, $cache = true) {
 142:         $objects = array();
 143:         $extension = false;
 144:         $name = $type;
 145: 
 146:         if ($type === 'file' && !$path) {
 147:             return false;
 148:         } elseif ($type === 'file') {
 149:             $extension = true;
 150:             $name = $type . str_replace(DS, '', $path);
 151:         }
 152:         $_this =& Configure::getInstance();
 153: 
 154:         if (empty($_this->__objects) && $cache === true) {
 155:             $_this->__objects = Cache::read('object_map', '_cake_core_');
 156:         }
 157: 
 158:         if (empty($_this->__objects) || !isset($_this->__objects[$type]) || $cache !== true) {
 159:             $types = array(
 160:                 'model' => array('suffix' => '.php', 'base' => 'AppModel', 'core' => false),
 161:                 'behavior' => array('suffix' => '.php', 'base' => 'ModelBehavior'),
 162:                 'controller' => array('suffix' => '_controller.php', 'base' => 'AppController'),
 163:                 'component' => array('suffix' => '.php', 'base' => null),
 164:                 'view' => array('suffix' => '.php', 'base' => null),
 165:                 'helper' => array('suffix' => '.php', 'base' => 'AppHelper'),
 166:                 'plugin' => array('suffix' => '', 'base' => null),
 167:                 'vendor' => array('suffix' => '', 'base' => null),
 168:                 'class' => array('suffix' => '.php', 'base' => null),
 169:                 'file' => array('suffix' => '.php', 'base' => null)
 170:             );
 171: 
 172:             if (!isset($types[$type])) {
 173:                 return false;
 174:             }
 175:             $objects = array();
 176: 
 177:             if (empty($path)) {
 178:                 $path = $_this->{$type . 'Paths'};
 179:                 if (isset($types[$type]['core']) && $types[$type]['core'] === false) {
 180:                     array_pop($path);
 181:                 }
 182:             }
 183:             $items = array();
 184: 
 185:             foreach ((array)$path as $dir) {
 186:                 if ($type === 'file' || $type === 'class' || strpos($dir, $type) !== false) {
 187:                     $items = $_this->__list($dir, $types[$type]['suffix'], $extension);
 188:                     $objects = array_merge($items, array_diff($objects, $items));
 189:                 }
 190:             }
 191: 
 192:             if ($type !== 'file') {
 193:                 foreach ($objects as $key => $value) {
 194:                     $objects[$key] = Inflector::camelize($value);
 195:                 }
 196:             }
 197:             if ($cache === true && !empty($objects)) {
 198:                 $_this->__objects[$name] = $objects;
 199:                 $_this->__resetCache(true);
 200:             } else {
 201:                 return $objects;
 202:             }
 203:         }
 204:         return $_this->__objects[$name];
 205:     }
 206: /**
 207:  * Returns an array of filenames of PHP files in the given directory.
 208:  *
 209:  * @param  string $path Path to scan for files
 210:  * @param  string $suffix if false, return only directories. if string, match and return files
 211:  * @return array  List of directories or files in directory
 212:  */
 213:     function __list($path, $suffix = false, $extension = false) {
 214:         if (!class_exists('Folder')) {
 215:             require LIBS . 'folder.php';
 216:         }
 217:         $items = array();
 218:         $Folder =& new Folder($path);
 219:         $contents = $Folder->read(false, true);
 220: 
 221:         if (is_array($contents)) {
 222:             if (!$suffix) {
 223:                 return $contents[0];
 224:             } else {
 225:                 foreach ($contents[1] as $item) {
 226:                     if (substr($item, - strlen($suffix)) === $suffix) {
 227:                         if ($extension) {
 228:                             $items[] = $item;
 229:                         } else {
 230:                             $items[] = substr($item, 0, strlen($item) - strlen($suffix));
 231:                         }
 232:                     }
 233:                 }
 234:             }
 235:         }
 236:         return $items;
 237:     }
 238: /**
 239:  * Used to store a dynamic variable in the Configure instance.
 240:  *
 241:  * Usage:
 242:  * {{{
 243:  * Configure::write('One.key1', 'value of the Configure::One[key1]');
 244:  * Configure::write(array('One.key1' => 'value of the Configure::One[key1]'));
 245:  * Configure::write('One', array(
 246:  *     'key1' => 'value of the Configure::One[key1]',
 247:  *     'key2' => 'value of the Configure::One[key2]'
 248:  * );
 249:  *
 250:  * Configure::write(array(
 251:  *     'One.key1' => 'value of the Configure::One[key1]',
 252:  *     'One.key2' => 'value of the Configure::One[key2]'
 253:  * ));
 254:  * }}}
 255:  *
 256:  * @link http://book.cakephp.org/view/412/write
 257:  * @param array $config Name of var to write
 258:  * @param mixed $value Value to set for var
 259:  * @return void
 260:  * @access public
 261:  */
 262:     function write($config, $value = null) {
 263:         $_this =& Configure::getInstance();
 264: 
 265:         if (!is_array($config)) {
 266:             $config = array($config => $value);
 267:         }
 268: 
 269:         foreach ($config as $names => $value) {
 270:             $name = $_this->__configVarNames($names);
 271: 
 272:             switch (count($name)) {
 273:                 case 3:
 274:                     $_this->{$name[0]}[$name[1]][$name[2]] = $value;
 275:                 break;
 276:                 case 2:
 277:                     $_this->{$name[0]}[$name[1]] = $value;
 278:                 break;
 279:                 case 1:
 280:                     $_this->{$name[0]} = $value;
 281:                 break;
 282:             }
 283:         }
 284: 
 285:         if (isset($config['debug'])) {
 286:             if ($_this->debug) {
 287:                 error_reporting(E_ALL & ~E_DEPRECATED & ~E_STRICT);
 288: 
 289:                 if (function_exists('ini_set')) {
 290:                     ini_set('display_errors', 1);
 291:                 }
 292: 
 293:                 if (!class_exists('Debugger')) {
 294:                     require LIBS . 'debugger.php';
 295:                 }
 296:                 if (!class_exists('CakeLog')) {
 297:                     require LIBS . 'cake_log.php';
 298:                 }
 299:                 Configure::write('log', LOG_NOTICE);
 300:             } else {
 301:                 error_reporting(0);
 302:                 Configure::write('log', LOG_NOTICE);
 303:             }
 304:         }
 305:     }
 306: /**
 307:  * Used to read information stored in the Configure instance.
 308:  *
 309:  * Usage
 310:  * Configure::read('Name'); will return all values for Name
 311:  * Configure::read('Name.key'); will return only the value of Configure::Name[key]
 312:  *
 313:  * @link          http://book.cakephp.org/view/413/read
 314:  * @param string $var Variable to obtain
 315:  * @return string value of Configure::$var
 316:  * @access public
 317:  */
 318:     function read($var = 'debug') {
 319:         $_this =& Configure::getInstance();
 320: 
 321:         if ($var === 'debug') {
 322:             if (!isset($_this->debug)) {
 323:                 if (defined('DEBUG')) {
 324:                     $_this->debug = DEBUG;
 325:                 } else {
 326:                     $_this->debug = 0;
 327:                 }
 328:             }
 329:             return $_this->debug;
 330:         }
 331:         $name = $_this->__configVarNames($var);
 332: 
 333:         switch (count($name)) {
 334:             case 3:
 335:                 if (isset($_this->{$name[0]}[$name[1]][$name[2]])) {
 336:                     return $_this->{$name[0]}[$name[1]][$name[2]];
 337:                 }
 338:             break;
 339:             case 2:
 340:                 if (isset($_this->{$name[0]}[$name[1]])) {
 341:                     return $_this->{$name[0]}[$name[1]];
 342:                 }
 343:             break;
 344:             case 1:
 345:                 if (isset($_this->{$name[0]})) {
 346:                     return $_this->{$name[0]};
 347:                 }
 348:             break;
 349:         }
 350:         return null;
 351:     }
 352: /**
 353:  * Used to delete a variable from the Configure instance.
 354:  *
 355:  * Usage:
 356:  * Configure::delete('Name'); will delete the entire Configure::Name
 357:  * Configure::delete('Name.key'); will delete only the Configure::Name[key]
 358:  *
 359:  * @link          http://book.cakephp.org/view/414/delete
 360:  * @param string $var the var to be deleted
 361:  * @return void
 362:  * @access public
 363:  */
 364:     function delete($var = null) {
 365:         $_this =& Configure::getInstance();
 366:         $name = $_this->__configVarNames($var);
 367: 
 368:         if (isset($name[1])) {
 369:             unset($_this->{$name[0]}[$name[1]]);
 370:         } else {
 371:             unset($_this->{$name[0]});
 372:         }
 373:     }
 374: /**
 375:  * Loads a file from app/config/configure_file.php.
 376:  * Config file variables should be formated like:
 377:  *  $config['name'] = 'value';
 378:  * These will be used to create dynamic Configure vars.
 379:  *
 380:  * Usage Configure::load('configure_file');
 381:  *
 382:  * @link          http://book.cakephp.org/view/415/load
 383:  * @param string $fileName name of file to load, extension must be .php and only the name
 384:  *                         should be used, not the extenstion
 385:  * @return mixed false if file not found, void if load successful
 386:  * @access public
 387:  */
 388:     function load($fileName) {
 389:         $found = false;
 390: 
 391:         if (file_exists(CONFIGS . $fileName . '.php')) {
 392:             include(CONFIGS . $fileName . '.php');
 393:             $found = true;
 394:         } elseif (file_exists(CACHE . 'persistent' . DS . $fileName . '.php')) {
 395:             include(CACHE . 'persistent' . DS . $fileName . '.php');
 396:             $found = true;
 397:         } else {
 398:             foreach (Configure::corePaths('cake') as $key => $path) {
 399:                 if (file_exists($path . DS . 'config' . DS . $fileName . '.php')) {
 400:                     include($path . DS . 'config' . DS . $fileName . '.php');
 401:                     $found = true;
 402:                     break;
 403:                 }
 404:             }
 405:         }
 406: 
 407:         if (!$found) {
 408:             return false;
 409:         }
 410: 
 411:         if (!isset($config)) {
 412:             $error = __("Configure::load() - no variable \$config found in %s.php", true);
 413:             trigger_error(sprintf($error, $fileName), E_USER_WARNING);
 414:             return false;
 415:         }
 416:         return Configure::write($config);
 417:     }
 418: /**
 419:  * Used to determine the current version of CakePHP.
 420:  *
 421:  * Usage Configure::version();
 422:  *
 423:  * @link          http://book.cakephp.org/view/416/version
 424:  * @return string Current version of CakePHP
 425:  * @access public
 426:  */
 427:     function version() {
 428:         $_this =& Configure::getInstance();
 429: 
 430:         if (!isset($_this->Cake['version'])) {
 431:             require(CORE_PATH . 'cake' . DS . 'config' . DS . 'config.php');
 432:             $_this->write($config);
 433:         }
 434:         return $_this->Cake['version'];
 435:     }
 436: /**
 437:  * Used to write a config file to disk.
 438:  *
 439:  * Configure::store('Model', 'class.paths', array('Users' => array(
 440:  *      'path' => 'users', 'plugin' => true
 441:  * )));
 442:  *
 443:  * @param string $type Type of config file to write, ex: Models, Controllers, Helpers, Components
 444:  * @param string $name file name.
 445:  * @param array $data array of values to store.
 446:  * @return void
 447:  * @access public
 448:  */
 449:     function store($type, $name, $data = array()) {
 450:         $write = true;
 451:         $content = '';
 452: 
 453:         foreach ($data as $key => $value) {
 454:             $content .= "\$config['$type']['$key'] = " . var_export($value, true) . ";\n";
 455:         }
 456:         if (is_null($type)) {
 457:             $write = false;
 458:         }
 459:         Configure::__writeConfig($content, $name, $write);
 460:     }
 461: /**
 462:  * Returns a key/value list of all paths where core libs are found.
 463:  * Passing $type only returns the values for a given value of $key.
 464:  *
 465:  * @param string $type valid values are: 'model', 'behavior', 'controller', 'component',
 466:  *                      'view', 'helper', 'datasource', 'libs', and 'cake'
 467:  * @return array numeric keyed array of core lib paths
 468:  * @access public
 469:  */
 470:     function corePaths($type = null) {
 471:         $paths = Cache::read('core_paths', '_cake_core_');
 472:         if (!$paths) {
 473:             $paths = array();
 474:             $openBasedir = ini_get('open_basedir');
 475:             if ($openBasedir) {
 476:                 $all = explode(PATH_SEPARATOR, $openBasedir);
 477:                 $all = array_flip(array_flip((array_merge(array(CAKE_CORE_INCLUDE_PATH), $all))));
 478:             } else {
 479:                 $all = explode(PATH_SEPARATOR, ini_get('include_path'));
 480:                 $all = array_flip(array_flip((array_merge(array(CAKE_CORE_INCLUDE_PATH), $all))));
 481:             }
 482:             foreach ($all as $path) {
 483:                 if ($path !== DS) {
 484:                     $path = rtrim($path, DS);
 485:                 }
 486:                 if (empty($path) || $path === '.') {
 487:                     continue;
 488:                 }
 489:                 $cake = $path .  DS . 'cake' . DS;
 490:                 $libs = $cake . 'libs' . DS;
 491:                 if (is_dir($libs)) {
 492:                     $paths['libs'][] = $libs;
 493:                     $paths['model'][] = $libs . 'model' . DS;
 494:                     $paths['behavior'][] = $libs . 'model' . DS . 'behaviors' . DS;
 495:                     $paths['controller'][] = $libs . 'controller' . DS;
 496:                     $paths['component'][] = $libs . 'controller' . DS . 'components' . DS;
 497:                     $paths['view'][] = $libs . 'view' . DS;
 498:                     $paths['helper'][] = $libs . 'view' . DS . 'helpers' . DS;
 499:                     $paths['cake'][] = $cake;
 500:                     $paths['vendor'][] = $path . DS . 'vendors' . DS;
 501:                     $paths['shell'][] = $cake . 'console' . DS . 'libs' . DS;
 502:                     break;
 503:                 }
 504:             }
 505:             Cache::write('core_paths', array_filter($paths), '_cake_core_');
 506:         }
 507:         if ($type && isset($paths[$type])) {
 508:             return $paths[$type];
 509:         }
 510:         return $paths;
 511:     }
 512: /**
 513:  * Creates a cached version of a configuration file.
 514:  * Appends values passed from Configure::store() to the cached file
 515:  *
 516:  * @param string $content Content to write on file
 517:  * @param string $name Name to use for cache file
 518:  * @param boolean $write true if content should be written, false otherwise
 519:  * @return void
 520:  * @access private
 521:  */
 522:     function __writeConfig($content, $name, $write = true) {
 523:         $file = CACHE . 'persistent' . DS . $name . '.php';
 524: 
 525:         if (Configure::read() > 0) {
 526:             $expires = "+10 seconds";
 527:         } else {
 528:             $expires = "+999 days";
 529:         }
 530:         $cache = cache('persistent' . DS . $name . '.php', null, $expires);
 531: 
 532:         if ($cache === null) {
 533:             cache('persistent' . DS . $name . '.php', "<?php\n\$config = array();\n", $expires);
 534:         }
 535: 
 536:         if ($write === true) {
 537:             if (!class_exists('File')) {
 538:                 require LIBS . 'file.php';
 539:             }
 540:             $fileClass = new File($file);
 541: 
 542:             if ($fileClass->writable()) {
 543:                 $fileClass->append($content);
 544:             }
 545:         }
 546:     }
 547: /**
 548:  * Checks $name for dot notation to create dynamic Configure::$var as an array when needed.
 549:  *
 550:  * @param mixed $name Name to split
 551:  * @return array Name separated in items through dot notation
 552:  * @access private
 553:  */
 554:     function __configVarNames($name) {
 555:         if (is_string($name)) {
 556:             if (strpos($name, ".")) {
 557:                 return explode(".", $name);
 558:             }
 559:             return array($name);
 560:         }
 561:         return $name;
 562:     }
 563: /**
 564:  * Build path references. Merges the supplied $paths
 565:  * with the base paths and the default core paths.
 566:  *
 567:  * @param array $paths paths defines in config/bootstrap.php
 568:  * @return void
 569:  * @access public
 570:  */
 571:     function buildPaths($paths) {
 572:         $_this =& Configure::getInstance();
 573:         $core = $_this->corePaths();
 574:         $basePaths = array(
 575:             'model' => array(MODELS),
 576:             'behavior' => array(BEHAVIORS),
 577:             'controller' => array(CONTROLLERS),
 578:             'component' => array(COMPONENTS),
 579:             'view' => array(VIEWS),
 580:             'helper' => array(HELPERS),
 581:             'plugin' => array(APP . 'plugins' . DS),
 582:             'vendor' => array(APP . 'vendors' . DS, VENDORS),
 583:             'locale' => array(APP . 'locale' . DS),
 584:             'shell' => array(),
 585:             'datasource' => array(MODELS . 'datasources')
 586:         );
 587: 
 588:         foreach ($basePaths as $type => $default) {
 589:             $pathsVar = $type . 'Paths';
 590:             $merge = array();
 591: 
 592:             if (isset($core[$type])) {
 593:                 $merge = $core[$type];
 594:             }
 595:             if ($type === 'model' || $type === 'controller' || $type === 'helper') {
 596:                 $merge = array_merge(array(APP), $merge);
 597:             }
 598: 
 599:             if (!is_array($default)) {
 600:                 $default = array($default);
 601:             }
 602:             $_this->{$pathsVar} = $default;
 603: 
 604:             if (isset($paths[$pathsVar]) && !empty($paths[$pathsVar])) {
 605:                 $path = array_flip(array_flip((array_merge(
 606:                     $_this->{$pathsVar}, (array)$paths[$pathsVar], $merge
 607:                 ))));
 608:                 $_this->{$pathsVar} = array_values($path);
 609:             } else {
 610:                 $path = array_flip(array_flip((array_merge($_this->{$pathsVar}, $merge))));
 611:                 $_this->{$pathsVar} = array_values($path);
 612:             }
 613:         }
 614:     }
 615: /**
 616:  * Loads app/config/bootstrap.php.
 617:  * If the alternative paths are set in this file
 618:  * they will be added to the paths vars.
 619:  *
 620:  * @param boolean $boot Load application bootstrap (if true)
 621:  * @return void
 622:  * @access private
 623:  */
 624:     function __loadBootstrap($boot) {
 625:         $modelPaths = $behaviorPaths = $controllerPaths = $componentPaths = $viewPaths = $helperPaths = $pluginPaths = $vendorPaths = $localePaths = $shellPaths = null;
 626: 
 627:         if ($boot) {
 628:             Configure::write('App', array('base' => false, 'baseUrl' => false, 'dir' => APP_DIR, 'webroot' => WEBROOT_DIR));
 629: 
 630:             if (!include(CONFIGS . 'core.php')) {
 631:                 trigger_error(sprintf(__("Can't find application core file. Please create %score.php, and make sure it is readable by PHP.", true), CONFIGS), E_USER_ERROR);
 632:             }
 633: 
 634:             if (Configure::read('Cache.disable') !== true) {
 635:                 $cache = Cache::config('default');
 636: 
 637:                 if (empty($cache['settings'])) {
 638:                     trigger_error('Cache not configured properly. Please check Cache::config(); in APP/config/core.php', E_USER_WARNING);
 639:                     $cache = Cache::config('default', array('engine' => 'File'));
 640:                 }
 641:                 $path = $prefix = $duration = null;
 642: 
 643:                 if (!empty($cache['settings']['path'])) {
 644:                     $path = realpath($cache['settings']['path']);
 645:                 } else {
 646:                     $prefix = $cache['settings']['prefix'];
 647:                 }
 648: 
 649:                 if (Configure::read() >= 1) {
 650:                     $duration = '+10 seconds';
 651:                 } else {
 652:                     $duration = '+999 days';
 653:                 }
 654: 
 655:                 if (Cache::config('_cake_core_') === false) {
 656:                     Cache::config('_cake_core_', array_merge($cache['settings'], array(
 657:                         'prefix' => $prefix . 'cake_core_', 'path' => $path . DS . 'persistent' . DS,
 658:                         'serialize' => true, 'duration' => $duration
 659:                     )));
 660:                 }
 661: 
 662:                 if (Cache::config('_cake_model_') === false) {
 663:                     Cache::config('_cake_model_', array_merge($cache['settings'], array(
 664:                         'prefix' => $prefix . 'cake_model_', 'path' => $path . DS . 'models' . DS,
 665:                         'serialize' => true, 'duration' => $duration
 666:                     )));
 667:                 }
 668:                 Cache::config('default');
 669:             }
 670: 
 671:             if (!include(CONFIGS . 'bootstrap.php')) {
 672:                 trigger_error(sprintf(__("Can't find application bootstrap file. Please create %sbootstrap.php, and make sure it is readable by PHP.", true), CONFIGS), E_USER_ERROR);
 673:             }
 674: 
 675:             Configure::buildPaths(compact(
 676:                 'modelPaths', 'viewPaths', 'controllerPaths', 'helperPaths', 'componentPaths',
 677:                 'behaviorPaths', 'pluginPaths', 'vendorPaths', 'localePaths', 'shellPaths'
 678:             ));
 679:         }
 680:     }
 681: /**
 682:  * Determines if $__objects cache should be reset.
 683:  *
 684:  * @param boolean $reset 
 685:  * @return boolean
 686:  * @access private
 687:  */ 
 688:     function __resetCache($reset = null) {
 689:         static $cache = array();
 690:         if (!$cache && $reset === true) {
 691:             $cache = true;  
 692:         }
 693:         return $cache;
 694:     }
 695: /**
 696:  * Caches the object map when the instance of the Configure class is destroyed
 697:  *
 698:  * @access public
 699:  */
 700:     function __destruct() {
 701:         if ($this->__resetCache() === true) {
 702:             Cache::write('object_map', array_filter($this->__objects), '_cake_core_');
 703:         }
 704:     }
 705: }
 706: /**
 707:  * Class and file loader.
 708:  *
 709:  * @link          http://book.cakephp.org/view/499/The-App-Class
 710:  * @since         CakePHP(tm) v 1.2.0.6001
 711:  * @package       cake
 712:  * @subpackage    cake.cake.libs
 713:  */
 714: class App extends Object {
 715: /**
 716:  * Paths to search for files.
 717:  *
 718:  * @var array
 719:  * @access public
 720:  */
 721:     var $search = array();
 722: /**
 723:  * Whether or not to return the file that is loaded.
 724:  *
 725:  * @var boolean
 726:  * @access public
 727:  */
 728:     var $return = false;
 729: /**
 730:  * Holds key/value pairs of $type => file path.
 731:  *
 732:  * @var array
 733:  * @access private
 734:  */
 735:     var $__map = array();
 736: /**
 737:  * Holds paths for deep searching of files.
 738:  *
 739:  * @var array
 740:  * @access private
 741:  */
 742:     var $__paths = array();
 743: /**
 744:  * Holds loaded files.
 745:  *
 746:  * @var array
 747:  * @access private
 748:  */
 749:     var $__loaded = array();
 750: /**
 751:  * Finds classes based on $name or specific file(s) to search.
 752:  *
 753:  * @link          http://book.cakephp.org/view/529/Using-App-import
 754:  * @param mixed $type The type of Class if passed as a string, or all params can be passed as
 755:  *                    an single array to $type,
 756:  * @param string $name Name of the Class or a unique name for the file
 757:  * @param mixed $parent boolean true if Class Parent should be searched, accepts key => value
 758:  *              array('parent' => $parent ,'file' => $file, 'search' => $search, 'ext' => '$ext');
 759:  *              $ext allows setting the extension of the file name
 760:  *              based on Inflector::underscore($name) . ".$ext";
 761:  * @param array $search paths to search for files, array('path 1', 'path 2', 'path 3');
 762:  * @param string $file full name of the file to search for including extension
 763:  * @param boolean $return, return the loaded file, the file must have a return
 764:  *                         statement in it to work: return $variable;
 765:  * @return boolean true if Class is already in memory or if file is found and loaded, false if not
 766:  * @access public
 767:  */
 768:     function import($type = null, $name = null, $parent = true, $search = array(), $file = null, $return = false) {
 769:         $plugin = $directory = null;
 770: 
 771:         if (is_array($type)) {
 772:             extract($type, EXTR_OVERWRITE);
 773:         }
 774: 
 775:         if (is_array($parent)) {
 776:             extract($parent, EXTR_OVERWRITE);
 777:         }
 778: 
 779:         if ($name === null && $file === null) {
 780:             $name = $type;
 781:             $type = 'Core';
 782:         } elseif ($name === null) {
 783:             $type = 'File';
 784:         }
 785: 
 786:         if (is_array($name)) {
 787:             foreach ($name as $class) {
 788:                 $tempType = $type;
 789:                 $plugin = null;
 790: 
 791:                 if (strpos($class, '.') !== false) {
 792:                     $value = explode('.', $class);
 793:                     $count = count($value);
 794: 
 795:                     if ($count > 2) {
 796:                         $tempType = $value[0];
 797:                         $plugin = $value[1] . '.';
 798:                         $class = $value[2];
 799:                     } elseif ($count === 2 && ($type === 'Core' || $type === 'File')) {
 800:                         $tempType = $value[0];
 801:                         $class = $value[1];
 802:                     } else {
 803:                         $plugin = $value[0] . '.';
 804:                         $class = $value[1];
 805:                     }
 806:                 }
 807: 
 808:                 if (!App::import($tempType, $plugin . $class)) {
 809:                     return false;
 810:                 }
 811:             }
 812:             return true;
 813:         }
 814: 
 815:         if ($name != null && strpos($name, '.') !== false) {
 816:             list($plugin, $name) = explode('.', $name);
 817:         }
 818:         $_this =& App::getInstance();
 819:         $_this->return = $return;
 820: 
 821:         if (isset($ext)) {
 822:             $file = Inflector::underscore($name) . ".$ext";
 823:         }
 824:         $ext = $_this->__settings($type, $plugin, $parent);
 825: 
 826:         if ($name != null && !class_exists($name . $ext['class'])) {
 827:             if ($load = $_this->__mapped($name . $ext['class'], $type, $plugin)) {
 828:                 if ($_this->__load($load)) {
 829:                     $_this->__overload($type, $name . $ext['class']);
 830: 
 831:                     if ($_this->return) {
 832:                         $value = include $load;
 833:                         return $value;
 834:                     }
 835:                     return true;
 836:                 } else {
 837:                     $_this->__remove($name . $ext['class'], $type, $plugin);
 838:                     $_this->__resetCache(true);
 839:                 }
 840:             }
 841:             if (!empty($search)) {
 842:                 $_this->search = $search;
 843:             } elseif ($plugin) {
 844:                 $_this->search = $_this->__paths('plugin');
 845:             } else {
 846:                 $_this->search = $_this->__paths($type);
 847:             }
 848:             $find = $file;
 849: 
 850:             if ($find === null) {
 851:                 $find = Inflector::underscore($name . $ext['suffix']).'.php';
 852: 
 853:                 if ($plugin) {
 854:                     $paths = $_this->search;
 855:                     foreach ($paths as $key => $value) {
 856:                         $_this->search[$key] = $value . $ext['path'];
 857:                     }
 858:                     $plugin = Inflector::camelize($plugin);
 859:                 }
 860:             }
 861: 
 862:             if (strtolower($type) !== 'vendor' && empty($search) && $_this->__load($file)) {
 863:                 $directory = false;
 864:             } else {
 865:                 $file = $find;
 866:                 $directory = $_this->__find($find, true);
 867:             }
 868: 
 869:             if ($directory !== null) {
 870:                 $_this->__resetCache(true);
 871:                 $_this->__map($directory . $file, $name . $ext['class'], $type, $plugin);
 872:                 $_this->__overload($type, $name . $ext['class']);
 873: 
 874:                 if ($_this->return) {
 875:                     $value = include $directory . $file;
 876:                     return $value;
 877:                 }
 878:                 return true;
 879:             }
 880:             return false;
 881:         }
 882:         return true;
 883:     }
 884: /**
 885:  * Returns a single instance of App.
 886:  *
 887:  * @return object
 888:  * @access public
 889:  */
 890:     function &getInstance() {
 891:         static $instance = array();
 892:         if (!$instance) {
 893:             $instance[0] =& new App();
 894:             $instance[0]->__map = Cache::read('file_map', '_cake_core_');
 895:         }
 896:         return $instance[0];
 897:     }
 898: /**
 899:  * Locates the $file in $__paths, searches recursively.
 900:  *
 901:  * @param string $file full file name
 902:  * @param boolean $recursive search $__paths recursively
 903:  * @return mixed boolean on fail, $file directory path on success
 904:  * @access private
 905:  */
 906:     function __find($file, $recursive = true) {
 907:         if (empty($this->search)) {
 908:             return null;
 909:         } elseif (is_string($this->search)) {
 910:             $this->search = array($this->search);
 911:         }
 912: 
 913:         if (empty($this->__paths)) {
 914:             $this->__paths = Cache::read('dir_map', '_cake_core_');
 915:         }
 916: 
 917:         foreach ($this->search as $path) {
 918:             $path = rtrim($path, DS);
 919: 
 920:             if ($path === rtrim(APP, DS)) {
 921:                 $recursive = false;
 922:             }
 923:             if ($recursive === false) {
 924:                 if ($this->__load($path . DS . $file)) {
 925:                     return $path . DS;
 926:                 }
 927:                 continue;
 928:             }
 929:             if (!isset($this->__paths[$path])) {
 930:                 if (!class_exists('Folder')) {
 931:                     require LIBS . 'folder.php';
 932:                 }
 933:                 $Folder =& new Folder();
 934:                 $directories = $Folder->tree($path, false, 'dir');
 935:                 $this->__paths[$path] = $directories;
 936:             }
 937: 
 938:             foreach ($this->__paths[$path] as $directory) {
 939:                 if ($this->__load($directory . DS . $file)) {
 940:                     return $directory . DS;
 941:                 }
 942:             }
 943:         }
 944:         return null;
 945:     }
 946: /**
 947:  * Attempts to load $file.
 948:  *
 949:  * @param string $file full path to file including file name
 950:  * @return boolean
 951:  * @access private
 952:  */
 953:     function __load($file) {
 954:         if (empty($file)) {
 955:             return false;
 956:         }
 957:         if (!$this->return && isset($this->__loaded[$file])) {
 958:             return true;
 959:         }
 960:         if (file_exists($file)) {
 961:             if (!$this->return) {
 962:                 require($file);
 963:                 $this->__loaded[$file] = true;
 964:             }
 965:             return true;
 966:         }
 967:         return false;
 968:     }
 969: /**
 970:  * Maps the $name to the $file.
 971:  *
 972:  * @param string $file full path to file
 973:  * @param string $name unique name for this map
 974:  * @param string $type type object being mapped
 975:  * @param string $plugin if object is from a plugin, the name of the plugin
 976:  * @access private
 977:  */
 978:     function __map($file, $name, $type, $plugin) {
 979:         if ($plugin) {
 980:             $plugin = Inflector::camelize($plugin);
 981:             $this->__map['Plugin'][$plugin][$type][$name] = $file;
 982:         } else {
 983:             $this->__map[$type][$name] = $file;
 984:         }
 985:     }
 986: /**
 987:  * Returns a file's complete path.
 988:  *
 989:  * @param string $name unique name
 990:  * @param string $type type object
 991:  * @param string $plugin if object is from a plugin, the name of the plugin
 992:  * @return mixed, file path if found, false otherwise
 993:  * @access private
 994:  */
 995:     function __mapped($name, $type, $plugin) {
 996:         if ($plugin) {
 997:             $plugin = Inflector::camelize($plugin);
 998: 
 999:             if (isset($this->__map['Plugin'][$plugin][$type]) && isset($this->__map['Plugin'][$plugin][$type][$name])) {
1000:                 return $this->__map['Plugin'][$plugin][$type][$name];
1001:             }
1002:             return false;
1003:         }
1004: 
1005:         if (isset($this->__map[$type]) && isset($this->__map[$type][$name])) {
1006:             return $this->__map[$type][$name];
1007:         }
1008:         return false;
1009:     }
1010: /**
1011:  * Used to overload objects as needed.
1012:  *
1013:  * @param string $type Model or Helper
1014:  * @param string $name Class name to overload
1015:  * @access private
1016:  */
1017:     function __overload($type, $name) {
1018:         if (($type === 'Model' || $type === 'Helper') && strtolower($name) != 'schema') {
1019:             Overloadable::overload($name);
1020:         }
1021:     }
1022: /**
1023:  * Loads parent classes based on $type.
1024:  * Returns a prefix or suffix needed for loading files.
1025:  *
1026:  * @param string $type type of object
1027:  * @param string $plugin name of plugin
1028:  * @param boolean $parent false will not attempt to load parent
1029:  * @return array
1030:  * @access private
1031:  */
1032:     function __settings($type, $plugin, $parent) {
1033:         if (!$parent) {
1034:             return null;
1035:         }
1036: 
1037:         if ($plugin) {
1038:             $plugin = Inflector::underscore($plugin);
1039:             $name = Inflector::camelize($plugin);
1040:         }
1041:         $path = null;
1042:         $load = strtolower($type);
1043: 
1044:         switch ($load) {
1045:             case 'model':
1046:                 if (!class_exists('Model')) {
1047:                     App::import('Core', 'Model', false, Configure::corePaths('model'));
1048:                 }
1049:                 if (!class_exists('AppModel')) {
1050:                     App::import($type, 'AppModel', false, Configure::read('modelPaths'));
1051:                 }
1052:                 if ($plugin) {
1053:                     if (!class_exists($name . 'AppModel')) {
1054:                         App::import($type, $plugin . '.' . $name . 'AppModel', false, array(), $plugin . DS . $plugin . '_app_model.php');
1055:                     }
1056:                     $path = $plugin . DS . 'models' . DS;
1057:                 }
1058:                 return array('class' => null, 'suffix' => null, 'path' => $path);
1059:             break;
1060:             case 'behavior':
1061:                 if ($plugin) {
1062:                     $path = $plugin . DS . 'models' . DS . 'behaviors' . DS;
1063:                 }
1064:                 return array('class' => $type, 'suffix' => null, 'path' => $path);
1065:             break;
1066:             case 'controller':
1067:                 App::import($type, 'AppController', false);
1068:                 if ($plugin) {
1069:                     App::import($type, $plugin . '.' . $name . 'AppController', false, array(), $plugin . DS . $plugin . '_app_controller.php');
1070:                     $path = $plugin . DS . 'controllers' . DS;
1071:                 }
1072:                 return array('class' => $type, 'suffix' => $type, 'path' => $path);
1073:             break;
1074:             case 'component':
1075:                 if ($plugin) {
1076:                     $path = $plugin . DS . 'controllers' . DS . 'components' . DS;
1077:                 }
1078:                 return array('class' => $type, 'suffix' => null, 'path' => $path);
1079:             break;
1080:             case 'view':
1081:                 if ($plugin) {
1082:                     $path = $plugin . DS . 'views' . DS;
1083:                 }
1084:                 return array('class' => $type, 'suffix' => null, 'path' => $path);
1085:             break;
1086:             case 'helper':
1087:                 if (!class_exists('AppHelper')) {
1088:                     App::import($type, 'AppHelper', false);
1089:                 }
1090:                 if ($plugin) {
1091:                     $path = $plugin . DS . 'views' . DS . 'helpers' . DS;
1092:                 }
1093:                 return array('class' => $type, 'suffix' => null, 'path' => $path);
1094:             break;
1095:             case 'vendor':
1096:                 if ($plugin) {
1097:                     $path = $plugin . DS . 'vendors' . DS;
1098:                 }
1099:                 return array('class' => null, 'suffix' => null, 'path' => $path);
1100:             break;
1101:             default:
1102:                 $type = $suffix = $path = null;
1103:             break;
1104:         }
1105:         return array('class' => null, 'suffix' => null, 'path' => null);
1106:     }
1107: /**
1108:  * Returns default search paths.
1109:  *
1110:  * @param string $type type of object to be searched
1111:  * @return array list of paths
1112:  * @access private
1113:  */
1114:     function __paths($type) {
1115:         $type = strtolower($type);
1116: 
1117:         if ($type === 'core') {
1118:             $path = Configure::corePaths();
1119:             $paths = array();
1120: 
1121:             foreach ($path as $key => $value) {
1122:                 $count = count($key);
1123:                 for ($i = 0; $i < $count; $i++) {
1124:                     $paths[] = $path[$key][$i];
1125:                 }
1126:             }
1127:             return $paths;
1128:         }
1129: 
1130:         if ($paths = Configure::read($type . 'Paths')) {
1131:             return $paths;
1132:         }
1133: 
1134:         switch ($type) {
1135:             case 'plugin':
1136:                 return array(APP . 'plugins' . DS);
1137:             case 'vendor':
1138:                 return array(APP . 'vendors' . DS, VENDORS, APP . 'plugins' . DS);
1139:             case 'controller':
1140:                 return array(APP . 'controllers' . DS, APP);
1141:             case 'model':
1142:                 return array(APP . 'models' . DS, APP);
1143:             case 'view':
1144:                 return array(APP . 'views' . DS);
1145:         }
1146:     }
1147: /**
1148:  * Removes file location from map if the file has been deleted.
1149:  *
1150:  * @param string $name name of object
1151:  * @param string $type type of object
1152:  * @param string $plugin name of plugin
1153:  * @return void
1154:  * @access private
1155:  */
1156:     function __remove($name, $type, $plugin) {
1157:         if ($plugin) {
1158:             $plugin = Inflector::camelize($plugin);
1159:             unset($this->__map['Plugin'][$plugin][$type][$name]);
1160:         } else {
1161:             unset($this->__map[$type][$name]);
1162:         }
1163:     }
1164: /**
1165:  * Determines if $__maps and $__paths cache should be reset.
1166:  *
1167:  * @param boolean $reset 
1168:  * @return boolean
1169:  * @access private
1170:  */ 
1171:     function __resetCache($reset = null) {
1172:         static $cache = array();
1173:         if (!$cache && $reset === true) {
1174:             $cache = true;  
1175:         }
1176:         return $cache;
1177:     }
1178: /**
1179:  * Object destructor.
1180:  *
1181:  * Writes cache file if changes have been made to the $__map or $__paths
1182:  *
1183:  * @return void
1184:  * @access private
1185:  */
1186:     function __destruct() {
1187:         if ($this->__resetCache() === true) {
1188:             $core = Configure::corePaths('cake');
1189:             unset($this->__paths[rtrim($core[0], DS)]);
1190:             Cache::write('dir_map', array_filter($this->__paths), '_cake_core_');
1191:             Cache::write('file_map', array_filter($this->__map), '_cake_core_');
1192:         }
1193:     }
1194: }
1195: ?>
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