Created
May 11, 2014 11:45
-
-
Save wilsonge/ec53ea62290d417b5c76 to your computer and use it in GitHub Desktop.
Model and View times
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Associative array of models | |
* stored as $models[$prefix][$name] used by get models | |
* @var array | |
*/ | |
protected $models = array(); | |
/** | |
* View to display | |
* @var JViewCms | |
*/ | |
protected $view; | |
public function getModel($prefix = null, $name = null, $config = array()) | |
{ | |
if (is_null($prefix)) | |
{ | |
$prefix = $this->getPrefix(); | |
} | |
if (is_null($name)) | |
{ | |
$name = $config['subject']; | |
} | |
$prefix = ucfirst($prefix); | |
$name = ucfirst($name); | |
if (isset($this->models[$prefix][$name])) | |
{ | |
return $this->models[$prefix][$name]; | |
} | |
$class = $prefix . 'Model' . $name; | |
if (!class_exists($class)) | |
{ | |
throw new RuntimeException(JText::sprintf('JLIB_APPLICATION_ERROR_MODELCLASS_NOT_FOUND', $class)); | |
} | |
$this->models[$prefix][$name] = new $class($config); | |
return $this->models[$prefix][$name]; | |
} | |
protected function getView($prefix = null, $name = null, $type, $config = array()) | |
{ | |
if (is_null($prefix)) | |
{ | |
$prefix = $this->getPrefix(); | |
} | |
if (is_null($name)) | |
{ | |
$name = $config['subject']; | |
} | |
$class = ucfirst($prefix) . 'View' . ucfirst($name) . ucfirst($type); | |
if ($this->view instanceof $class) | |
{ | |
return $this->view; | |
} | |
if (!class_exists($class)) | |
{ | |
$path = 'com_' . strtolower($prefix) . '/view'; | |
$path .= '/' . strtolower($name) . '/'; | |
throw new ErrorException(JText::sprintf('JLIB_APPLICATION_ERROR_VIEW_CLASS_NOT_FOUND', $class, $path), 500); | |
} | |
$config = $this->normalizeConfig($config); | |
$this->view = new $class($config); | |
return $this->view; | |
} | |
public function execute() | |
{ | |
// play around with the model | |
$this->getModel() | |
// play around with the view | |
$this->getView() | |
// This method won't need to initialize the model and view as it's being done | |
// in the getModel and getView and cached there | |
parent::execute(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment