Skip to content

Commit 4b4ae7b

Browse files
committed
a special update for backward support of PHP 5.2 (remove PSR-0 autoloader, __DIR__, namespace, closure)
1 parent cf2a0ce commit 4b4ae7b

6 files changed

Lines changed: 103 additions & 87 deletions

File tree

app/autoload.php

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,20 @@
1212
|
1313
*/
1414

15-
require ROOT . '/vendor/autoload.php';
15+
16+
// Twig extension auto loading
17+
require ROOT . '/vendor/slim/extras/Views/Extension/TwigAutoloader.php';
18+
// Slim class name not Slim_Slim
19+
require ROOT . '/vendor/slim/slim/Slim/Slim.php';
20+
// slim extra has no PSR-0 autoloader support in this version
21+
require ROOT . '/vendor/slim/extras/Views/TwigView.php';
22+
// Twig extension manually loading
23+
require ROOT . '/vendor/twig/twig/lib/Twig/ExtensionInterface.php';
24+
require ROOT . '/vendor/twig/twig/lib/Twig/Extension.php';
25+
require ROOT . '/vendor/twig/extensions/lib/Twig/Extensions/Extension/Text.php';
26+
27+
require ROOT . '/vendor/gabordemooij/redbean/RedBean/redbean.inc.php';
28+
1629

1730
/*
1831
|--------------------------------------------------------------------------
@@ -25,15 +38,17 @@
2538
*/
2639

2740
// Autoloader to load classes in /app/models/
28-
spl_autoload_register(function ($class) {
41+
spl_autoload_register('autoloader_model');
42+
43+
function autoloader_model($class) {
2944
if (0 !== strpos($class, 'Model_')) {
3045
return;
3146
}
3247

3348
if (is_file($file = ROOT . '/app/models/' . $class . '.php')) {
3449
require $file;
3550
}
36-
});
51+
}
3752

3853

3954
?>

app/config/app.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,16 @@
1010
return array(
1111
'mode' => SLIM_MODE,
1212
'cookies.secret_key' => md5('appsecretkey'),
13-
'view' => new \Slim\Views\Twig(),
13+
'view' => new TwigView(),
1414
'templates.path' => ROOT . '/app/views/',
1515

1616
'debug' => SLIM_MODE === SLIM_MODE_DEV,
1717
'log.enabled' => SLIM_MODE === SLIM_MODE_PRO,
18-
'log.writer' => new \Slim\Extras\Log\DateTimeFileWriter(array(
19-
'path' => ROOT . '/app/storage/logs',
20-
'name_format' => 'Y-m-d',
21-
'message_format' => '%label% - %date% - %message%'
22-
))
18+
// 'log.writer' => new \Slim\Extras\Log\DateTimeFileWriter(array(
19+
// 'path' => ROOT . '/app/storage/logs',
20+
// 'name_format' => 'Y-m-d',
21+
// 'message_format' => '%label% - %date% - %message%'
22+
// ))
2323
);
2424

2525

Lines changed: 53 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,67 @@
11
<?php
22

33
//GET route
4-
$app->get('/', function () use ($app) {
4+
$app->get('/', 'route_default');
55

6-
$guests = R::findAll('guest', 'ORDER BY modify_date DESC');
7-
$options = array();
8-
$options['guests'] = $guests;
9-
$options['pmenu'] = array(
10-
array('desc' => 'Slim', 'url' => 'http://www.slimframework.com/'),
11-
array('desc' => 'Redbean', 'url' => 'http://redbeanphp.com/'),
12-
array('desc' => 'Twig', 'url' => 'http://twig.sensiolabs.org/'),
13-
array('desc' => 'Twitter Bootstrap', 'url' => 'http://twitter.github.io/bootstrap/'),
14-
);
15-
$options['smenu'] = array(
16-
array('desc' => 'GitHub Repository', 'url' => 'https://github.com/vanting/RedSlim'),
17-
array('desc' => 'Composer/Packagist', 'url' => 'https://packagist.org/packages/redslim/redslim'),
18-
array('desc' => 'Pagoda Box App Cafe', 'url' => 'https://pagodabox.com/cafe/vanting/redslim'),
19-
);
20-
$app->view()->appendData($options);
21-
$app->render('demo.html.twig');
22-
});
6+
function route_default() {
7+
$app = Slim::getInstance();
8+
$guests = R::findAll('guest', 'ORDER BY modify_date DESC');
9+
$options = array();
10+
$options['guests'] = $guests;
11+
$options['pmenu'] = array(
12+
array('desc' => 'Slim', 'url' => 'http://www.slimframework.com/'),
13+
array('desc' => 'Redbean', 'url' => 'http://redbeanphp.com/'),
14+
array('desc' => 'Twig', 'url' => 'http://twig.sensiolabs.org/'),
15+
array('desc' => 'Twitter Bootstrap', 'url' => 'http://twitter.github.io/bootstrap/'),
16+
);
17+
$options['smenu'] = array(
18+
array('desc' => 'GitHub Repository', 'url' => 'https://github.com/vanting/RedSlim'),
19+
array('desc' => 'Composer/Packagist', 'url' => 'https://packagist.org/packages/redslim/redslim'),
20+
array('desc' => 'Pagoda Box App Cafe', 'url' => 'https://pagodabox.com/cafe/vanting/redslim'),
21+
);
22+
$app->view()->appendData($options);
23+
$app->render('demo.html.twig');
24+
}
2325

24-
$app->get('/api/comment/json', function () use ($app) {
26+
$app->get('/api/comment/json', 'api_comment_json')->name('api_comment_json');
2527

26-
$result = R::getAll('SELECT * FROM guest ORDER BY modify_date DESC');
27-
header("Content-Type: application/json");
28-
echo json_encode($result);
29-
})->name('api_comment_json');
28+
function api_comment_json() {
29+
$app = Slim::getInstance();
30+
$result = R::getAll('SELECT * FROM guest ORDER BY modify_date DESC');
31+
header("Content-Type: application/json");
32+
echo json_encode($result);
33+
}
3034

3135
//POST route
32-
$app->post('/guest/comment', function () use($app) {
36+
$app->post('/guest/comment', 'guest_comment')->name('guest_comment');
3337

34-
$guest = R::dispense('guest');
38+
function guest_comment() {
39+
$app = Slim::getInstance();
40+
$guest = R::dispense('guest');
3541

36-
$name = $app->request->post('name');
37-
if (empty($name))
38-
$name = 'anonymous';
42+
$name = $app->request()->post('name');
43+
if (empty($name))
44+
$name = 'anonymous';
3945

40-
$guest->name = $name;
41-
$guest->message = $app->request->post('message');
42-
$guest->ip = $app->request->getIp();
43-
44-
// prepare to delete old comments
45-
$yesterday = date('Y-m-d' , strtotime('-1 day'));
46-
47-
// start transaction
48-
R::begin();
49-
try {
50-
R::exec('DELETE FROM guest WHERE modify_date < ?', array($yesterday));
51-
R::store($guest);
52-
R::commit();
53-
$app->flash('success', 'Nice to hear from you!');
54-
} catch (Exception $e) {
55-
R::rollback();
56-
$app->flash('error', 'Oops... seems something goes wrong.');
57-
}
58-
$app->redirect($app->request->getReferrer());
59-
})->name('guest_comment');
46+
$guest->name = $name;
47+
$guest->message = $app->request()->post('message');
48+
$guest->ip = $app->request()->getIp();
6049

61-
//PUT route
62-
$app->put('/put', function () use($app) {
63-
echo 'This is a PUT route';
64-
});
50+
// prepare to delete old comments
51+
$yesterday = date('Y-m-d', strtotime('-1 day'));
52+
53+
// start transaction
54+
R::begin();
55+
try {
56+
R::exec('DELETE FROM guest WHERE modify_date < ?', array($yesterday));
57+
R::store($guest);
58+
R::commit();
59+
$app->flash('success', 'Nice to hear from you!');
60+
} catch (Exception $e) {
61+
R::rollback();
62+
$app->flash('error', 'Oops... seems something goes wrong.');
63+
}
64+
$app->redirect($app->request()->getReferrer());
65+
}
6566

66-
//DELETE route
67-
$app->delete('/delete', function () use($app) {
68-
echo 'This is a DELETE route';
69-
});
7067
?>

app/start.php

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
*/
1212

1313
// Instantiate application
14-
$app = new \Slim\Slim(require_once ROOT . '/app/config/app.php');
14+
$app = new Slim(require_once ROOT . '/app/config/app.php');
1515
$app->setName('RedSlim');
1616

1717

@@ -61,16 +61,19 @@
6161
|
6262
*/
6363

64-
$view = $app->view();
65-
$view->parserOptions = array(
66-
'debug' => true,
67-
'cache' => ROOT . '/app/storage/cache/twig',
64+
TwigView::$twigDirectory = ROOT . '/vendor/twig/twig/lib/Twig';
65+
TwigView::$twigOptions = array(
6866
'auto_reload' => true,
67+
'cache' => ROOT . '/app/storage/cache/twig',
68+
'debug' => true,
6969
//'strict_variables' => true
7070
);
7171

72-
$view->parserExtensions = array(
73-
new \Slim\Views\TwigExtension(),
72+
TwigView::$twigExtensions = array(
73+
'Twig_Extensions_Slim',
74+
'Twig_Extension_Debug',
75+
'Twig_Extensions_Extension_Text',
76+
//'Twig_Extensions_Markdown',
7477
);
7578

7679
/*
@@ -82,32 +85,35 @@
8285
| the connection.
8386
|
8487
*/
85-
class R extends RedBean_Facade {
88+
class RB {
8689

8790
static function loadConfig($config) {
8891

8992
$conn = $config['connections'][$config['default']];
9093

9194
switch($conn['driver']) {
9295
case 'mysql':
93-
self::setup ($conn['driver'] . ':host=' . $conn['host'] . '; dbname=' . $conn['database'], $conn['username'], $conn['password']);
96+
R::setup ($conn['driver'] . ':host=' . $conn['host'] . '; dbname=' . $conn['database'], $conn['username'], $conn['password']);
9497
break;
9598
case 'sqlite':
96-
self::setup ($conn['driver'] . ':' . $conn['database']);
99+
R::setup ($conn['driver'] . ':' . $conn['database']);
97100
break;
98101
}
99102
}
100103

101104
}
102105

103-
R::loadConfig(require_once ROOT . '/app/config/database.php');
106+
RB::loadConfig(require_once ROOT . '/app/config/database.php');
107+
108+
104109

105110
// Disable fluid mode in production environment
106-
$app->configureMode(SLIM_MODE_PRO, function () use ($app) {
111+
$app->configureMode(SLIM_MODE_PRO, 'switchMode');
112+
113+
function switchMode() {
107114
// note, transactions will be auto-committed in fluid mode
108115
R::freeze(true);
109-
});
110-
116+
}
111117

112118
/*
113119
|--------------------------------------------------------------------------

composer.json

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,12 @@
1212
}
1313
],
1414
"require": {
15-
"php": ">=5.3.2",
16-
"slim/slim": "2.3.*",
17-
"slim/extras": "2.0.*",
18-
"slim/views":"0.1.*",
15+
"php": ">=5.2.0",
16+
"slim/slim": "1.6.7",
17+
"slim/extras": "1.0.2",
1918
"twig/twig": "1.*",
20-
"twig/extensions": "*",
21-
"gabordemooij/redbean": "*",
22-
"raveren/kint": "dev-master"
19+
"twig/extensions": "1.0.*",
20+
"gabordemooij/redbean": "*"
2321
},
2422
"config": {
2523
"preferred-install": "dist"

web/index.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
error_reporting(error_reporting() & ~E_NOTICE); // ignore error notice of undefined variables
1919
date_default_timezone_set('Asia/Hong_Kong');
2020

21-
define('ROOT', dirname(__DIR__));
21+
define('ROOT', dirname(dirname(__FILE__)));
2222

2323
/*
2424
|--------------------------------------------------------------------------

0 commit comments

Comments
 (0)