/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 slight.php

  • Committer: Tim Marston
  • Date: 2015-12-01 11:00:20 UTC
  • Revision ID: tim@ed.am-20151201110020-fyuq0f0bg69cxcbv
renamed project; added connect() method and e() and link() helpers

Show diffs side-by-side

added added

removed removed

1
1
<?php
2
2
 
3
3
/**
4
 
 * Fight: an extension to Flight, in the form of useful configuration changes
 
4
 * Slight: 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
 
 * FIGHT! ...brings the following changes to Flight:
11
 
 *
12
 
 *  1. Global $app object.
13
 
 *
14
 
 *  2. App directorties.
 
10
 * Slight brings the following changes to Flight:
 
11
 *
 
12
 *  1. A global $app object.
 
13
 *
 
14
 *  2. App directories.
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 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
 
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
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 contracred to a
 
67
 * be usable as method names), and multiple underscores are contracted to a
68
68
 * single underscore (so that the path separators can be represented as a
69
 
 * double-underscode in method names)
 
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.
70
94
 */
71
95
 
72
 
// instantiate flight app
 
96
 
 
97
// instantiate global flight app
73
98
require 'flight/autoload.php';
74
99
$app = new flight\Engine();
75
100
 
76
 
// set fight class path
 
101
// set slight class path
77
102
$app->path( __DIR__.'/classes' );
78
103
 
79
104
// set app paths
80
105
$app->set( 'flight.views.path', 'app/views' );
81
106
$app->path( 'app/controllers' );
 
107
$app->path( 'app/models' );
82
108
 
83
109
// config
84
110
$app->set( 'flight.log_errors', true );
108
134
 
109
135
                                        // default action?
110
136
                                        if( $splat === '' ) {
111
 
                                                if( method_exists( $class, 'default_action' ) ) {
 
137
                                                if( method_exists( $class, 'default_action' ) )
 
138
                                                {
 
139
                                                        // set controller url
 
140
                                                        $app->set( 'controller', '' );
 
141
 
 
142
                                                        // instantiate class and call method
112
143
                                                        $obj = new $class();
113
144
                                                        return $obj->default_action();
114
145
                                                }
123
154
                                                {
124
155
                                                        // check to see if combined parts make a method name
125
156
                                                        $method = 'action_'.implode( '__', $parts );
126
 
                                                        if( method_exists( $class, $method ) ) {
 
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
127
168
                                                                $obj = new $class();
128
169
                                                                return call_user_func_array(
129
170
                                                                        array( $obj, $method ), $params );
143
184
                }
144
185
        } );
145
186
 
146
 
// add $app to view parameters
 
187
// add $app to global view parameters
147
188
$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
        } );