-
Notifications
You must be signed in to change notification settings - Fork 1
/
DecoratorSelector.class.php
136 lines (117 loc) · 4.67 KB
/
DecoratorSelector.class.php
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
<?php
/*
PHP-Mesh - A page meshing framework for PHP.
Copyright (C) 2003-2009 Trejkaz
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
You can contact the author by electronic mail, which is presently
at the following address: trejkaz@trypticon.org
*/
require_once('Decorator.class.php');
require_once('utils.php');
/**
* This class defines objects which are capable of locating decorator files
* given the decorators' names. It does this using the .phpmeshrc configuration
* file in the document root.
*
* @author Trejkaz <trejkaz@trypticon.org>
*/
class DecoratorSelector
{
// The name of the default decorator.
// This variable is prone to change, so don't use it directly.
var $_default_decorator_name;
// The location of the decorator directory.
// This variable is prone to change, so don't use it directly.
var $_decorator_directory;
/**
* Creates the decorator selector, reading the .phpmeshrc file from
* the document root to provide its configuration.
*/
function DecoratorSelector()
{
$phpmeshrc_file = find_nearest('.phpmeshrc');
if ($phpmeshrc_file == NULL)
{
die("FATAL: Couldn't find .phpmeshrc");
}
// Protect against Random Q. User inserting space in his config file.
ob_start();
require($phpmeshrc_file);
ob_end_clean();
// Get the name of the default decorator.
$this->_default_decorator_name = $meshconfig{'decorator_default'};
// Resolve the decorator directory relative to the .phpmeshrc file.
$this->_decorator_directory = resolve_path(dirname($phpmeshrc_file), $meshconfig{'decorator_directory'});
}
/**
* Gets the absolute path to a file for the given filename, which may be relative.
* The path is resolved relative to the decorator directory.
*
* @param $path the path to the file, relative or absolute.
* @return the absolute path to the file.
*/
function get_path($path)
{
return resolve_path($this->_decorator_directory, $path);
}
/**
* Gets a Decorator object for the given name.
* If the name is not provided, gets the default decorator. If the decorator
* requested is not found, the default is used instead (assuming of course that
* _it_ could be found.)
*
* @param $decorator_name the name of the decorator to fetch.
* @return the decorator, or NULL if it was not found and no default was found either.
*/
function get_decorator($decorator_name = NULL)
{
// If the name provided is empty or invalid, provide a default.
if ($decorator_name == NULL || $decorator_name == '' || preg_match("/^\w+$/", $decorator_name) != 1)
{
if (@isset($_GET["decorator"]))
{
$decorator_name = $_GET["decorator"];
}
else
{
// Will use the default in a second.
$decorator_name = $this->_default_decorator_name;
}
}
// Treat identity as a special decorator name.
if ($decorator_name == "identity")
{
return NULL;
}
// Which file is used will depend on the language.
$accepted_languages = parse_http_accept_language();
foreach ($accepted_languages as $lang)
{
// Build the filename. This is vaguely like Apache MultiViews.
$decorator_filename = $this->get_path($decorator_name . '.' . $lang . '.php');
if (file_exists($decorator_filename))
{
return new Decorator($decorator_filename);
}
}
// Fall back to the filename without the language appended on.
$decorator_filename = $this->get_path($decorator_name . '.php');
if (file_exists($decorator_filename))
{
return new Decorator($decorator_filename);
}
// Fall out to return NULL if the file couldn't be found.
return NULL;
}
}
?>