cvs: pear /Template/Template Source.php
| From: | Alan Knowles | Date: | Wed, 16 Jun 2004 15:42:05 +0000 |
| Subject: | cvs: pear /Template/Template Source.php | ||
| Groups: | php.pear.cvs | ||
| Request: | Send a blank email to pear-cvs+get-20740@lists.php.net to get a copy of this message | ||
alan_k Wed Jun 16 11:42:05 2004 EDT
Added files:
/pear/Template/Template Source.php
Log:
move Multisource to source - generic unusual source handler
http://cvs.php.net/co.php/pear/Template/Template/Source.php?r=1.1&p=1
Index: pear/Template/Template/Source.php
+++ pear/Template/Template/Source.php
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4: */
// +----------------------------------------------------------------------+
// | PHP Version 4 |
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2002 The PHP Group |
// +----------------------------------------------------------------------+
// | This source file is subject to version 2.02 of the PHP license, |
// | that is bundled with this package in the file LICENSE, and is |
// | available at through the world-wide-web at |
// | http://www.php.net/license/2_02.txt.
|
// | If you did not receive a copy of the PHP license and are unable to |
// | obtain it through the world-wide-web, please send a note to |
// | license@php.net so we can mail you a copy immediately. |
// +----------------------------------------------------------------------+
// | Authors: alan@akkbhome.com |
// +----------------------------------------------------------------------+
//
// $Id: Source.php,v 1.1 2004/06/16 15:42:05 alan_k Exp $
//
// Source Provider - deals with
// a) mulitple paths for the source of a template
// b) different source types (eg. file handles, template source, object providers?)
//
class Template_Source
{
/**
* Default options related to this provider.
*
* @var array
* @access public
*/
var $options = array(
'source.muliple_order' => null, // use 'reverse' - to sort
backwards..
'source.muliple_allow' => false, // set to true to allow the same
// file to exist in multiple sources
// eg for themes.
);
/**
* A reference to the instantated template.
*
* @var object Template
* @access public
*/
var $template = null;
/**
* initialize the Provider.
*
* @param object Template The instantated template.
*
* @return boolean (it may return a PEAR::Error)
* @access public
* @see Template_Provider
*/
function initProvider(&$template)
{
// if you need to access template variables (Eg. options/data/providers)
// you should store a reference to the template object.
$this->template = &$template;
// if you add default options, merge your options.
$this->template->options =
array_merge($this->options,$this->template->options);
return true;
}
/**
* Find the location of the requested file.
*
* Looks in :
* a) {options[template_dir][*]}/templatename.{locale}.{ext}
* b) {options[template_dir][*]}/templatename
*
*
* @param string name of file.
*
* @return boolean true || pear error on failure.
* @access public
*/
function open($file) {
switch (get_type($file)) {
case 'string':
$options = $this->template->options;
$files = &$this->template->options;
//Remove the slash if there is one in front, just to be safe.
$file = ltrim($file,DIRECTORY_SEPARATOR);
$files['current_template'] = false;
$this->resolveLocale( $file );
// found it already! ( a localized version )
if ($files['current_template']) {
return true;
}
$this->resolveMultiPath();
if ($files['currentTemplate'] === false) {
return $this->template->raiseError(
"Could not find Template %s in any of your paths (%s)" .
"Than once, (in %s and %s)",
TEMPLATE_ERROR_INVALIDARGS, TEMPLATE_ERROR_DIE,
array($file, implode(PATH_SEPERATOR,
$options['template_dir'])));
}
return true;
case 'array': // source included..
return false;
case 'resource': // file pointer..
return false;
}
}
function resolveLocale($file)
{
// PART A mulitlanguage support: ( part B is gettext support in the engine..)
// - user created language version of template.
// - compile('abcdef.html') will check for compile('abcdef.en.html')
// (eg. when locale=en)
$options = $this->template->options;
$files = &$this->template->options;
$parts = array();
if ($options['locale'] &&
preg_match('/(.*)(\.[a-z]+)$/i',$file,$parts)) {
$newfile = $parts[1].'.'.$options['locale'] .$parts[2];
$dirs = array_unique($options['template_dir']);
if ($options['multisource.order'] == 'reverse') {
$dirs = array_reverse($dirs);
}
foreach ($options['template_dir'] as $tmplDir) {
if (@!file_exists($tmplDir . DIRECTORY_SEPARATOR .$newfile)) {
continue;
}
if ($files['current_template'] !== false &&
!$options['multisource.allow']) {
$old = $files['current_template'];
$files['current_template'] = false;
return $this->template->raiseError(
"You have more than one template Named %s in your paths, found in
more" .
"Than once, (in %s and %s)",
TEMPLATE_ERROR_INVALIDARGS, TEMPLATE_ERROR_DIE,
array($newfile, $old, $tmplDir . DIRECTORY_SEPARATOR .$newfile ));
}
$files['current_template'] = $tmplDir . DIRECTORY_SEPARATOR .$newfile;
$files['current_template_from_dir'] = $tmplDir;
$files['current_template_used_file'] = $newfile;
}
}
}
function resolveMultiPath() {
// look in all the posible locations for the template directory..
if ($files['currentTemplate'] === false) {
$dirs = array_unique($options['template_dir']);
if ($options['multisource.order'] == 'reverse') {
$dirs = array_reverse($dirs);
}
foreach ($dirs as $tmplDir) {
if (!@file_exists($tmplDir . DIRECTORY_SEPARATOR . $file)) {
continue;
}
if (!$options['multisource.allow'] &&
($files['current_template'] !== false)) {
return $this->template->raiseError(
"You have more than one template Named %s in your paths, found in
more" .
"Than once, (in %s and %s)",
TEMPLATE_ERROR_INVALIDARGS, TEMPLATE_ERROR_DIE,
array($file, $old, $tmplDir . DIRECTORY_SEPARATOR .$file ));
}
$files['current_template'] = $tmplDir . DIRECTORY_SEPARATOR . $file;
$files['current_template_from_dir'] = $tmplDir;
}
}
}
}