diff --git a/app/autoload.php b/app/autoload.php index f59d014..e542327 100644 --- a/app/autoload.php +++ b/app/autoload.php @@ -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'; + /* |-------------------------------------------------------------------------- @@ -25,7 +38,9 @@ */ // 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; } @@ -33,7 +48,7 @@ if (is_file($file = ROOT . '/app/models/' . $class . '.php')) { require $file; } - }); + } ?> diff --git a/app/config/app.php b/app/config/app.php index 7a5a6ab..238828a 100644 --- a/app/config/app.php +++ b/app/config/app.php @@ -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%' +// )) ); diff --git a/app/controllers/demo.controller.php b/app/controllers/demo.controller.php index 1335309..a5d29fe 100644 --- a/app/controllers/demo.controller.php +++ b/app/controllers/demo.controller.php @@ -1,70 +1,67 @@ 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'; - }); ?> \ No newline at end of file diff --git a/app/start.php b/app/start.php index 93a63ba..1eb96ef 100644 --- a/app/start.php +++ b/app/start.php @@ -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'); @@ -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', ); /* @@ -82,7 +85,7 @@ | the connection. | */ -class R extends RedBean_Facade { +class RB { static function loadConfig($config) { @@ -90,24 +93,27 @@ static function loadConfig($config) { 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); -}); - +} /* |-------------------------------------------------------------------------- diff --git a/composer.json b/composer.json index 2dd68f6..0e8acd9 100644 --- a/composer.json +++ b/composer.json @@ -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" diff --git a/web/index.php b/web/index.php index cc18941..c9cbc0b 100644 --- a/web/index.php +++ b/web/index.php @@ -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__))); /* |--------------------------------------------------------------------------