Added Canvas 1.1.0, originally not under SCM so no historical development records...
[canvas.git] / library / Conventions.php
blob290a4a5a385a0a2c4989611a12ec9e849d2f9dea
1 <?php
2 // @title Conventions (class of configurations and all)
3 // @author Matt Todd <matt@matttoddphoto.com>
4 // @created 2006-03-29
5 // @desc Conventions and default values to be used throughout the system
6 // @requires stdexception.php (StdException class)
7 // @note Yes, it came a little late in the game.
9 // This file is to be clean
11 class Conventions {
12 public static $app_dir = '';
14 // path info (for routing)
15 public static function path_info() {
16 return substr($_SERVER['PATH_INFO'], 1);
19 // 'directories' contains the directory names for various components.
20 // It will appear to be repetitive, but this is due to the fact that
21 // the keys are the same as the default so that the internal system
22 // never has to know it's something different!
23 public static $directories = array(
24 'library'=>'library', // standard and concrete for now
25 'adapters'=>'library/adapters',
26 'controllers'=>'controllers',
27 'models'=>'models',
28 'helpers'=>'helpers',
29 'views'=>'views',
30 'config'=>'config',
31 'logs'=>'logs',
32 'extensions'=>'extensions',
33 ); // directories
35 // static method to get the directory name
36 public static function directory($name) {
37 return self::$directories[$name];
40 // 'names' contains specifications for how names should be formed
41 // and derived. If custom functionality is desired, modify the
42 // names and the functions, such as to Camelize names for names
43 // as 'PublicController' instead of 'public_controller'
44 public static $names = array(
45 // config
46 'config'=> array(
47 // cache file
48 'cache'=> 'config.cache',
50 // the generic file (for non-specific config-files)
51 'file'=> '%s.yml',
53 // explicit files
54 'files'=> array(
55 'routes'=> 'routes.php',
59 // controllers
60 'controllers'=> array(
61 'class'=> '%s_controller',
62 'file'=> '%s_controller.php',
63 'action'=> '%s',
66 // models
67 'models'=> array(
68 'class'=> '%s',
69 'file'=> '%s.php',
72 // views
73 'views'=> array(
74 'name'=> '%s',
75 'class'=> '%s_view',
76 'file'=> '%s_view.php',
77 'action'=> '%s.php',
78 'controller'=> '%s',
79 'layout' => 'layout.php',
82 // helpers
83 'helpers'=> array(
84 'class'=> '%s_helper',
85 'file'=> '%s_helper.php',
88 // libraries
89 'library'=> array(
90 'file'=> '%s.php',
91 'adapter'=> '%s.php',
94 // extensions
95 'extensions'=> array(
96 'file'=> '%s.php',
99 // logs
100 'logs'=> array(
101 'file'=> '%s.log',
102 'separate_file'=> '%s.%s.log'
104 ); // names
106 // naming methods ///////////////////////////////////////////////////////////////////////////////////////////////////
107 // form standard names
109 // controller naming functions
110 public static function controller_name($name) {
111 return sprintf(self::$names['controllers']['class'], $name);
113 public static function controller_class_name($name) {
114 return self::controller_name($name);
116 public static function controller_file_name($name) {
117 return sprintf(self::$names['controllers']['file'], $name);
119 // action name
120 public static function action_name($name) {
121 return sprintf(self::$names['controllers']['action'], $name);
124 // model naming functions
125 public static function model_name($name) {
126 return sprintf(self::$names['models']['class'], $name);
128 public static function model_class_name($name) {
129 return self::model_name($name);
131 public static function model_file_name($name) {
132 return sprintf(self::$names['models']['file'], $name);
135 // view naming functions
136 public static function view_name($name) {
137 return sprintf(self::$names['views']['name'], $name);
139 public static function view_class_name($name) {
140 return sprintf(self::$names['views']['class'], $name);
142 public static function view_class_file_name($name) {
143 return sprintf(self::$names['views']['file'], $name);
145 public static function view_file_name($name) {
146 return sprintf(self::$names['views']['action'], $name);
148 public static function view_controller_name($name) {
149 return sprintf(self::$names['views']['controller'], $name);
151 public static function view_layout_file_name() {
152 return self::$names['views']['layout'];
155 // helper naming functions
156 public static function helper_name($name) {
157 return sprintf(self::$names['helpers']['class'], $name);
159 public static function helper_class_name($name) {
160 return self::helper_name($name);
162 public static function helper_file_name($name) {
163 return sprintf(self::$names['helpers']['file'], $name);
166 // extensions and libraries
167 public static function library_file_name($name) {
168 return sprintf(self::$names['library']['file'], $name);
170 public static function extension_file_name($name) {
171 return sprintf(self::$names['extensions']['file'], $name);
174 // adapters
175 public static function adapter_file_name($name) {
176 return sprintf(self::$names['library']['adapter'], strtolower($name));
179 // paths methods //////////////////////////////////////////////////////////////////////////////////////////////////////
181 // generic path function
182 public static function path($dir, $file) {
183 return self::$app_dir . self::directory($dir) . '/' . $file;
186 // controller path
187 public static function controller_path($name) {
188 return self::path('controllers', self::controller_file_name($name));
190 // model path
191 public static function model_path($name) {
192 return self::path('models', self::model_file_name($name));
194 // view path
195 public static function view_path($controller, $name) {
196 return self::path('views', (self::view_controller_name($controller) . '/' . self::view_file_name($name)));
198 public static function view_class_path($name) {
199 return self::path('views', self::view_class_name($name));
201 public static function view_class_file_path($name) {
202 return self::path('views', self::view_class_file_name($name));
204 // helper path
205 public static function helper_path($name) {
206 return self::path('helpers', self::helper_file_name($name));
208 // library path
209 public static function library_path($name) {
210 return self::path('library', self::library_file_name($name));
212 // extension path
213 public static function extension_path($name) {
214 return self::path('extensions', self::extension_file_name($name));
216 // adapter path
217 public static function adapter_path($name) {
218 return self::path('adapters', self::adapter_file_name($name));
221 // specific file methods //
223 // config cache file
224 public static function config_cache_file() {
225 return self::path('config', self::$names['config']['cache']);
227 // config file name
228 public static function config_file($name) {
229 // if the filename is explicitly set, return that value
230 if(!empty(self::$names['config']['files'][$name]))
231 return self::path('config', self::$names['config']['files'][$name]);
233 // otherwise, return the default form of the config file (such as '{$name}.yml')
234 return self::path('config', sprintf(self::$names['config']['file'], $name));
237 // log file
238 // config file name
239 public static function log_file($name) {
240 return self::path('logs', sprintf(self::$names['logs']['file'], $name));
242 public static function separate_log_file($section, $name) {
243 return self::path('logs', sprintf(self::$names['logs']['separate_file'], $section, $name));