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

C CakePHP 2.1 API

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

Packages

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

Classes

  • CakeNumber
  • CakeTime
  • ClassRegistry
  • Debugger
  • File
  • Folder
  • Inflector
  • ObjectCollection
  • Sanitize
  • Security
  • Set
  • String
  • Validation
  • Xml
  1: <?php
  2: /**
  3:  * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4:  * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  5:  *
  6:  * Licensed under The MIT License
  7:  * Redistributions of files must retain the above copyright notice.
  8:  *
  9:  * @copyright     Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
 10:  * @link          http://cakephp.org CakePHP(tm) Project
 11:  * @package       Cake.Utility
 12:  * @since         CakePHP(tm) v 0.9.2
 13:  * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
 14:  */
 15: 
 16: /**
 17:  * Included libraries.
 18:  */
 19: App::uses('Model', 'Model');
 20: App::uses('AppModel', 'Model');
 21: App::uses('ConnectionManager', 'Model');
 22: 
 23: /**
 24:  * Class Collections.
 25:  *
 26:  * A repository for class objects, each registered with a key.
 27:  * If you try to add an object with the same key twice, nothing will come of it.
 28:  * If you need a second instance of an object, give it another key.
 29:  *
 30:  * @package       Cake.Utility
 31:  */
 32: class ClassRegistry {
 33: 
 34: /**
 35:  * Names of classes with their objects.
 36:  *
 37:  * @var array
 38:  */
 39:     protected $_objects = array();
 40: 
 41: /**
 42:  * Names of class names mapped to the object in the registry.
 43:  *
 44:  * @var array
 45:  */
 46:     protected $_map = array();
 47: 
 48: /**
 49:  * Default constructor parameter settings, indexed by type
 50:  *
 51:  * @var array
 52:  */
 53:     protected $_config = array();
 54: 
 55: /**
 56:  * Return a singleton instance of the ClassRegistry.
 57:  *
 58:  * @return ClassRegistry instance
 59:  */
 60:     public static function &getInstance() {
 61:         static $instance = array();
 62:         if (!$instance) {
 63:             $instance[0] = new ClassRegistry();
 64:         }
 65:         return $instance[0];
 66:     }
 67: 
 68: /**
 69:  * Loads a class, registers the object in the registry and returns instance of the object. ClassRegistry::init()
 70:  * is used as a factory for models, and handle correct injecting of settings, that assist in testing.
 71:  *
 72:  * Examples
 73:  * Simple Use: Get a Post model instance ```ClassRegistry::init('Post');```
 74:  *
 75:  * Expanded: ```array('class' => 'ClassName', 'alias' => 'AliasNameStoredInTheRegistry', 'type' => 'Model');```
 76:  *
 77:  * Model Classes can accept optional ```array('id' => $id, 'table' => $table, 'ds' => $ds, 'alias' => $alias);```
 78:  *
 79:  * When $class is a numeric keyed array, multiple class instances will be stored in the registry,
 80:  *  no instance of the object will be returned
 81:  * {{{
 82:  * array(
 83:  *      array('class' => 'ClassName', 'alias' => 'AliasNameStoredInTheRegistry'),
 84:  *      array('class' => 'ClassName', 'alias' => 'AliasNameStoredInTheRegistry'),
 85:  *      array('class' => 'ClassName', 'alias' => 'AliasNameStoredInTheRegistry')
 86:  * );
 87:  * }}}
 88:  * @param mixed $class as a string or a single key => value array instance will be created,
 89:  *  stored in the registry and returned.
 90:  * @param boolean $strict if set to true it will return false if the class was not found instead
 91:  *  of trying to create an AppModel
 92:  * @return object instance of ClassName.
 93:  * @throws CakeException when you try to construct an interface or abstract class.
 94:  */
 95:     public static function init($class, $strict = false) {
 96:         $_this = ClassRegistry::getInstance();
 97:         $false = false;
 98:         $true = true;
 99: 
100:         if (is_array($class)) {
101:             $objects = $class;
102:             if (!isset($class[0])) {
103:                 $objects = array($class);
104:             }
105:         } else {
106:             $objects = array(array('class' => $class));
107:         }
108:         $defaults = isset($_this->_config['Model']) ? $_this->_config['Model'] : array();
109:         $count = count($objects);
110:         $availableDs = array_keys(ConnectionManager::enumConnectionObjects());
111: 
112:         foreach ($objects as $key => $settings) {
113:             if (is_array($settings)) {
114:                 $pluginPath = null;
115:                 $settings = array_merge($defaults, $settings);
116:                 $class = $settings['class'];
117: 
118:                 list($plugin, $class) = pluginSplit($class);
119:                 if ($plugin) {
120:                     $pluginPath = $plugin . '.';
121:                 }
122: 
123:                 if (empty($settings['alias'])) {
124:                     $settings['alias'] = $class;
125:                 }
126:                 $alias = $settings['alias'];
127: 
128:                 if ($model = $_this->_duplicate($alias, $class)) {
129:                     $_this->map($alias, $class);
130:                     return $model;
131:                 }
132: 
133:                 App::uses($plugin . 'AppModel', $pluginPath . 'Model');
134:                 App::uses($class, $pluginPath . 'Model');
135: 
136:                 if (class_exists($class) || interface_exists($class)) {
137:                     $reflection = new ReflectionClass($class);
138:                     if ($reflection->isAbstract() || $reflection->isInterface()) {
139:                         throw new CakeException(__d('cake_dev', 'Cannot create instance of %s, as it is abstract or is an interface', $class));
140:                     }
141:                     $testing = isset($settings['testing']) ? $settings['testing'] : false;
142:                     if ($testing) {
143:                         $settings['ds'] = 'test';
144:                         $defaultProperties = $reflection->getDefaultProperties();
145:                         if (isset($defaultProperties['useDbConfig'])) {
146:                             $useDbConfig = $defaultProperties['useDbConfig'];
147:                             if (in_array('test_' . $useDbConfig, $availableDs)) {
148:                                 $useDbConfig = 'test_' . $useDbConfig;
149:                             }
150:                             if (strpos($useDbConfig, 'test') === 0) {
151:                                 $settings['ds'] = $useDbConfig;
152:                             }
153:                         }
154:                     }
155:                     if ($reflection->getConstructor()) {
156:                         $instance = $reflection->newInstance($settings);
157:                     } else {
158:                         $instance = $reflection->newInstance();
159:                     }
160:                     if ($strict) {
161:                         $instance = ($instance instanceof Model) ? $instance : null;
162:                     }
163:                 }
164:                 if (!isset($instance)) {
165:                     if ($strict) {
166:                         return false;
167:                     } elseif ($plugin && class_exists($plugin . 'AppModel')) {
168:                         $appModel = $plugin . 'AppModel';
169:                     } else {
170:                         $appModel = 'AppModel';
171:                     }
172:                     if (!empty($appModel)) {
173:                         $settings['name'] = $class;
174:                         $instance = new $appModel($settings);
175:                     }
176: 
177:                     if (!isset($instance)) {
178:                         trigger_error(__d('cake_dev', '(ClassRegistry::init() could not create instance of %1$s class %2$s ', $class, $type), E_USER_WARNING);
179:                         return $false;
180:                     }
181:                 }
182:                 $_this->map($alias, $class);
183:             } elseif (is_numeric($settings)) {
184:                 trigger_error(__d('cake_dev', '(ClassRegistry::init() Attempted to create instance of a class with a numeric name'), E_USER_WARNING);
185:                 return $false;
186:             }
187:         }
188: 
189:         if ($count > 1) {
190:             return $true;
191:         }
192:         return $instance;
193:     }
194: 
195: /**
196:  * Add $object to the registry, associating it with the name $key.
197:  *
198:  * @param string $key   Key for the object in registry
199:  * @param mixed $object Object to store
200:  * @return boolean True if the object was written, false if $key already exists
201:  */
202:     public static function addObject($key, $object) {
203:         $_this = ClassRegistry::getInstance();
204:         $key = Inflector::underscore($key);
205:         if (!isset($_this->_objects[$key])) {
206:             $_this->_objects[$key] = $object;
207:             return true;
208:         }
209:         return false;
210:     }
211: 
212: /**
213:  * Remove object which corresponds to given key.
214:  *
215:  * @param string $key   Key of object to remove from registry
216:  * @return void
217:  */
218:     public static function removeObject($key) {
219:         $_this = ClassRegistry::getInstance();
220:         $key = Inflector::underscore($key);
221:         if (isset($_this->_objects[$key])) {
222:             unset($_this->_objects[$key]);
223:         }
224:     }
225: 
226: /**
227:  * Returns true if given key is present in the ClassRegistry.
228:  *
229:  * @param string $key Key to look for
230:  * @return boolean true if key exists in registry, false otherwise
231:  */
232:     public static function isKeySet($key) {
233:         $_this = ClassRegistry::getInstance();
234:         $key = Inflector::underscore($key);
235:         if (isset($_this->_objects[$key])) {
236:             return true;
237:         } elseif (isset($_this->_map[$key])) {
238:             return true;
239:         }
240:         return false;
241:     }
242: 
243: /**
244:  * Get all keys from the registry.
245:  *
246:  * @return array Set of keys stored in registry
247:  */
248:     public static function keys() {
249:         $_this = ClassRegistry::getInstance();
250:         return array_keys($_this->_objects);
251:     }
252: 
253: /**
254:  * Return object which corresponds to given key.
255:  *
256:  * @param string $key Key of object to look for
257:  * @return mixed Object stored in registry or boolean false if the object does not exist.
258:  */
259:     public static function &getObject($key) {
260:         $_this = ClassRegistry::getInstance();
261:         $key = Inflector::underscore($key);
262:         $return = false;
263:         if (isset($_this->_objects[$key])) {
264:             $return = $_this->_objects[$key];
265:         } else {
266:             $key = $_this->_getMap($key);
267:             if (isset($_this->_objects[$key])) {
268:                 $return = $_this->_objects[$key];
269:             }
270:         }
271:         return $return;
272:     }
273: 
274: /**
275:  * Sets the default constructor parameter for an object type
276:  *
277:  * @param string $type Type of object.  If this parameter is omitted, defaults to "Model"
278:  * @param array $param The parameter that will be passed to object constructors when objects
279:  *                      of $type are created
280:  * @return mixed Void if $param is being set.  Otherwise, if only $type is passed, returns
281:  *               the previously-set value of $param, or null if not set.
282:  */
283:     public static function config($type, $param = array()) {
284:         $_this = ClassRegistry::getInstance();
285: 
286:         if (empty($param) && is_array($type)) {
287:             $param = $type;
288:             $type = 'Model';
289:         } elseif (is_null($param)) {
290:             unset($_this->_config[$type]);
291:         } elseif (empty($param) && is_string($type)) {
292:             return isset($_this->_config[$type]) ? $_this->_config[$type] : null;
293:         }
294:         if (isset($_this->_config[$type]['testing'])) {
295:             $param['testing'] = true;
296:         }
297:         $_this->_config[$type] = $param;
298:     }
299: 
300: /**
301:  * Checks to see if $alias is a duplicate $class Object
302:  *
303:  * @param string $alias
304:  * @param string $class
305:  * @return boolean
306:  */
307:     protected function &_duplicate($alias,  $class) {
308:         $duplicate = false;
309:         if ($this->isKeySet($alias)) {
310:             $model = $this->getObject($alias);
311:             if (is_object($model) && (is_a($model, $class) || $model->alias === $class)) {
312:                 $duplicate = $model;
313:             }
314:             unset($model);
315:         }
316:         return $duplicate;
317:     }
318: 
319: /**
320:  * Add a key name pair to the registry to map name to class in the registry.
321:  *
322:  * @param string $key Key to include in map
323:  * @param string $name Key that is being mapped
324:  * @return void
325:  */
326:     public static function map($key, $name) {
327:         $_this = ClassRegistry::getInstance();
328:         $key = Inflector::underscore($key);
329:         $name = Inflector::underscore($name);
330:         if (!isset($_this->_map[$key])) {
331:             $_this->_map[$key] = $name;
332:         }
333:     }
334: 
335: /**
336:  * Get all keys from the map in the registry.
337:  *
338:  * @return array Keys of registry's map
339:  */
340:     public static function mapKeys() {
341:         $_this = ClassRegistry::getInstance();
342:         return array_keys($_this->_map);
343:     }
344: 
345: /**
346:  * Return the name of a class in the registry.
347:  *
348:  * @param string $key Key to find in map
349:  * @return string Mapped value
350:  */
351:     protected function _getMap($key) {
352:         if (isset($this->_map[$key])) {
353:             return $this->_map[$key];
354:         }
355:     }
356: 
357: /**
358:  * Flushes all objects from the ClassRegistry.
359:  *
360:  * @return void
361:  */
362:     public static function flush() {
363:         $_this = ClassRegistry::getInstance();
364:         $_this->_objects = array();
365:         $_this->_map = array();
366:     }
367: 
368: }
369: 
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