/** * Loads and instantiates models required by this controller. * If Controller::$persistModel; is true, controller will cache model instances on first request, * additional request will used cached models. * If the model is non existent, it will throw a missing database table error, as Cake generates * dynamic models for the time being. * * @param string $modelClass Name of model class to load * @param mixed $id Initial ID the instanced model class should have * @return mixed true when single model found and instance created, error returned if model not found. * @return bool */ public function loadModel($modelClass, $id = null) { $cached = false; $object = null; list($plugin, $modelClass) = pluginSplit($modelClass, true, null); if (isset($this->_internals['persistModel']) && $this->_internals['persistModel'] === true) { $cached = $this->_persist($modelClass, null, $object); } if ($cached === false) { $this->_internals['modelNames'][] = $modelClass; $this->{$modelClass} = ClassRegistry::init(array('class' => $plugin . $modelClass, 'alias' => $modelClass, 'id' => $id)); if (!$this->{$modelClass}) { $message = sprintf("Missing Model: %s", $modelClass); if (!empty($plugin)) { $message .= sprintf(" in %s Plugin", substr($plugin, 0, -1)); } throw new Exception($message); } if (isset($this->_internals['persistModel']) && $this->_internals['persistModel'] === true) { $this->_persist($modelClass, true, $this->{$modelClass}); $registry =& ClassRegistry::getInstance(); $this->_persist($modelClass . 'registry', true, $registry->__objects, 'registry'); } } else { $this->_persist($modelClass . 'registry', true, $object, 'registry'); $this->_persist($modelClass, true, $object); $this->_internals['modelNames'][] = $modelClass; } return true; }
/** * Load the model and assign it to the controller * * @access public * @param string $modelClass * @return bool | mixed */ function load($modelClass, $objectName = null) { if (!$this->controller) { return false; } $id = false; $object = null; $cached = false; $plugin = ''; if ($this->controller->plugin) { $plugin = $this->controller->plugin . '.'; } if (!class_exists($modelClass)) { $this->controller->loadModel($plugin . $modelClass); } if (class_exists($modelClass)) { if (!$objectName) { $objectName = $modelClass; } if ($this->controller->persistModel) { $cached = $this->_persist($modelClass, null, $object); } if (false === $cached) { $model =& new $modelClass($id); $this->controller->modelNames[] = $modelClass; $this->controller->{$objectName} =& $model; if ($this->controller->persistModel === true) { $this->_persist($modelClass, true, $model); $registry = ClassRegistry::getInstance(); $this->_persist($modelClass . 'registry', true, $registry->__objects, 'registry'); } } else { $this->_persist($modelClass . 'registry', true, $object, 'registry'); $this->_persist($modelClass, true, $object); $this->controller->{$objectName} = $this->{$modelClass}; // unset the temp model, for PHP4 unset($this->{$modelClass}); $this->controller->modelNames[] = $modelClass; } } else { return $this->controller->cakeError('missingModel', array(array('className' => $modelClass, 'webroot' => '', 'base' => $this->controller->base))); } return true; }
/** * Flushes all objects from the ClassRegistry. * * @return void */ public static function flush() { $_this = ClassRegistry::getInstance(); $_this->_objects = array(); $_this->_map = array(); }
/** * Flushes all objects from the ClassRegistry. * * @return void * @access public * @static */ function flush() { $_this =& ClassRegistry::getInstance(); $_this->__objects = array(); $_this->__map = array(); }
/** * Loads and instantiates models required by this controller. * If Controller::persistModel; is true, controller will cache model instances on first request, * additional request will used cached models. * If the model is non existent, it will throw a missing database table error, as Cake generates * dynamic models for the time being. * * @param string $modelClass Name of model class to load * @param mixed $id Initial ID the instanced model class should have * @return mixed true when single model found and instance created error returned if models not found. * @access public */ function loadModel($modelClass = null, $id = null) { if ($modelClass === null) { $modelClass = $this->modelClass; } $cached = false; $object = null; $plugin = null; if ($this->uses === false) { if ($this->plugin) { $plugin = $this->plugin . '.'; } } if (strpos($modelClass, '.') !== false) { list($plugin, $modelClass) = explode('.', $modelClass); $plugin = $plugin . '.'; } if ($this->persistModel === true) { $cached = $this->_persist($modelClass, null, $object); } if ($cached === false) { $this->modelNames[] = $modelClass; if (!PHP5) { $this->{$modelClass} =& ClassRegistry::init(array('class' => $plugin . $modelClass, 'alias' => $modelClass, 'id' => $id)); } else { $this->{$modelClass} = ClassRegistry::init(array('class' => $plugin . $modelClass, 'alias' => $modelClass, 'id' => $id)); } if (!$this->{$modelClass}) { return $this->cakeError('missingModel', array(array('className' => $modelClass, 'webroot' => '', 'base' => $this->base))); } if ($this->persistModel === true) { $this->_persist($modelClass, true, $this->{$modelClass}); $registry = ClassRegistry::getInstance(); $this->_persist($modelClass . 'registry', true, $registry->__objects, 'registry'); } } else { $this->_persist($modelClass . 'registry', true, $object, 'registry'); $this->_persist($modelClass, true, $object); $this->modelNames[] = $modelClass; } }
function endTest() { FFactory::clean(); ClassRegistry::getInstance()->flush(); }
/** * Callback issued when a controller's action is about to be invoked through testAction(). * * @param Controller $controller Controller that's about to be invoked. * @param array $params Additional parameters as sent by testAction(). */ function startController(&$controller, $params = array()) { if (isset($params['fixturize']) && (is_array($params['fixturize']) && !empty($params['fixturize']) || $params['fixturize'] === true)) { if (!isset($this->db)) { $this->_initDb(); } $classRegistry =& ClassRegistry::getInstance(); $models = array(); foreach ($classRegistry->__map as $key => $name) { $object =& $classRegistry->getObject(Inflector::camelize($key)); if (is_subclass_of($object, 'Model') && (is_array($params['fixturize']) && in_array($object->alias, $params['fixturize']) || $params['fixturize'] === true)) { $models[$object->alias] = array('table' => $object->table, 'model' => $object->alias, 'key' => Inflector::camelize($key)); } } if (!empty($models) && isset($this->db)) { $this->_queries = array('create' => array(), 'insert' => array(), 'drop' => array()); foreach ($models as $model) { $fixture =& new CakeTestFixture($this->db); $fixture->name = $model['model'] . 'Test'; $fixture->table = $model['table']; $fixture->import = array('model' => $model['model'], 'records' => true); $fixture->init(); $createFixture = $fixture->create($this->db); $insertsFixture = $fixture->insert(); $dropFixture = $fixture->drop(); if (!empty($createFixture)) { $this->_queries['create'] = array_merge($this->_queries['create'], array($createFixture)); } if (!empty($insertsFixture)) { $this->_queries['insert'] = array_merge($this->_queries['insert'], $insertsFixture); } if (!empty($dropFixture)) { $this->_queries['drop'] = array_merge($this->_queries['drop'], array($dropFixture)); } } foreach ($this->_queries['create'] as $query) { if (isset($query) && $query !== false) { $this->db->execute($query); } } foreach ($this->_queries['insert'] as $query) { if (isset($query) && $query !== false) { $this->db->execute($query); } } foreach ($models as $model) { $object =& $classRegistry->getObject($model['key']); if ($object !== false) { $object->setDataSource('test_suite'); $object->cacheSources = false; } } } } }
/** * Loads and instantiates models required by this controller. * If Controller::persistModel; is true, controller will create cached model instances on first request, * additional request will used cached models * * @return mixed true when single model found and instance created error returned if models not found. * @access public */ function constructClasses() { if ($this->uses === null || $this->uses === array()) { return false; } if (empty($this->passedArgs) || !isset($this->passedArgs['0'])) { $id = false; } else { $id = $this->passedArgs['0']; } $cached = false; $object = null; if ($this->persistModel === true) { uses('neat_array'); } if ($this->uses === false) { if (!class_exists($this->modelClass)) { loadModel($this->modelClass); } } if (class_exists($this->modelClass) && $this->uses === false) { if ($this->persistModel === true) { $cached = $this->_persist($this->modelClass, null, $object); } if ($cached === false) { $model =& new $this->modelClass($id); $this->modelNames[] = $this->modelClass; $this->{$this->modelClass} =& $model; if ($this->persistModel === true) { $this->_persist($this->modelClass, true, $model); $registry = ClassRegistry::getInstance(); $this->_persist($this->modelClass . 'registry', true, $registry->_objects, 'registry'); } } else { $this->_persist($this->modelClass . 'registry', true, $object, 'registry'); $this->_persist($this->modelClass, true, $object); $this->modelNames[] = $this->modelClass; } return true; } elseif ($this->uses === false) { return $this->cakeError('missingModel', array(array('className' => $this->modelClass, 'webroot' => '', 'base' => $this->base))); } if ($this->uses) { $uses = is_array($this->uses) ? $this->uses : array($this->uses); $this->modelClass = $uses[0]; foreach ($uses as $modelClass) { $id = false; $cached = false; $object = null; $modelKey = Inflector::underscore($modelClass); if (!class_exists($modelClass)) { loadModel($modelClass); } if (class_exists($modelClass)) { if ($this->persistModel === true) { $cached = $this->_persist($modelClass, null, $object); } if ($cached === false) { $model =& new $modelClass($id); $this->modelNames[] = $modelClass; $this->{$modelClass} =& $model; if ($this->persistModel === true) { $this->_persist($modelClass, true, $model); $registry = ClassRegistry::getInstance(); $this->_persist($modelClass . 'registry', true, $registry->_objects, 'registry'); } } else { $this->_persist($modelClass . 'registry', true, $object, 'registry'); $this->_persist($modelClass, true, $object); $this->modelNames[] = $modelClass; } } else { return $this->cakeError('missingModel', array(array('className' => $modelClass, 'webroot' => '', 'base' => $this->base))); } } return true; } }
/** * Limpa todos os objetos instanciados do registro. * * @return boolean true */ public static function flush() { $self =& ClassRegistry::getInstance(); $self->objects = array(); return true; }
/** * Executes a Cake URL, optionally getting the view rendering or what is returned * when params['requested'] is set. * * @param string $url Cake URL to execute (e.g: /articles/view/455) * @param array $params Parameters * * @return mixed What is returned from action (if $requested is true), or view rendered html * * @access protected */ function testAction($url, $params = array()) { $default = array('return' => 'result', 'fixturize' => false, 'data' => array(), 'method' => 'post'); $params = am($default, $params); if (!empty($params['data'])) { $data = array('data' => $params['data']); if (low($params['method']) == 'get') { $_GET = $data; } else { $_POST = $data; } } $return = $params['return']; unset($params['data']); unset($params['method']); unset($params['return']); $dispatcher =& new CakeTestDispatcher(); $dispatcher->testCase($this); if (low($return) != 'result') { $params['return'] = 0; ob_start(); @$dispatcher->dispatch($url, $params); $result = ob_get_clean(); if (low($return) == 'vars') { $view =& ClassRegistry::getObject('view'); $viewVars = $view->getVars(); $result = array(); foreach ($viewVars as $var) { $result[$var] = $view->getVar($var); } if (!empty($view->pageTitle)) { $result = am($result, array('title' => $view->pageTitle)); } } } else { $params['return'] = 1; $params['bare'] = 1; $params['requested'] = 1; $result = @$dispatcher->dispatch($url, $params); } $classRegistry =& ClassRegistry::getInstance(); $keys = array_keys($classRegistry->__objects); foreach ($keys as $key) { $key = Inflector::camelize($key); $classRegistry->removeObject($key); } $classRegistry->__map = array(); if (isset($this->_queries)) { unset($this->_queries); } return $result; }
/** * Return object which corresponds to given key. * * @param string $key * @return mixed */ function &getObject($key) { $_this =& ClassRegistry::getInstance(); $key = strtolower($key); return $_this->_objects[$key]; }
/** * Return object which corresponds to given key. * * @param string $key * @return mixed */ function &getObject($key) { $_this =& ClassRegistry::getInstance(); $key = Inflector::underscore($key); return $_this->_objects[$key]; }
/** * Loads and instantiates models required by this controller. * If Controller::$persistModel; is true, controller will cache model instances on first request, * additional request will used cached models. * If the model is non existent, it will throw a missing database table error, as Cake generates * dynamic models for the time being. * * @param string $modelClass Name of model class to load * @param mixed $id Initial ID the instanced model class should have * @return mixed true when single model found and instance created, error returned if model not found. * @throws MissingModelException if the model class cannot be found. */ public function loadModel($modelClass = null, $id = null) { if ($modelClass === null) { $modelClass = $this->modelClass; } $cached = false; $object = null; list($plugin, $modelClass) = pluginSplit($modelClass, true); if ($this->persistModel === true) { $cached = $this->_persist($modelClass, null, $object); } if ($cached === false) { $this->modelNames[] = $modelClass; $this->{$modelClass} = ClassRegistry::init(array('class' => $plugin . $modelClass, 'alias' => $modelClass, 'id' => $id)); if (!$this->{$modelClass}) { throw new MissingModelException($modelClass); } if ($this->persistModel === true) { $this->_persist($modelClass, true, $this->{$modelClass}); $registry = ClassRegistry::getInstance(); $this->_persist($modelClass . 'registry', true, $registry->__objects, 'registry'); } } else { $this->_persist($modelClass . 'registry', true, $object, 'registry'); $this->_persist($modelClass, true, $object); $this->modelNames[] = $modelClass; } return true; }
/** * Loads and instantiates models required by this controller. * If Controller::persistModel; is true, controller will create cached model instances on first request, * additional request will used cached models * * @param string $modelClass Name of model class to load * @param mixed $id Initial ID the instanced model class should have * @return mixed true when single model found and instance created error returned if models not found. * @access public */ function loadModel($modelClass = null, $id = false) { if ($modelClass === null) { $modelClass = $this->modelClass; } $cached = false; $object = null; $plugin = null; if ($this->plugin) { $plugin = $this->plugin . '.'; } $modelKey = Inflector::underscore($modelClass); if (!class_exists($modelClass)) { loadModel($plugin . $modelClass); } if (class_exists($modelClass)) { if ($this->persistModel === true) { $cached = $this->_persist($modelClass, null, $object); } if ($cached === false) { $model =& new $modelClass($id); $this->modelNames[] = $modelClass; $this->{$modelClass} =& $model; if ($this->persistModel === true) { $this->_persist($modelClass, true, $model); $registry = ClassRegistry::getInstance(); $this->_persist($modelClass . 'registry', true, $registry->__objects, 'registry'); } } else { $this->_persist($modelClass . 'registry', true, $object, 'registry'); $this->_persist($modelClass, true, $object); $this->modelNames[] = $modelClass; } } else { return $this->cakeError('missingModel', array(array('className' => $modelClass, 'webroot' => '', 'base' => $this->base))); } }