/usr/share/php/Horde/View/Base.php is in php-horde-view 2.0.6-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 | <?php
/**
* @category Horde
* @package View
*/
/**
* Abstract base class for Horde_View to get private constructs out of
* template scope.
*
* @category Horde
* @package View
*/
abstract class Horde_View_Base
{
/**
* @var string
*/
public static $defaultFormBuilder = 'Horde_View_Helper_Form_Builder';
/**
* Path stack for templates.
*
* @var array
*/
private $_templatePath = array('./');
/**
* Template to execute.
*
* Stored in a private variable to keep it out of the public view scope.
*
* @var string
*/
private $_file = null;
/**
* Cache of helper objects.
*
* @var array
*/
private $_helpers = array();
/**
* Encoding to use in escaping mechanisms.
*
* @var string
*/
private $_encoding = 'UTF-8';
/**
* Should we throw an error if helper methods collide?
*
* @var boolean
*/
private $_throwOnHelperCollision = false;
/**
* Protected properties.
*
* @var array
*/
private $_protectedProperties;
/**
* Constructor.
*
* @param array $config Configuration key-value pairs.
*/
public function __construct($config = array())
{
// Encoding.
if (!empty($config['encoding'])) {
$this->setEncoding($config['encoding']);
}
// User-defined template path.
if (!empty($config['templatePath'])) {
$this->addTemplatePath($config['templatePath']);
}
$this->_protectedProperties = get_class_vars(__CLASS__);
}
/**
* Undefined variables return null.
*
* @return null
*/
public function __get($name)
{
return null;
}
/**
* Accesses a helper object from within a template.
*
* @param string $method The helper method.
* @param array $args The parameters for the helper.
*
* @return string The result of the helper method.
* @throws Horde_View_Exception
*/
public function __call($method, $args)
{
if (isset($this->_helpers[$method])) {
return call_user_func_array(array($this->_helpers[$method], $method), $args);
}
throw new Horde_View_Exception('Helper for ' . $method . ' not found.');
}
/**
* Adds to the stack of template paths in LIFO order.
*
* @param string|array The directory (-ies) to add.
*/
public function addTemplatePath($path)
{
foreach ((array)$path as $dir) {
// Attempt to strip any possible separator and append a
// directory separator.
$dir = rtrim($dir, '\\/' . DIRECTORY_SEPARATOR) . '/';
// Add to the top of the stack.
array_unshift($this->_templatePath, $dir);
}
}
/**
* Resets the stack of template paths.
*
* To clear all paths, use Horde_View::setTemplatePath(null).
*
* @param string|array The directory (-ies) to set as the path.
*/
public function setTemplatePath($path)
{
$this->_templatePath = array();
$this->addTemplatePath($path);
}
/**
* Returns the template paths.
*
* @return array The stack of current template paths.
*/
public function getTemplatePaths()
{
return $this->_templatePath;
}
/**
* Adds to the stack of helpers in LIFO order.
*
* If the $helper parameter is a string instead of a Helper instance, then
* it will be treated as a class name. Names without "_" and that do not
* have "Helper" in them will be prefixed with Horde_View_Helper_; other
* names will be treated as literal class names. Examples:
*
* <code>
* // Adds a new Horde_View_Helper_Tag to the view:
* $v->addHelper('Tag');
* // Adds a new AppHelper object to the view if it exists, otherwise
* // throws an exception:
* $v->addHelper('AppHelper');
* </code>
*
* @param Horde_View_Helper|string $helper The helper instance to add.
*
* @return Horde_View_Helper Returns the helper object that was added.
* @throws Horde_View_Exception
*/
public function addHelper($helper)
{
if (is_string($helper)) {
if (strpos($helper, '_') === false &&
strpos($helper, 'Helper') === false) {
$class = 'Horde_View_Helper_' . $helper;
} else {
$class = $helper;
}
if (!class_exists($class)) {
throw new Horde_View_Exception('Helper class ' . $helper . ' not found');
}
$helper = new $class($this);
}
foreach (get_class_methods($helper) as $method) {
if (isset($this->_helpers[$method])) {
$msg = 'Helper method ' . get_class($this->_helpers[$method])
. '#' . $method . ' overridden by ' . get_class($helper)
. '#' . $method;
if ($this->_throwOnHelperCollision) {
throw new Horde_View_Exception($msg);
}
if ($this->logger) {
$this->logger->warn($msg);
}
}
$this->_helpers[$method] = $helper;
}
return $helper;
}
/**
* Assigns multiple variables to the view.
*
* The array keys are used as names, each assigned their corresponding
* array value.
*
* @param array $array The array of key/value pairs to assign.
*/
public function assign($array)
{
foreach ($array as $key => $val) {
if (isset($this->_protectedProperties[$key])) {
throw new Horde_View_Exception('Cannot overwrite internal variables in assign()');
}
$this->$key = $val;
}
}
/**
* Processes a template and returns the output.
*
* @param string $name The template to process.
*
* @return string The template output.
*/
public function render($name, $locals = array())
{
// Render partial.
if (is_array($name) && $partial = $name['partial']) {
unset($name['partial']);
return $this->renderPartial($partial, $name);
}
// Find the template file name.
$this->_file = $this->_template($name);
// Remove $name from local scope.
unset($name);
ob_start();
$this->_run($this->_file, $locals);
return ob_get_clean();
}
/**
* Renders a partial template.
*
* Partial template filenames are named with a leading underscore, although
* this underscore is not used when specifying the name of the partial.
*
* We would reference the file /views/shared/_sidebarInfo.html in our
* template using:
*
* <code>
* <div>
* <?php echo $this->renderPartial('sidebarInfo') ?>
* </div>
* </code>
*
* @param string $name
* @param array $options
*
* @return string The template output.
*/
public function renderPartial($name, $options = array())
{
// Pop name off of the path.
$parts = strstr($name, '/') ? explode('/', $name) : array($name);
$name = array_pop($parts);
$path = count($parts)
? implode('/', $parts) . '/'
: '';
// Check if they passed in a collection before validating keys.
$useCollection = array_key_exists('collection', $options);
$valid = array('object' => null,
'locals' => array(),
'collection' => array());
$options = array_merge($valid, $options);
$locals = array($name => null);
// Set the object variable.
if ($options['object']) {
$locals[$name] = $options['object'];
}
// Set local variables to be used in the partial.
if (isset($options['locals']) &&
(is_array($options['locals']) ||
$options['locals'] instanceof Traversable)) {
foreach ($options['locals'] as $key => $val) {
$locals[$key] = $val;
}
}
if ($useCollection) {
// Collection.
$rendered = '';
if (is_array($options['collection'])) {
foreach (array_values($options['collection']) as $i => $item) {
$locals["{$name}Counter"] = $i;
$locals[$name] = $item;
$rendered .= $this->render("{$path}_{$name}", $locals);
}
}
} else {
// Single render.
$rendered = $this->render("{$path}_{$name}", $locals);
}
return $rendered;
}
/**
* Sets the output encoding.
*
* @param string $encoding A character set name.
*/
public function setEncoding($encoding)
{
$this->_encoding = $encoding;
}
/**
* Returns the current output encoding.
*
* @return string The current character set.
*/
public function getEncoding()
{
return $this->_encoding;
}
/**
* Controls the behavior when a helper method is overridden by another
* helper.
*
* @param boolean $throw Throw an exception when helper methods collide?
*/
public function throwOnHelperCollision($throw = true)
{
$this->_throwOnHelperCollision = $throw;
}
/**
* Finds a template from the available directories.
*
* @param $name string The base name of the template.
*
* @return string The full path to the first matching template.
*/
protected function _template($name)
{
// Append missing .html.
if (!strstr($name, '.')) {
$name .= '.html.php';
}
if (!count($this->_templatePath)) {
throw new Horde_View_Exception('No template directory set; unable to locate ' . $name);
}
foreach ($this->_templatePath as $dir) {
if (is_readable($dir . $name)) {
return $dir . $name;
}
}
throw new Horde_View_Exception("\"$name\" not found in template path (\"" . implode(':', $this->_templatePath) . '")');
}
/**
* Includes the template in a scope with only public variables.
*
* @param string The template to execute. Not declared in the function
* signature so it stays out of the view's public scope.
* @param array Any local variables to declare.
*/
abstract protected function _run();
}
|