/
initialize.php
176 lines (153 loc) · 4.36 KB
/
initialize.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
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
<?php
/**
* Alchemy Theme Framework
*
* Features:
* * LessCSS development mode
* * Full support for HTML5
* * IE 8 below Partial support for CSS3 through PIE
* * Very Simple Helpers(for now)
*
* This file Contains the basic bootstrap helpers for Alchemy - this shouldn't change much
*
* @package Alchemy
* @author Israel D. Canasa
* @copyright (c) 2010 MagicThemes.com - Wiz Media, Inc
* @license http://www.gnu.org/licenses/gpl-2.0.html
*/
class Alchemy
{
protected static $_root;
protected static $_system;
protected static $_context;
protected static $_versions;
protected static $_classes = array();
/**
* Initialize Alchemy and sets up the version stack.
* - Alchemy uses a Cascading Version System(CVS) for absolute compatibility with older versions
* - The CVS makes it absolutely sure that a template will use the version it was built on.
* - It means that upgrading Alchemy won't break anything
* - When a template initializes Alchemy, it can specify the version that it will use.
* - Version number format is YYMMDD which reflect the date of release. This is better than using cryptic version numbers.
*
* @param string $version
* @return void
* @author Israel D. Canasa
*/
public static function start($context = NULL, $version = 'latest')
{
self::$_context = ($context) ? $context : stdclass;
// Detect the System
// TODO: One day, this should detect the environment for WordPress, Nooku 1.0 and other systems.
if (_JEXEC)
{
self::$_root = realpath(dirname(__FILE__));
$jversion = explode('.', JVERSION);array_pop($jversion);
self::$_system = 'joomla'.implode('', $jversion);
}
self::$_versions = array($version);
// Put the $version on top of the stack
foreach(scandir(self::$_root.DS.'versions') as $dir)
{
if ($dir == '.' OR $dir == '..') {
continue;
}
if ( ! $version OR (int)$version > (int)$dir OR $version === 'latest')
{
self::$_versions[] = $dir;
}
}
rsort(self::$_versions);
}
/**
* Loads, instantiates and returns Alchemy helper classes
*
* Class names are converted to file names by making the class name
* lowercase and converting underscores to slashes:
*
* @example
* // Loads versions/[version]/helpers/html.php
* Alchemy::call('helpers/html');
*
* @param string class relative path
* @return object
*/
public static function call($class, $parameters = NULL)
{
if (isset(self::$_classes[$class]))
{
return self::$_classes[$class];
}
if ($path = Alchemy::find_file('versions', $class))
{
// Load the class file
require $path;
$classname = 'alchemy_'.str_replace('/', '_', $class);
$instance = new $classname($parameters);
// Stores the instance for reuse and returns it
return self::$_classes[$class] = $instance;
}
// Class is not in the filesystem
return FALSE;
}
/**
* Load the configuration based on the current system and context
*
* @param string $key
* @return mixed
* @author Israel D. Canasa
*/
public static function config($key = NULL)
{
static $config;
if (is_null($config))
{
// Loads the config driver for the current system
$config = Alchemy::call('system/'.self::$_system.'/config', self::$_context);
}
if ($key)
{
return $config->get($key);
}
return $config;
}
public static function context()
{
return self::$_context;
}
/**
* Finds the path of a file by directory, filename.
* - Uses a cascading versioning system.
* - The version called in Alchemy::start() will be on the top of the stack.
* - Newer versions will be ignored.
*
* @param string directory name (views, i18n, classes, extensions, etc.)
* @param string filename with subdirectory
* @return string single file path
*/
public static function find_file($dir, $file, $ext = NULL)
{
// Use the php extension by default
$ext = (is_null($ext)) ? '.php': $ext;
// Create a partial path of the filename
$path = $file.$ext;
if ($dir != 'versions')
{
if (is_file($directory.$path))
{
// A path has been found
return $directory.$path;
}
}
foreach (self::$_versions as $version)
{
$directory = self::$_root.DS.$dir.DS.$version.DS;
if (is_file($directory.$path))
{
// A path has been found
return $directory.$path;
}
}
return FALSE;
}
}