cvs: pear /Template/Template Multipath.php Multisource.php
| From: | Alan Knowles | Date: | Mon, 14 Jun 2004 08:55:01 +0000 |
| Subject: | cvs: pear /Template/Template Multipath.php Multisource.php | ||
| Groups: | php.pear.cvs | ||
| Request: | Send a blank email to pear-cvs+get-20641@lists.php.net to get a copy of this message | ||
alan_k Mon Jun 14 04:55:01 2004 EDT
Added files:
/pear/Template/Template Multisource.php
Removed files:
/pear/Template/Template Multipath.php
Log:
oops got the file name wrong
http://cvs.php.net/co.php/pear/Template/Template/Multisource.php?r=1.1&p=1
Index: pear/Template/Template/Multisource.php
+++ pear/Template/Template/Multisource.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: Multisource.php,v 1.1 2004/06/14 08:55:01 alan_k Exp $
//
// Multipath Provider - checks mulitple paths for the source of a template
//
class Template_Multisource
{
/**
* Default options related to this provider.
*
* @var array
* @access public
*/
var $options = array(
'multisource.order' => null, // use 'reverse' - to sort
backwards..
'multisource.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 find($file) {
$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);
// 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)
$files['current_template'] = false;
$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;
}
}
// found it already! ( a localized version )
if ($files['current_template']) {
return true;
}
// 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;
}
}
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;
}
}