Overview

Namespaces

  • Thelia
    • Action
    • Config
    • Controller
    • Core
      • Bundle
      • DependencyInjection
        • Compiler
        • Loader
      • Event
      • EventListener
      • Template
        • BaseParam
    • Exception
    • Log
      • Destination
    • Model
      • map
      • om
    • Routing
      • Matcher
    • Tools
    • Tpex
      • BaseParam
      • Element
        • Loop
        • TestLoop
      • Event
      • Exception
      • Filter
      • Tokenizer

Classes

  • BaseElement
  • ConditionalLoopElement
  • ConditionalVarLoopElement
  • ElementCollection
  • RepeatLoopElement
  • SimpleLoopElement
  • TestLoopElement
  • TextElement
  • VariablesTemplate
  • Overview
  • Namespace
  • Class
  • Tree
  1: <?php
  2: /*************************************************************************************/
  3: /*                                                                                   */
  4: /*      Thelia                                                                       */
  5: /*                                                                                   */
  6: /*      Copyright (c) OpenStudio                                                     */
  7: /*  email : info@thelia.net                                                      */
  8: /*      web : http://www.thelia.net                                                  */
  9: /*                                                                                   */
 10: /*      This program is free software; you can redistribute it and/or modify         */
 11: /*      it under the terms of the GNU General Public License as published by         */
 12: /*      the Free Software Foundation; either version 3 of the License                */
 13: /*                                                                                   */
 14: /*      This program is distributed in the hope that it will be useful,              */
 15: /*      but WITHOUT ANY WARRANTY; without even the implied warranty of               */
 16: /*      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                */
 17: /*      GNU General Public License for more details.                                 */
 18: /*                                                                                   */
 19: /*      You should have received a copy of the GNU General Public License            */
 20: /*      along with this program. If not, see <http://www.gnu.org/licenses/>.     */
 21: /*                                                                                   */
 22: /*************************************************************************************/
 23: 
 24: namespace Thelia\Tpex\Element;
 25: 
 26: use Thelia\Tpex\Exception\TokenSyntaxException;
 27: use Symfony\Component\HttpFoundation\Request;
 28: 
 29: class VariablesTemplate
 30: {
 31:     /**
 32:      * @var \Thelia\Tpex\Element\VariablesTemplate
 33:      */
 34:     private static $instance = false;
 35: 
 36:     private $symboles = array();
 37: 
 38:     protected $filters = array();
 39: 
 40:     protected $init = false;
 41: 
 42:     protected $tokens = array();
 43: 
 44:     protected $sessionPrefix = "thelia_";
 45: 
 46:     /**
 47:      * @var \Symfony\Component\HttpFoundation\Request
 48:      */
 49:     protected $request;
 50: 
 51:     public static function init(array $filters, Request $request)
 52:     {
 53:         if(self::$instance === false) self::$instance = new VariablesTemplate();
 54: 
 55:         self::$instance->request = $request;
 56:         self::$instance->filters = $filters;
 57:     }
 58: 
 59:     /**
 60:      * @param $text
 61:      * @return mixed
 62:      * @throws \RuntimeException
 63:      */
 64:     public static function analyser($text)
 65:     {
 66:         if(self::$instance === false) throw new \RuntimeException("VariableInstance must be init with init static
 67:         method before using it");
 68: 
 69:         return self::$instance->start($text);
 70:     }
 71: 
 72:     protected function start($text)
 73:     {
 74:         $this->tokens = preg_split('/(\#|\{|\}|,)/', $text, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
 75:         array_unshift($this->tokens, '');
 76: 
 77:         return $this->analyse();
 78:     }
 79: 
 80:     protected function analyse($endChar = '')
 81:     {
 82:         $content = '';
 83: 
 84:         while (($tok = next($this->tokens)) !== false) {
 85:             if ($tok == "#") {
 86:                 $tok = next($this->tokens);
 87:                 if ($tok == "SET" || $tok == "SESSION_SET") {
 88:                     $this->analyseSet($tok);
 89:                 } elseif ($tok == "GET" || $tok == "ENV" || $tok == "SESSION") {
 90:                     $content .= $this->analyseGet($tok);
 91:                 } elseif (strpos($tok, "FILTER_") === 0) {
 92:                     $content .= $this->analyseFilter($tok);
 93:                 } else {
 94:                     $content .= "#".$tok;
 95:                 }
 96:             } elseif ($endChar != '' && $tok == $endChar) {
 97:                 break;
 98:             } else {
 99:                 $content .= $tok;
100:             }
101:         }
102: 
103:         return $content;
104:     }
105: 
106:     protected function analyseFilter($token)
107:     {
108:         /**
109:          * @TODO parse filters
110:          */
111:     }
112: 
113:     protected function analyseGet($token)
114:     {
115:         if (($tok = next($this->tokens)) == "{") {
116:             $var = $this->analyseVarName();
117: 
118:             if (($tok = next($this->tokens)) == ",") {
119:                 $default = $this->analyse("}");
120: 
121:                 return $this->filterGetVar($token, $var, $default);
122:             } elseif ($tok == "}") {
123:                 return $this->filterGetVar($token, $var);
124:             } else {
125:                 throw new TokenSyntaxException(sprintf("%s %s : need ',' or '}', %s found", $token, $var, $tok));
126:             }
127:         } else {
128:             return $tok;
129:         }
130:     }
131: 
132:     protected function filterGetVar($type, $var, $default = null)
133:     {
134:         switch ($type) {
135:             case "ENV":
136:                 return $this->request->get($var, $default);
137:                 break;
138:             case "GET":
139:                 return (isset($this->symboles[$var]) && $this->symboles[$var] != '')? $this->symboles[$var] : $default;
140:                 break;
141:             case "SESSION":
142:                 return $this->request->getSession()->get($this->sessionPrefix.$var, $default);
143:         }
144:     }
145: 
146:     protected function analyseSet($token)
147:     {
148:         $tok = next($this->tokens);
149: 
150:         if ($tok == "{") {
151:             $var = $this->analyseVarName();
152: 
153:             if (next($this->tokens) == ",") {
154:                 $val = $this->analyse('}');
155: 
156:                 if (current($this->tokens) == '}') {
157:                     switch ($token) {
158:                         case "SET":
159:                             $this->symboles[$var] = $val;
160:                             break;
161:                         case "SESSION_SET":
162:                             $this->request->getSession()->set($this->sessionPrefix.$var, $val);
163:                             break;
164:                     }
165: 
166:                     return '';
167:                 } else {
168:                     throw new TokenSyntaxException(sprintf("%s %s needed, %s found",$token, $var, $tok));
169:                 }
170:             } elseif (current($this->tokens) == "}") {
171:                 switch ($token) {
172:                     case "SET":
173:                         unset($this->symboles[$var]);
174:                         break;
175:                     case "SESSION_SET":
176:                         $this->request->getSession()->remove($this->sessionPrefix.$var);
177:                         break;
178:                 }
179: 
180:                 return '';
181:             }
182:         } else {
183:             return $tok;
184:         }
185:     }
186: 
187:     protected function analyseVarName()
188:     {
189:         $var = trim(next($this->tokens));
190: 
191:         if (! preg_match('/^[\w\:]+$/', $var)) {
192:             throw new TokenSyntaxException(sprintf("Invalid var name : %s", $var));
193:         }
194: 
195:         return $var;
196:     }
197: }
198: 
thelia API documentation generated by ApiGen 2.8.0