Overview

Namespaces

  • None
  • PHP
  • Pry
    • Auth
      • Interfaces
    • Config
    • Controller
    • Date
    • Db
    • Feed
      • Abstracts
      • Writers
    • File
      • Decorator
    • Form
      • Element
    • Image
    • Log
      • Writer
    • Net
      • Exception
    • Session
    • Util
    • Validate
      • Validator
    • View

Classes

  • View
  • Overview
  • Namespace
  • Class
  • Tree
  1: <?php
  2: 
  3: /**
  4:  * Pry Framework
  5:  *
  6:  * LICENSE
  7:  *
  8:  * This source file is subject to the new BSD license that is bundled
  9:  * with this package in the file LICENSE.txt.
 10:  * 
 11:  */
 12: 
 13: namespace Pry\View;
 14: 
 15: /**
 16:  * Classe pour une gestion simple des vues
 17:  * @category Pry
 18:  * @package View
 19:  * @version 1.0
 20:  * @author Olivier ROGER <roger.olivier@gmail.com>
 21:  * @copyright  2007-2012 Prynel
 22:  *
 23:  */
 24: class View
 25: {
 26:     /** Ne pas echapper la variable */
 27: 
 28:     const NO_ESCAPE = 1;
 29: 
 30:     /**
 31:      * Ensemble des variables utilisable dans la vue
 32:      * @var array
 33:      */
 34:     protected $variables = array();
 35: 
 36:     /**
 37:      * Dossier où sont situer les vues
 38:      * @var string 
 39:      */
 40:     protected $viewFolder;
 41: 
 42:     /**
 43:      * Fichier vue à charger
 44:      * @var string 
 45:      */
 46:     protected $view;
 47: 
 48:     public function __construct()
 49:     {
 50:         
 51:     }
 52: 
 53:     /**
 54:      * Défini le dossier de base contenant les vues
 55:      * @param string $path
 56:      * @throws InvalidArgumentException Si le dossier n'existe pas
 57:      */
 58:     public function setViewBase($path)
 59:     {
 60:         if (!file_exists($path))
 61:             throw new \InvalidArgumentException('Can\'t find ' . $path . 'folder');
 62:         $this->viewFolder = $path;
 63:     }
 64: 
 65:     /**
 66:      * Défini la vue à charger
 67:      * @param string $filePath Chemin dans le dossier des vue
 68:      * @throws BadMethodCallException Si le dossier de base des vues n'est pas défini
 69:      * @throws InvalidArgumentException Si la vue n'existe pas
 70:      */
 71:     public function load($filePath)
 72:     {
 73:         if (empty($this->viewFolder))
 74:             throw new \BadMethodCallException('No view base defined.You should call setViewBase First.');
 75: 
 76:         $this->view = $filePath;
 77: 
 78:         if (!file_exists($this->viewFolder . $this->view))
 79:             throw new \InvalidArgumentException('Can\'t find the ' . $this->view . 'view');
 80:     }
 81: 
 82:     /**
 83:      * Défini une variable de vue. La variable sera ensuite utilisable dans la vue via sa clé
 84:      * @param string $key
 85:      * @param mixed $value
 86:      * @param int $option
 87:      */
 88:     public function set($key, $value, $option = null)
 89:     {
 90:         if ($option === self::NO_ESCAPE)
 91:             $this->variables[$key] = $value;
 92:         else
 93:             $this->variables[$key] = htmlspecialchars($value, ENT_QUOTES);
 94:     }
 95: 
 96:     /**
 97:      * Récupère une variable de vue
 98:      * @param type $key
 99:      * @return type
100:      * @throws InvalidArgumentException Si la clé n'existe pas
101:      */
102:     public function get($key)
103:     {
104:         if (!array_key_exists($key, $this->variables))
105:             throw new \InvalidArgumentException('No value for this key');
106: 
107:         return $this->variables[$key];
108:     }
109: 
110:     /**
111:      * Raccourcis pour définir une variable de vue.
112:      * Cette variable sera forcément échapée à l'affichage
113:      * @param type $name
114:      * @param type $value
115:      */
116:     public function __set($name, $value)
117:     {
118:         $this->set($name, $value, null);
119:     }
120: 
121:     /**
122:      * Raccourcis pour récupérer une variable de vue
123:      * @param type $name
124:      */
125:     public function __get($name)
126:     {
127:         $this->get($name);
128:     }
129: 
130:     /**
131:      * Affiche la vue
132:      * @throws BadMethodCallException Si la vue n'as pas été chargée
133:      */
134:     public function render()
135:     {
136:         if (empty($this->viewFolder))
137:             throw new \BadMethodCallException('PLease call load() before render()');
138:         extract($this->variables);
139:         include_once $this->viewFolder . $this->view;
140:     }
141: 
142: }
143: 
Pry API documentation generated by ApiGen 2.8.0