Source of file View.php
Size: 7,792 Bytes - Last Modified: 2015-12-22T09:12:14-05:00
../src/View.php
1234567891011121314151617181920212223242526272829303132333435363738394041
Covered by 2 test(s):
42
Covered by 2 test(s):
43
Covered by 2 test(s):
44
Covered by 2 test(s):
45464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
| <?php /** * Base View Class * * @category PHP * @package MvcLite * @subpackage View * @since File available since release 1.0.1 * @author Cory Collier <corycollier@corycollier.com> */ namespace MvcLite; use MvcLite\Traits\Singleton as SingletonTrait; use MvcLite\Traits\Filepath as FilepathTrait; use MvcLite\Traits\Loader as LoaderTrait; use MvcLite\Traits\Request as RequestTrait; use MvcLite\Traits\Session as SessionTrait; use MvcLite\Traits\Config as ConfigTrait; /** * Base View Class * * @category PHP * @package MvcLite * @subpackage View * @since Class available since release 1.0.1 * @author Cory Collier <corycollier@corycollier.com> */ class View extends ObjectAbstract { use SingletonTrait; use FilepathTrait; use LoaderTrait; use RequestTrait; use SessionTrait; use ConfigTrait; /** * Constants */ const DEFAULT_FORMAT = 'html'; const ERR_BAD_HELPER_NAME = "Requested view helper [%s] could not be found"; const ERR_BAD_FORMAT = "The format given [%s] is not supported"; /** * Variables assigned to the view * * @var array */ protected $vars = []; /** * a list of previously loaded view helpers * * @var array */ protected $helpers = []; /** * The name of the view script to be used * * @var string */ protected $script; /** * The name of the layout script to be used * * @var string */ protected $layout; /** * The format type for the view * * @var string */ protected $format = self::DEFAULT_FORMAT; /** * The list of paths used to search for view scripts. * * @var array */ protected $viewScriptPaths = []; /** * method to start the view. * * @return MvcLite\View Return s$his for object-chaining. */ public function init() { $config = $this->getConfig(); $this->set('title', $config->get('app.title')); $settings = $config->getSection('layout'); foreach ($settings as $key => $value) { $this->set($key, $value); } $settings = $config->getSection('view'); foreach ($settings as $key => $value) { $this->set($key, $value); } return $this; } /** * Getter for the format. * * @return string The format for the view. */ public function getFormat() { return $this->format; } /** * Setter for the format. * * @param string $format The format for the view. * * @return MvcLite\View Returns $this for object-chaining. */ public function setFormat($format) { $this->format = $format; return $this; } /** * Method to add a path to the list of paths used to search for view scripts * * @param string $path * @return MvcLite\View $this for object-chaining. */ public function addViewScriptPath($path) { if (strpos($path, APP_PATH) === false) { $path = $this->filepath([APP_PATH, $path]); } $this->viewScriptPaths[] = $path; return $this; } /** * return the view script paths, reversed to enforce LIFO * * @return array */ public function getViewScriptPaths() { return array_reverse($this->viewScriptPaths); } /** * Method to set the script attrubute * * @param string $path * * @return MvcLite\View $this for object-chaining. */ public function setScript($path) { $this->script = (string)$path; return $this; } /** * Method to get the script attribute * * @return string the name of the view script to use */ public function getScript() { return $this->script; } /** * Method to set the layout attribute * * @param string $path * * @return MvcLite\View $this for object-chaining. */ public function setLayout($path) { $this->layout = (string)$path; return $this; } /** * Returns the layout script name * * @return string The name of the layout script to use */ public function getLayout() { return $this->layout; } /** * Gets the view script * * @return string The path to the view script. */ public function getViewScript() { $paths = $this->getViewScriptPaths(); $script = $this->getScript(); $format = $this->getFormat(); // iterate through the view paths foreach ($paths as $path) { $path = $this->filepath([$path, $script . '.' . $format . '.php']); if (file_exists($path)) { return $path; } } } /** * Returns the layout script; * @return string The filename of the layout script. */ public function getLayoutScript() { $format = $this->getFormat(); $layoutScript = $this->getLayout() . '.' . $format . '.php'; return $this->filepath([ APP_PATH, 'View', 'layouts', $layoutScript ]); } /** * Method to render the view * * @return string The result of the rendering */ public function render() { $format = $this->getFormat(); $script = $this->getScript(); $viewScript = $this->getViewScript(); $layoutScript = $this->getLayoutScript(); if (! $this->getScript() || ! $this->getViewScript()) { return null; } ob_start(); include $viewScript; $content = ob_get_clean(); $this->set('content', $content); // if there is no layout, then return the content if (! $layoutScript || $format !== 'html') { return $this->filter($content); } ob_start(); include $layoutScript; $contents = ob_get_clean(); return $this->filter($contents); } /** * Method to filter string input * * @param $string the unfiltered output * * @return string the filtered output */ public function filter($string) { return $string; } /** * Setter for the _vars property. * * @param string $var * @param unknown_type $value * * @return MvcLite\View $this for object-chaining. */ public function set($var, $value = '') { $this->vars[$var] = $value; return $this; } /** * getter for the _vars property * * @param string $var * * @return mixed */ public function get($var) { if (array_key_exists($var, $this->vars)) { return $this->vars[$var]; } } /** * getter for a view helper instance * * @param string $name * * @return MvcLite\View_Helper */ public function getHelper($name) { $loader = $this->getLoader(); // if the helper has already been loaded, just return the instance if (@$this->helpers[$name]) { return $this->helpers[$name]; } foreach (['App', 'MvcLite'] as $library) { // create the full class name $className = "\\{$library}\\View\\Helper\\" . ucfirst("{$name}"); if ($loader->loadClass($className)) { $this->helpers[$name] = new $className($this); return $this->helpers[$name]; } } // throw an exception if we get this far throw new Exception(sprintf(self::ERR_BAD_HELPER_NAME, $name)); } } |