/www/slight

To get this branch, use:
bzr branch http://bzr.ed.am/www/slight

« back to all changes in this revision

Viewing changes to fight.php

  • Committer: Tim Marston
  • Date: 2015-08-05 18:48:29 UTC
  • Revision ID: tim@ed.am-20150805184829-c3qj13d1h89pkb2l
initial commit

Show diffs side-by-side

added added

removed removed

1
1
<?php
2
2
 
3
3
/**
4
 
 * Slight: an extension to Flight, in the form of useful configuration changes
 
4
 * Fight: an extension to Flight, in the form of useful configuration changes
5
5
 * and extra library code.
6
6
 *
7
7
 * @copyright Copyright (c) 2015, Tim Marston <tim@ed.am>
8
8
 * @licence   MIT
9
9
 *
10
 
 * Slight brings the following changes to Flight:
11
 
 *
12
 
 *  1. A global $app object.
13
 
 *
14
 
 *  2. App directories.
 
10
 * FIGHT! ...brings the following changes to Flight:
 
11
 *
 
12
 *  1. Global $app object.
 
13
 *
 
14
 *  2. App directorties.
15
15
 *
16
16
 * It is expected that your app has the following layout, and class and view
17
17
 * paths are set up to reflect this:
55
55
 * NOTE: the use of default values for arguments is REQUIRED, or a PHP error
56
56
 * would occur where if they were missing in the HTTP request.
57
57
 *
58
 
 * You can also write "specialisations" of actions, where an action has a
59
 
 * specific argument value.  This is achieved by using a double underscore in
60
 
 * the method name to represent the path separator.  For example, using the
61
 
 * above routing, /some/path/list/1 will route to MyClass::action_list__1(), if
62
 
 * it exists, leaving URLs that have other values in the last part of their path
63
 
 * (for example, /some/path/list/2) to still route to MyClass::action_list() as
 
58
 * You can also specify either "special cases" of actions, or actions for a
 
59
 * lower level of the URL path, by using a double-underscore to represent a path
 
60
 * separator in your action method name.  For example, using the above routing,
 
61
 * /some/path/list/1 will route to MyClass::action_list__1(), if it exists,
 
62
 * leaving URLs that have other values in the last part of their path (for
 
63
 * example, /some/path/list/2) to still route to MyClass::action_list() as
64
64
 * before (passing 2 as the first argument).
65
65
 *
66
66
 * Also note that hyphens in URLs are converted to underscores (or they wouldn't
67
 
 * be usable as method names), and multiple underscores are contracted to a
 
67
 * be usable as method names), and multiple underscores are contracred to a
68
68
 * single underscore (so that the path separators can be represented as a
69
 
 * double-underscore in method names)
70
 
 *
71
 
 *  4. Add database connection and classes
72
 
 *
73
 
 * The new method connect() takes an array of connection parameters, as follows:
74
 
 *     type: the database type (defaults to mysql)
75
 
 *     host: the database hostname (defaults to localhost)
76
 
 *     port: database port number (optional)
77
 
 *     dbname: database name
78
 
 *     username: authentication user
79
 
 *     password: authentication password
80
 
 *     prefix: a table prefix
81
 
 *
82
 
 * This makes a PDO connection available at $app->db().
83
 
 *
84
 
 * A Model base class is available, for an app to base it's own models on.
85
 
 *
86
 
 *  5. Helper functions
87
 
 *
88
 
 * The following helper functions are provided:
89
 
 *
90
 
 * e()
91
 
 *     This is a global function which escapes text for use in HTML.
92
 
 * $app->link( 'some/where' )
93
 
 *     Constructs and returns links, taking in to account the base_url.
 
69
 * double-underscode in method names)
94
70
 */
95
71
 
96
 
 
97
 
// instantiate global flight app
 
72
// instantiate flight app
98
73
require 'flight/autoload.php';
99
74
$app = new flight\Engine();
100
75
 
101
 
// set slight class path
 
76
// set fight class path
102
77
$app->path( __DIR__.'/classes' );
103
78
 
104
79
// set app paths
105
80
$app->set( 'flight.views.path', 'app/views' );
106
81
$app->path( 'app/controllers' );
107
 
$app->path( 'app/models' );
108
82
 
109
83
// config
110
84
$app->set( 'flight.log_errors', true );
134
108
 
135
109
                                        // default action?
136
110
                                        if( $splat === '' ) {
137
 
                                                if( method_exists( $class, 'default_action' ) )
138
 
                                                {
139
 
                                                        // set controller url
140
 
                                                        $app->set( 'controller', '' );
141
 
 
142
 
                                                        // instantiate class and call method
 
111
                                                if( method_exists( $class, 'default_action' ) ) {
143
112
                                                        $obj = new $class();
144
113
                                                        return $obj->default_action();
145
114
                                                }
154
123
                                                {
155
124
                                                        // check to see if combined parts make a method name
156
125
                                                        $method = 'action_'.implode( '__', $parts );
157
 
                                                        if( method_exists( $class, $method ) )
158
 
                                                        {
159
 
                                                                // set controller url
160
 
                                                                $controller = explode( '/', preg_replace(
161
 
                                                                        array( '/^\//', '/\/$/' ), '',
162
 
                                                                        $app->request()->url ) );
163
 
                                                                array_splice( $controller, -count( $params ) );
164
 
                                                                $app->set( 'controller',
165
 
                                                                        join( '/', $controller ) );
166
 
 
167
 
                                                                // instantiate class and call method
 
126
                                                        if( method_exists( $class, $method ) ) {
168
127
                                                                $obj = new $class();
169
128
                                                                return call_user_func_array(
170
129
                                                                        array( $obj, $method ), $params );
184
143
                }
185
144
        } );
186
145
 
187
 
// add $app to global view parameters
 
146
// add $app to view parameters
188
147
$app->view()->set( 'app', $app );
189
 
 
190
 
// add connect() method
191
 
$app->map( 'connect',
192
 
        function( $params ) use( &$app )
193
 
        {
194
 
                // default values
195
 
                $params += array( 'type' => 'mysql', 'host' => 'localhost' );
196
 
 
197
 
                // construct pdo string
198
 
                $pdostr = $params[ 'type' ].':host='.$params[ 'host' ];
199
 
                if( isset( $params[ 'port' ] ) ) $pdostr .= ';port='.$params[ 'port' ];
200
 
                $pdostr .= ';dbname='.$params[ 'dbname' ];
201
 
 
202
 
                // register DB class
203
 
                $app->register( 'db', 'PDO',
204
 
                        array( $pdostr, $params[ 'username' ], $params[ 'password' ] ) );
205
 
 
206
 
                // set variables
207
 
                if( isset( $params[ 'prefix' ] ) ) {
208
 
                        $app->set( 'dbprefix', $params[ 'prefix' ] );
209
 
                }
210
 
        } );
211
 
 
212
 
// add general html escaping function
213
 
function e( $text )
214
 
{
215
 
        return htmlspecialchars( $text, ENT_COMPAT | ENT_HTML5 );
216
 
}
217
 
 
218
 
// add link helper
219
 
$app->map( 'link',
220
 
        function( $url = null ) use( &$app )
221
 
        {
222
 
                if( is_null( $url ) ) $url = $app->get( 'controller' );
223
 
                $url = preg_replace( array( '/^\//', '/\/$/' ), '', $url );
224
 
                return $app->get( 'base_url' ).'/'.$url;
225
 
        } );