~tsep-dev/tsep/0.9-beta

« back to all changes in this revision

Viewing changes to branches/symfony/cake/libs/configure.php

  • Committer: geoffreyfishing
  • Date: 2011-01-11 23:46:12 UTC
  • Revision ID: svn-v4:ae0de26e-ed09-4cbe-9a20-e40b4c60ac6c::125
Created a symfony branch for future migration to symfony

Show diffs side-by-side

added added

removed removed

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