Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

initial release

Added some basic functions, there is still alot stuff to do.
At this state it's a good base for everyone that starts programming a blog
  • Loading branch information...
commit caec7d06c548ceb8e867021c4c56b3f45e2c508a 0 parents
authored January 23, 2012
8  .htaccess
... ...
@@ -0,0 +1,8 @@
  1
+<IfModule mod_rewrite.c>
  2
+    Options -MultiViews
  3
+
  4
+    RewriteEngine On
  5
+   #RewriteBase /path/to/app
  6
+    RewriteCond %{REQUEST_FILENAME} !-f
  7
+    RewriteRule ^ index.php [L]
  8
+</IfModule>
16  index.php
... ...
@@ -0,0 +1,16 @@
  1
+<?php
  2
+    error_reporting(E_ALL);
  3
+    
  4
+    if(!isset($here)) {
  5
+        $here = __DIR__;
  6
+    }
  7
+    
  8
+    if(!defined('LIB')) {
  9
+        define('LIB', __DIR__.'/lib');
  10
+    }
  11
+    
  12
+    include LIB.'/constants.php';
  13
+    include LIB.'/publish.php';
  14
+    
  15
+    blog_run();
  16
+?>
61  lib/bootstrap.php
... ...
@@ -0,0 +1,61 @@
  1
+<?php
  2
+require 'silex.phar';
  3
+
  4
+/** @var $app Silex\Application */
  5
+$app = new Silex\Application();
  6
+
  7
+/**
  8
+ * Settins 
  9
+ */
  10
+
  11
+/**
  12
+ * Turn Debug on or off 
  13
+ */
  14
+(DEBUG == 1) ? $app['debug'] = true : $app['debug'] = false;
  15
+
  16
+/**
  17
+ * Twig as Tempatesystem 
  18
+ */
  19
+
  20
+$app['autoloader']->registerNamespace('SilexExtension', __DIR__ . '/../vendor/silex-extension/src');
  21
+$app['autoloader']->register();
  22
+$app->register(new Silex\Provider\TwigServiceProvider(), array(
  23
+    'twig.class_path' => __DIR__.'/../vendor/twig/lib',
  24
+    'twig.path'     => __DIR__.'/../tmpl/'
  25
+));
  26
+
  27
+use Symfony\Component\ClassLoader\UniversalClassLoader;
  28
+$loader = new UniversalClassLoader();
  29
+$loader->registerNameSpace('entities', __DIR__);
  30
+$loader->register();
  31
+
  32
+/**
  33
+ * Symfony Validator Extension 
  34
+ */
  35
+
  36
+$app->register(new Silex\Provider\ValidatorServiceProvider(), array(
  37
+    'validator.class_path' => __DIR__.'/../vendor/symfony/src'
  38
+));
  39
+
  40
+/**
  41
+ * MongoDB 
  42
+ */
  43
+$app->register(new SilexExtension\MongoDbExtension(), array(
  44
+    'mongodb.class_path'    =>  __DIR__.'/../vendor/mongodb/lib',
  45
+    'mongodb.connection'    =>  array(
  46
+        'configuration' =>  function($configuration) {
  47
+        $configuration->setLoggerCallable(function($logs){
  48
+            (DEBUG == 1) ? print_r($logs) : null;
  49
+          });
  50
+        }
  51
+    )
  52
+));
  53
+
  54
+/** 
  55
+ * Register some needed providers 
  56
+ * You can simply add more here
  57
+ */
  58
+
  59
+$app->register(new Silex\Provider\UrlGeneratorServiceProvider());
  60
+
  61
+?>
5  lib/constants.php
... ...
@@ -0,0 +1,5 @@
  1
+<?php
  2
+define('DEBUG', 0);
  3
+define('MONGODB', 'blog');
  4
+
  5
+?>
105  lib/entities/Article.php
... ...
@@ -0,0 +1,105 @@
  1
+<?php
  2
+
  3
+namespace entities;
  4
+
  5
+/**
  6
+ * Description of Article
  7
+ *
  8
+ * @author pierre
  9
+ */
  10
+class Article {
  11
+    private $id;
  12
+    private $author;
  13
+    private $title;
  14
+    private $body;
  15
+    private $date;
  16
+    private $tags = array();
  17
+    
  18
+    public function save($app) {
  19
+        /* @var $db \Doctrine\MongoDB\Connection */
  20
+        $db =  $app['mongodb'];
  21
+        /* @var $collection \Doctrine\MongoDB\LoggableCollection */
  22
+        $collection = $db->selectCollection(MONGODB, 'articles');
  23
+        $self = array(
  24
+            'Author' => $this->author,
  25
+            'Title'  => $this->title,
  26
+            'Body'   => $this->body,
  27
+            'Date'   => $this->date,
  28
+            'Tags'   => $this->tags
  29
+        );
  30
+        $collection->insert($self);
  31
+    }
  32
+    
  33
+    /**
  34
+     *
  35
+     * @param type $app
  36
+     * @return \Doctrine\MongoDB\Cursor
  37
+     */
  38
+    public function getAll(\Silex\Application $app) {
  39
+        /* @var $db \Doctrine\MongoDB\Connection */
  40
+        $db =  $app['mongodb'];
  41
+        /* @var $collection \Doctrine\MongoDB\LoggableCollection */
  42
+        $collection = $db->selectCollection(MONGODB, 'articles');
  43
+        return $collection->find();
  44
+    }
  45
+    
  46
+    public function getOne(\Silex\Application $app, $title) {
  47
+        /* @var $db \Doctrine\MongoDB\Connection */
  48
+        $db =  $app['mongodb'];
  49
+        /* @var $collection \Doctrine\MongoDB\LoggableCollection */
  50
+        $collection = $db->selectCollection(MONGODB, 'articles');
  51
+        return $collection->findOne(array('Title' => $title));
  52
+    }
  53
+    
  54
+    public function getId() {
  55
+        return $this->id;
  56
+    }
  57
+
  58
+    public function setId($id) {
  59
+        $this->id = $id;
  60
+    }
  61
+
  62
+    public function getAuthor() {
  63
+        return $this->author;
  64
+    }
  65
+
  66
+    public function setAuthor($author) {
  67
+        $this->author = $author;
  68
+    }
  69
+
  70
+    public function getTitle() {
  71
+        return $this->title;
  72
+    }
  73
+
  74
+    public function setTitle($title) {
  75
+        $this->title = $title;
  76
+    }
  77
+
  78
+    public function getBody() {
  79
+        return $this->body;
  80
+    }
  81
+
  82
+    public function setBody($body) {
  83
+        $this->body = $body;
  84
+    }
  85
+
  86
+    public function getDate() {
  87
+        return $this->date;
  88
+    }
  89
+
  90
+    public function setDate($date) {
  91
+        $this->date = $date;
  92
+    }
  93
+
  94
+    public function getTags() {
  95
+        return $this->tags;
  96
+    }
  97
+
  98
+    public function setTags($tags) {
  99
+        $this->tags = $tags;
  100
+    }
  101
+
  102
+
  103
+}
  104
+
  105
+?>
10  lib/navigation.php
... ...
@@ -0,0 +1,10 @@
  1
+<?php
  2
+include 'bootstrap.php';
  3
+
  4
+/* @var $app Silex\Application */
  5
+$app->get('/navigation', function() use($app) {
  6
+    return $app['twig']->render('Navigation.twig',array(
  7
+        'navlinks' => array($app['url_generator']->generate('Home'))
  8
+    ));
  9
+});
  10
+?>
49  lib/publish.php
... ...
@@ -0,0 +1,49 @@
  1
+<?php
  2
+    include 'bootstrap.php';
  3
+    use entities\Article;
  4
+    
  5
+    function get_articles() {
  6
+        /* @var $app Silex\Application */
  7
+        global $app;
  8
+        
  9
+        $collection = 'articles';
  10
+        $app->get('/', function() use($app, $collection) {
  11
+            $article = new Article();
  12
+            /* @var $cursor Doctrine\MongoDB\LoggableCursor */
  13
+            $cursor = $article->getAll($app);
  14
+            
  15
+            $posts = '';
  16
+            foreach($cursor->toArray() as $post) {
  17
+                /**
  18
+                 * Clean Code :)
  19
+                 */
  20
+                $posts[] = $post;
  21
+            }
  22
+            
  23
+            return $app['twig']->render('Articles.twig', array('articles' => $posts));
  24
+            
  25
+        });
  26
+    }
  27
+    
  28
+    function get_article($title){
  29
+        global $app;
  30
+        
  31
+        /** Revert Title */
  32
+        $original_title = str_replace('_', ' ', $title);
  33
+        $article = new Article();
  34
+        $oneArticle = $article->getOne($app,$original_title);
  35
+        return $app['twig']->render('Post.twig', array('a' => $oneArticle));
  36
+    }
  37
+
  38
+    function blog_run() {
  39
+        global $app;
  40
+        //var_dump($app->get('/article/{id}', show_article($id))->assert('id', '\d+'));
  41
+        
  42
+        $app->get('/{pageName}', get_articles())->bind('home');
  43
+        $app->get('/article/{article_title}', function($article_title){
  44
+                    return get_article($article_title);
  45
+        });
  46
+        $app->run();
  47
+    }
  48
+
  49
+?>
BIN  lib/silex.phar
Binary file not shown
18  tmpl/Admin.twig
... ...
@@ -0,0 +1,18 @@
  1
+{% from 'forms.twig' import input as forms %}
  2
+<html>
  3
+    <head>
  4
+    {% block head %}
  5
+            <title>Bitte einloggen</title>        
  6
+    {% endblock %}
  7
+    </head>
  8
+    <body>
  9
+        <p>
  10
+        <form action="" method="POST">
  11
+        {{ forms('login', user) }}
  12
+        {{ forms('password', password, 'password') }}
  13
+        {{ forms('submit', "Login", 'submit') }}
  14
+        </form>
  15
+            </p>
  16
+    {{log}}
  17
+    </body>
  18
+</html>
12  tmpl/Articles.twig
... ...
@@ -0,0 +1,12 @@
  1
+{# empty Twig template #}
  2
+
  3
+{% extends "Main.twig" %}
  4
+
  5
+{% block articles %}
  6
+{% for a in articles %}
  7
+<div id='articlehead'>
  8
+    <h2><a href="/article/{{a.Title|replace(' ', '_')}}">{{ a.Title }}</a></h2> :: <small>{{ a.Author }}</small> :: {{ a.Date|date("d/m/Y @ H:t") }}
  9
+</div>
  10
+<p id='body'>{{a.Body}}</p>
  11
+{% endfor %}
  12
+{% endblock %}
12  tmpl/Main.twig
... ...
@@ -0,0 +1,12 @@
  1
+<html>
  2
+    <head>
  3
+        {% include 'misc/head.twig' %}
  4
+        <title>Nice Blog {{ pagename }}</title>        
  5
+    </head>
  6
+    <body>
  7
+        <div id="content">
  8
+        {% block articles %}
  9
+        {% endblock %}
  10
+        </div>
  11
+    </body>
  12
+</html>
10  tmpl/Post.twig
... ...
@@ -0,0 +1,10 @@
  1
+{# empty Twig template #}
  2
+
  3
+{% extends "Main.twig" %}
  4
+
  5
+{% block articles %}
  6
+<div id='articlehead'>
  7
+    <h2><a href="/article/{{a.Title|replace(' ', '_')}}">{{ a.Title }}</a></h2> :: <small>{{ a.Author }}</small> :: {{ a.Date|date("d/m/Y @ H:t") }}
  8
+</div>
  9
+<p id='body'>{{a.Body}}</p>
  10
+{% endblock %}
10  tmpl/misc/css.twig
... ...
@@ -0,0 +1,10 @@
  1
+<style type="text/css">
  2
+        html{color:#000;background:#FFF;}body,div,dl,dt,dd,ul,ol,li,h1,h2,h3,h4,h5,h6,pre,code,
  3
+        form,fieldset,legend,input,textarea,p,
  4
+        blockquote,th,td{margin:0;padding:0;}table{border-collapse:collapse;border-spacing:0;}fieldset,img{border:0;}address,caption,cite,code,dfn,em,strong,th,var{font-style:normal;font-weight:normal;}li{list-style:none;}caption,th{text-align:left;}h1,h2,h3,h4,h5,h6{font-size:100%;font-weight:normal;display:inline}q:before,q:after{content:'';}abbr,acronym{border:0;font-variant:normal;}sup{vertical-align:text-top;}sub{vertical-align:text-bottom;}input,textarea,select{font-family:inherit;font-size:inherit;font-weight:inherit;}input,textarea,select{*font-size:100%;}legend{color:#000;}
  5
+
  6
+        html { background: #eee; padding: 10px }
  7
+        body { font: 11px Verdana, Arial, sans-serif; color: #333 }
  8
+        img { border: 0; }
  9
+        #content { width:970px; margin:0 auto; }
  10
+    </style>
3  tmpl/misc/forms.twig
... ...
@@ -0,0 +1,3 @@
  1
+{% macro input(name, value, type, size) %}
  2
+    <input type="{{ type|default('text') }}" name="{{ name }}" value="{{ value|e }}" size="{{ size|default(20) }}" />
  3
+{% endmacro %}
3  tmpl/misc/head.twig
... ...
@@ -0,0 +1,3 @@
  1
+{% include "misc/css.twig" %}
  2
+<meta name="keywords" content="{{ keywords }}" />
  3
+<meta name="description" content="{{ description }}" />

0 notes on commit caec7d0

Please sign in to comment.
Something went wrong with that request. Please try again.