Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

updated to latest Silex

  • Loading branch information...
commit ca1fea8648a6b1f27f15e8b0300a6dffb4f54eb9 1 parent 8fba6b1
Igor authored
3  .gitignore
View
@@ -1,6 +1,3 @@
-dev.config.yml
-prod.config.yml
-test.config.yml
silex.phar
cache
.DS_Store
3  .gitmodules
View
@@ -7,3 +7,6 @@
[submodule "vendor/sfYaml"]
path = vendor/sfYaml
url = http://github.com/fabpot/yaml.git
+[submodule "vendor/symfony/src/Symfony/Component/Finder"]
+ path = vendor/symfony/src/Symfony/Component/Finder
+ url = https://github.com/symfony/Finder.git
125 app.php
View
@@ -9,72 +9,46 @@
*/
require_once __DIR__.'/silex.phar';
-require __DIR__.'/bootstrap.php';
-use Silex\Framework;
+use Silex\Application;
+use Silex\Extension\TwigExtension;
use Symfony\Component\Finder\Finder;
-use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpFoundation\Response;
+use Symfony\Component\HttpKernel\Exception\BaseHttpException;
+use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
-$container->setParameter('app.get_languages', function() {
- $languages = array();
- $finder = new Finder();
- foreach ($finder->name('*.min.js')->in(__DIR__.'/vendor/shjs/lang') as $file) {
- if (preg_match('#sh_(.+).min.js#', basename($file), $matches)) {
- $languages[] = $matches[1];
- }
- }
- return $languages;
-});
-
-$app = $container->get('silex');
-
-$app->before(function() use ($app, $container) {
- $request = $app->getRequest();
- $twig = $container->get('twig');
+$app = new Application();
- $getLanguages = $container->getParameter('app.get_languages');
+require __DIR__.'/bootstrap.php';
+$app->before(function() use ($app) {
// set up some template globals
- $twig->addGlobal('footer', $container->getParameter('app.footer'));
- $twig->addGlobal('base_path', $request->getBasePath());
- $twig->addGlobal('index_url', $request->getBasePath().'/');
- $twig->addGlobal('create_url', $request->getBasePath().'/create');
- $twig->addGlobal('languages', $getLanguages());
+ $app['twig']->addGlobal('base_path', $app['request']->getBasePath());
+ $app['twig']->addGlobal('index_url', $app['request']->getBasePath().'/');
+ $app['twig']->addGlobal('create_url', $app['request']->getBasePath().'/create');
+ $app['twig']->addGlobal('languages', $app['app.languages']);
});
-$app->get('/', function() use ($app, $container) {
- $request = $app->getRequest();
- $twig = $container->get('twig');
-
- $parentId = $request->get('parent', false);
+$app->get('/', function() use ($app) {
+ $parentId = $app['request']->get('parent', false);
$parent = false;
if ($parentId) {
- $pastes = $container->get('mongo.pastes');
+ $pastes = $app['mongo.pastes'];
$parent = $pastes->findOne(array("_id" => $parentId));
}
- $template = $twig->loadTemplate('index.html');
-
- return $template->render(array(
+ return $app['twig']->render('index.html', array(
'paste' => $parent,
));
});
$app->get('/create', function() use ($app) {
- $request = $app->getRequest();
-
- $response = new Response;
- $response->setRedirect($request->getBasePath().'/');
- return $response;
+ return $app->redirect($app['request']->getBasePath());
});
-$app->post('/create', function() use ($app, $container) {
- $request = $app->getRequest();
- $twig = $container->get('twig');
-
- $content = preg_replace('#\\r?\\n#', "\n", (string) $request->get('content', ''));
+$app->post('/create', function() use ($app) {
+ $content = preg_replace('#\r?\n#', "\n", $app['request']->get('content', ''));
$paste = array(
'_id' => substr(hash('sha512', $content . time() . rand(0, 255)), 0, 8),
@@ -83,71 +57,44 @@
);
if ('' === trim($paste['content'])) {
- $errorMsg = 'you must enter some content';
-
- $template = $twig->loadTemplate('index.html');
-
- return $template->render(array(
- 'error_msg' => $errorMsg,
+ return $app['twig']->render('index.html', array(
+ 'error_msg' => 'you must enter some content',
'paste' => $paste,
));
}
- $getLanguages = $container->getParameter('app.get_languages');
-
- $language = (string) $request->get('language', '');
- if (in_array($language, $getLanguages())) {
+ $language = $app['request']->get('language', '');
+ if (in_array($language, $app['app.languages'])) {
$paste['language'] = $language;
}
- $pastes = $container->get('mongo.pastes');
- $pastes->insert($paste);
+ $app['mongo.pastes']->insert($paste);
- $response = new Response;
- $response->setRedirect($request->getBasePath().'/view/'.$paste['_id']);
- return $response;
+ return $app->redirect($app['request']->getBasePath().'/'.$paste['_id']);
});
-$app->get('/view/{id}', function($id) use ($app, $container) {
- $request = $app->getRequest();
- $twig = $container->get('twig');
+$app->get('/about', function() use ($app) {
+ return $app['twig']->render('about.html');
+});
- $pastes = $container->get('mongo.pastes');
- $paste = $pastes->findOne(array("_id" => $id));
+$app->get('/{id}', function($id) use ($app) {
+ $paste = $app['mongo.pastes']->findOne(array("_id" => $id));
if (!$paste) {
throw new NotFoundHttpException('paste not found');
}
- $template = $twig->loadTemplate('view.html');
-
- return $template->render(array(
- 'copy_url' => $request->getBasePath().'/?parent='.$paste['_id'],
+ return $app['twig']->render('view.html', array(
+ 'copy_url' => $app['request']->getBasePath().'/?parent='.$paste['_id'],
'paste' => $paste,
));
-});
-
-$app->get('/about', function() use ($app, $container) {
- $request = $app->getRequest();
- $twig = $container->get('twig');
-
- $template = $twig->loadTemplate('about.html');
-
- return $template->render(array());
-});
-
-$app->error(function(Exception $e) use ($app, $container) {
- $request = $app->getRequest();
- $twig = $container->get('twig');
-
- $code = 500;
- if ($e instanceof NotFoundHttpException) {
- $code = 404;
- }
+})
+->assert('id', '[0-9a-f]{8}');
- $template = $twig->loadTemplate('error.html');
+$app->error(function(Exception $e) use ($app) {
+ $code = ($e instanceof BaseHttpException) ? $e->getStatusCode() : 500;
- return new Response($template->render(array(
+ return new Response($app['twig']->render('error.html', array(
'message' => $e->getMessage(),
)), $code);
});
60 bootstrap.php
View
@@ -8,21 +8,53 @@
* with this source code in the file LICENSE.
*/
-use Symfony\Component\DependencyInjection\ContainerBuilder;
-use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
-use Symfony\Component\ClassLoader\UniversalClassLoader;
-use Symfony\Component\Config\FileLocator;
-
-$loader = new UniversalClassLoader;
-$loader->registerPrefixes(array(
- 'Twig_' => __DIR__ . '/vendor/Twig/lib',
+use Silex\Extension\TwigExtension;
+
+use Symfony\Component\Finder\Finder;
+
+/* prevent direct access */
+if (!$app) {
+ exit;
+}
+
+$app->register(new TwigExtension(), array(
+ 'twig.path' => __DIR__.'/views',
+ 'twig.class_path' => __DIR__.'/vendor/twig/lib',
+ 'twig.options' => array('cache' => __DIR__.'/cache/twig', 'debug' => true),
));
-$loader->register();
-// di container
-$container = new ContainerBuilder();
+$app['autoloader']->registerNamespace('Symfony', __DIR__.'/vendor/symfony/src');
+
+$app['app.languages'] = function() {
+ $languages = array();
+ $finder = new Finder();
+ foreach ($finder->name('*.min.js')->in(__DIR__.'/vendor/shjs/lang') as $file) {
+ if (preg_match('#sh_(.+).min.js#', basename($file), $matches)) {
+ $languages[] = $matches[1];
+ }
+ }
+ return $languages;
+};
+
+$app['app.gc_interval'] = strtotime('24 hours ago');
+$app['footer'] = 'Hosted by <a href="https://affiliates.nexcess.net/idevaffiliate.php?id=1184">Nexcess.net</a>';
+
+$app['app.garbage_collect'] = $app->protect(function() use ($app) {
+ $result = $app['mongo.pastes']->remove(
+ array('createdAt' => array(
+ '$lt' => new MongoDate($app['app.gc_interval']))
+ ),
+ array('safe' => true)
+ );
+
+ return $result['n'];
+});
-$env = isset($_SERVER['APP_ENV']) ? $_SERVER['APP_ENV'] : 'dev';
+$app['mongo.pastes'] = function($app) {
+ return $app['mongo.db']->paste;
+};
-$loader = new YamlFileLoader($container, new FileLocator(__DIR__));
-$loader->load("$env.config.yml");
+$app['mongo.db'] = $app->share(function($app) {
+ $mongo = new Mongo();
+ return $mongo->trashbin;
+});
24 config.example.yml
View
@@ -1,24 +0,0 @@
-parameters:
- app.gc_interval: 24 hours ago
- app.footer: 'Hosted by <a href="https://affiliates.nexcess.net/idevaffiliate.php?id=1184">Nexcess.net</a>'
- mongo.dbname: trashbin
-
-services:
- silex: @silex.framework
- silex.framework:
- class: Silex\Framework
- mongo.pastes:
- class: MongoCollection
- arguments: [@mongo.db, 'paste']
- mongo.db:
- class: MongoDB
- arguments: [@mongo, %mongo.dbname%]
- mongo:
- class: Mongo
- twig: @twig.environment
- twig.environment:
- class: Twig_Environment
- arguments: [@twig.loader, { cache: cache/twig, debug: true }]
- twig.loader:
- class: Twig_Loader_Filesystem
- arguments: [views]
14 garbage_collector.php
View
@@ -8,16 +8,8 @@
* with this source code in the file LICENSE.
*/
-require_once __DIR__.'/silex.phar';
-require __DIR__.'/bootstrap.php';
+$app = require __DIR__.'/app.php';
-$pastes = $container->get('mongo.pastes');
+$deleted = $app['app.garbage_collect']();
-$result = $pastes->remove(
- array('createdAt' => array(
- '$lt' => new MongoDate(strtotime($container->getParameter('app.gc_interval'))))
- ),
- array('safe' => true)
-);
-
-echo "{$result['n']} records deleted\n";
+echo "$deleted records deleted\n";
1  vendor/symfony/src/Symfony/Component/Finder
@@ -0,0 +1 @@
+Subproject commit 707b2fc9b2bb7ccf631f2bfe7d650fd8f0707942
2  vendor/twig
@@ -1 +1 @@
-Subproject commit 05f20f88d2b3496af34d1d99ae66450ce8bca866
+Subproject commit 8fb005220e7590a16c2677ee61bfc5f79c466c97
2  views/layout.html
View
@@ -8,6 +8,6 @@
</head>
<body>
{% block body %}{% endblock %}
- <div id="footer">{{ footer|raw }}</div>
+ <div id="footer">{{ app.footer|raw }}</div>
</body>
</html>
Please sign in to comment.
Something went wrong with that request. Please try again.