Skip to content

Commit

Permalink
a special update for backward support of PHP 5.2 (remove PSR-0 autolo…
Browse files Browse the repository at this point in the history
…ader, __DIR__, namespace, closure)
  • Loading branch information
vanting committed Dec 14, 2013
1 parent cf2a0ce commit 4b4ae7b
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 87 deletions.
21 changes: 18 additions & 3 deletions app/autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,20 @@
|
*/

require ROOT . '/vendor/autoload.php';

// Twig extension auto loading
require ROOT . '/vendor/slim/extras/Views/Extension/TwigAutoloader.php';
// Slim class name not Slim_Slim
require ROOT . '/vendor/slim/slim/Slim/Slim.php';
// slim extra has no PSR-0 autoloader support in this version
require ROOT . '/vendor/slim/extras/Views/TwigView.php';
// Twig extension manually loading
require ROOT . '/vendor/twig/twig/lib/Twig/ExtensionInterface.php';
require ROOT . '/vendor/twig/twig/lib/Twig/Extension.php';
require ROOT . '/vendor/twig/extensions/lib/Twig/Extensions/Extension/Text.php';

require ROOT . '/vendor/gabordemooij/redbean/RedBean/redbean.inc.php';


/*
|--------------------------------------------------------------------------
Expand All @@ -25,15 +38,17 @@
*/

// Autoloader to load classes in /app/models/
spl_autoload_register(function ($class) {
spl_autoload_register('autoloader_model');

function autoloader_model($class) {
if (0 !== strpos($class, 'Model_')) {
return;
}

if (is_file($file = ROOT . '/app/models/' . $class . '.php')) {
require $file;
}
});
}


?>
12 changes: 6 additions & 6 deletions app/config/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@
return array(
'mode' => SLIM_MODE,
'cookies.secret_key' => md5('appsecretkey'),
'view' => new \Slim\Views\Twig(),
'view' => new TwigView(),
'templates.path' => ROOT . '/app/views/',

'debug' => SLIM_MODE === SLIM_MODE_DEV,
'log.enabled' => SLIM_MODE === SLIM_MODE_PRO,
'log.writer' => new \Slim\Extras\Log\DateTimeFileWriter(array(
'path' => ROOT . '/app/storage/logs',
'name_format' => 'Y-m-d',
'message_format' => '%label% - %date% - %message%'
))
// 'log.writer' => new \Slim\Extras\Log\DateTimeFileWriter(array(
// 'path' => ROOT . '/app/storage/logs',
// 'name_format' => 'Y-m-d',
// 'message_format' => '%label% - %date% - %message%'
// ))
);


Expand Down
109 changes: 53 additions & 56 deletions app/controllers/demo.controller.php
Original file line number Diff line number Diff line change
@@ -1,70 +1,67 @@
<?php

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

$guests = R::findAll('guest', 'ORDER BY modify_date DESC');
$options = array();
$options['guests'] = $guests;
$options['pmenu'] = array(
array('desc' => 'Slim', 'url' => 'http://www.slimframework.com/'),
array('desc' => 'Redbean', 'url' => 'http://redbeanphp.com/'),
array('desc' => 'Twig', 'url' => 'http://twig.sensiolabs.org/'),
array('desc' => 'Twitter Bootstrap', 'url' => 'http://twitter.github.io/bootstrap/'),
);
$options['smenu'] = array(
array('desc' => 'GitHub Repository', 'url' => 'https://github.com/vanting/RedSlim'),
array('desc' => 'Composer/Packagist', 'url' => 'https://packagist.org/packages/redslim/redslim'),
array('desc' => 'Pagoda Box App Cafe', 'url' => 'https://pagodabox.com/cafe/vanting/redslim'),
);
$app->view()->appendData($options);
$app->render('demo.html.twig');
});
function route_default() {
$app = Slim::getInstance();
$guests = R::findAll('guest', 'ORDER BY modify_date DESC');
$options = array();
$options['guests'] = $guests;
$options['pmenu'] = array(
array('desc' => 'Slim', 'url' => 'http://www.slimframework.com/'),
array('desc' => 'Redbean', 'url' => 'http://redbeanphp.com/'),
array('desc' => 'Twig', 'url' => 'http://twig.sensiolabs.org/'),
array('desc' => 'Twitter Bootstrap', 'url' => 'http://twitter.github.io/bootstrap/'),
);
$options['smenu'] = array(
array('desc' => 'GitHub Repository', 'url' => 'https://github.com/vanting/RedSlim'),
array('desc' => 'Composer/Packagist', 'url' => 'https://packagist.org/packages/redslim/redslim'),
array('desc' => 'Pagoda Box App Cafe', 'url' => 'https://pagodabox.com/cafe/vanting/redslim'),
);
$app->view()->appendData($options);
$app->render('demo.html.twig');
}

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

$result = R::getAll('SELECT * FROM guest ORDER BY modify_date DESC');
header("Content-Type: application/json");
echo json_encode($result);
})->name('api_comment_json');
function api_comment_json() {
$app = Slim::getInstance();
$result = R::getAll('SELECT * FROM guest ORDER BY modify_date DESC');
header("Content-Type: application/json");
echo json_encode($result);
}

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

$guest = R::dispense('guest');
function guest_comment() {
$app = Slim::getInstance();
$guest = R::dispense('guest');

$name = $app->request->post('name');
if (empty($name))
$name = 'anonymous';
$name = $app->request()->post('name');
if (empty($name))
$name = 'anonymous';

$guest->name = $name;
$guest->message = $app->request->post('message');
$guest->ip = $app->request->getIp();

// prepare to delete old comments
$yesterday = date('Y-m-d' , strtotime('-1 day'));

// start transaction
R::begin();
try {
R::exec('DELETE FROM guest WHERE modify_date < ?', array($yesterday));
R::store($guest);
R::commit();
$app->flash('success', 'Nice to hear from you!');
} catch (Exception $e) {
R::rollback();
$app->flash('error', 'Oops... seems something goes wrong.');
}
$app->redirect($app->request->getReferrer());
})->name('guest_comment');
$guest->name = $name;
$guest->message = $app->request()->post('message');
$guest->ip = $app->request()->getIp();

//PUT route
$app->put('/put', function () use($app) {
echo 'This is a PUT route';
});
// prepare to delete old comments
$yesterday = date('Y-m-d', strtotime('-1 day'));

// start transaction
R::begin();
try {
R::exec('DELETE FROM guest WHERE modify_date < ?', array($yesterday));
R::store($guest);
R::commit();
$app->flash('success', 'Nice to hear from you!');
} catch (Exception $e) {
R::rollback();
$app->flash('error', 'Oops... seems something goes wrong.');
}
$app->redirect($app->request()->getReferrer());
}

//DELETE route
$app->delete('/delete', function () use($app) {
echo 'This is a DELETE route';
});
?>
34 changes: 20 additions & 14 deletions app/start.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
*/

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


Expand Down Expand Up @@ -61,16 +61,19 @@
|
*/

$view = $app->view();
$view->parserOptions = array(
'debug' => true,
'cache' => ROOT . '/app/storage/cache/twig',
TwigView::$twigDirectory = ROOT . '/vendor/twig/twig/lib/Twig';
TwigView::$twigOptions = array(
'auto_reload' => true,
'cache' => ROOT . '/app/storage/cache/twig',
'debug' => true,
//'strict_variables' => true
);

$view->parserExtensions = array(
new \Slim\Views\TwigExtension(),
TwigView::$twigExtensions = array(
'Twig_Extensions_Slim',
'Twig_Extension_Debug',
'Twig_Extensions_Extension_Text',
//'Twig_Extensions_Markdown',
);

/*
Expand All @@ -82,32 +85,35 @@
| the connection.
|
*/
class R extends RedBean_Facade {
class RB {

static function loadConfig($config) {

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

switch($conn['driver']) {
case 'mysql':
self::setup ($conn['driver'] . ':host=' . $conn['host'] . '; dbname=' . $conn['database'], $conn['username'], $conn['password']);
R::setup ($conn['driver'] . ':host=' . $conn['host'] . '; dbname=' . $conn['database'], $conn['username'], $conn['password']);
break;
case 'sqlite':
self::setup ($conn['driver'] . ':' . $conn['database']);
R::setup ($conn['driver'] . ':' . $conn['database']);
break;
}
}

}

R::loadConfig(require_once ROOT . '/app/config/database.php');
RB::loadConfig(require_once ROOT . '/app/config/database.php');



// Disable fluid mode in production environment
$app->configureMode(SLIM_MODE_PRO, function () use ($app) {
$app->configureMode(SLIM_MODE_PRO, 'switchMode');

function switchMode() {
// note, transactions will be auto-committed in fluid mode
R::freeze(true);
});

}

/*
|--------------------------------------------------------------------------
Expand Down
12 changes: 5 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,12 @@
}
],
"require": {
"php": ">=5.3.2",
"slim/slim": "2.3.*",
"slim/extras": "2.0.*",
"slim/views":"0.1.*",
"php": ">=5.2.0",
"slim/slim": "1.6.7",
"slim/extras": "1.0.2",
"twig/twig": "1.*",
"twig/extensions": "*",
"gabordemooij/redbean": "*",
"raveren/kint": "dev-master"
"twig/extensions": "1.0.*",
"gabordemooij/redbean": "*"
},
"config": {
"preferred-install": "dist"
Expand Down
2 changes: 1 addition & 1 deletion web/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
error_reporting(error_reporting() & ~E_NOTICE); // ignore error notice of undefined variables
date_default_timezone_set('Asia/Hong_Kong');

define('ROOT', dirname(__DIR__));
define('ROOT', dirname(dirname(__FILE__)));

/*
|--------------------------------------------------------------------------
Expand Down

0 comments on commit 4b4ae7b

Please sign in to comment.