Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/dev' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
fkeloks committed Aug 15, 2017
2 parents 236f8f4 + 7b9df70 commit 9381846
Show file tree
Hide file tree
Showing 24 changed files with 600 additions and 395 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ TODO\.txt
cache/twig
cache/router

storage/databases/*
storage/logs/*

_tmp
27 changes: 27 additions & 0 deletions app/controllers/dbController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace App\Controllers;

/**
* Class mysqlController
* @package App\Controllers
*/
class dbController extends \BDSCore\BaseController
{
private $app;


public function __construct(\Psr\Http\Message\RequestInterface $request, \Psr\Http\Message\ResponseInterface $response)
{
parent::__construct($request, $response);

$this->app = new \App\Models\dbModel();
}


public function index()
{
$this->render('db.twig', ['bookmarks' => $this->app->bookmarkList()]);
}

}
24 changes: 24 additions & 0 deletions app/models/dbModel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
namespace App\Models;
use BDSCore;

class dbModel
{
private $database;

public function __construct()
{
$this->database = new BDSCore\Database\Database();
}


public function bookmarkList()
{
$sql = 'SELECT id, url, nom FROM bookmark ORDER BY id ASC';

$result = $this->database->fetchAll($sql);

return $result;
}

}
20 changes: 20 additions & 0 deletions app/views/db.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{% extends 'globals/default.twig' %}

{% block title %}DB{% endblock %}

{% block body %}

<div class="block-center">
<h1>Test database</h1>
</div>

<div class="block_table">
{% for key, bookmark in bookmarks %}

#{{ bookmark.id }} <a href="http://{{ bookmark.url }}">{{ bookmark.nom }}</a><br/>

{% endfor %}
</div>

{% endblock %}

4 changes: 2 additions & 2 deletions app/views/errors/error500.twig
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

<span class="header"></span>

<div class="block-center">
<div style="margin-top: 80px;" class="">
<h1>Error 500 : Internal server error</h1>
<span>
<span style="font-family: 'Courier New'; font-size: 0.80em;">
{% if(exception == false) %}
Unexpected error... <br>
An error occurred and your request couldn't be completed. Please try again.
Expand Down
10 changes: 5 additions & 5 deletions config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
'routerCache' => false,

'twigViews' => 'app/views',
'twigCache' => 'cache/twig',
'twigCache' => false, //'cache/twig',

'db_driver' => 'mysql',
'db_driver' => 'postgresql',
'db_host' => 'localhost',
'db_name' => 'BDS_Framework',
'db_username' => 'root',
'db_password' => null,
'db_name' => 'framework',
'db_username' => 'stephane',
'db_password' => 'stephane',

];
7 changes: 4 additions & 3 deletions config/router.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
],

'routes' => [
'homePage' => ['get', '/', 'HomeController@index'],
'helloPage' => ['get', '/hello/{name:\w+}', 'HelloController@index'],
'testPage' => ['get', '/test', 'TestController@index'],
'homePage' => ['get', '/', 'homeController@index'],
'helloPage' => ['get', '/hello/{name:\w+}', 'helloController@index'],
'testPage' => ['get', '/test', 'testController@index'],
'dbPage' => ['get', '/db', 'dbController@index'],
]

];
66 changes: 44 additions & 22 deletions core/Database/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,56 +14,71 @@ class Database
*/
private $pdo;


/**
* Database constructor.
* @param string|null $driver
* @param string|null $databaseName
* @throws DatabaseException
*/
public function __construct(string $driver = null, string $databaseName = null) {
if ($driver == null && $databaseName == null) {
throw new DatabaseException('The two parameters of the functions can not both be null');
}
if (!is_string($driver)) {
$driver = \BDSCore\Config\Config::getConfig('db_driver');
}

return $this->connect($driver, $databaseName);
public function __construct()
{
return $this->connect();
}


/**
* @param $driver
* @param $databaseName
* @return \PDO
* @throws DatabaseException
*/
public function connect($driver, $databaseName) {
if ($driver == 'sqlite') {
if ($databaseName == null) {
public function connect()
{
$params = [
'driver' => \BDSCore\Config\Config::getConfig('db_driver'),
'hostname' => \BDSCore\Config\Config::getConfig('db_host'),
'database' => \BDSCore\Config\Config::getConfig('db_name'),
'username' => \BDSCore\Config\Config::getConfig('db_username'),
'password' => \BDSCore\Config\Config::getConfig('db_password')
];

if ($params['driver'] == 'sqlite')
{
if ($params['database'] == null) {
throw new DatabaseException('The name of the database must be specified');
}
$this->pdo = new \PDO("sqlite:./storage/databases/{$databaseName}.sqlite");
} elseif ($driver == 'mysql') {
$params = [
'host' => \BDSCore\Config\Config::getConfig('db_host'),
'name' => \BDSCore\Config\Config::getConfig('db_name'),
'username' => \BDSCore\Config\Config::getConfig('db_username'),
'password' => \BDSCore\Config\Config::getConfig('db_password')
];

$this->pdo = new \PDO("sqlite:./storage/databases/{$params['database']}.sqlite");
}
elseif ($params['driver'] == 'mysql')
{
try {
$this->pdo = new \PDO("mysql:host={$params['host']};dbname={$params['name']};charset=UTF8", $params['username'], $params['password']);
$this->pdo = new \PDO("mysql:host={$params['hostname']};dbname={$params['database']};charset=UTF8", $params['username'], $params['password']);
} catch (\Exception $e) {
throw new DatabaseException($e->getMessage());
}
} else {
}
elseif ($params['driver'] == 'postgresql')
{
try {
$this->pdo = new \PDO("pgsql:dbname={$params['database']};host={$params['hostname']}", $params['username'], $params['password']);
} catch (\DatabaseException $e) {
throw new DatabaseException($e->getMessage());
}
}
else
{
throw new DatabaseException('Use of an unknown driver name in the \BDSCore\Database() class.');
}

$this->pdo->setAttribute(\PDO::ATTR_DEFAULT_FETCH_MODE, \PDO::FETCH_OBJ);
$this->pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);

return $this->pdo;
}


/**
* @param string $query
* @param array $params
Expand All @@ -77,13 +92,15 @@ public function query(string $query, array $params = [], string $entity = null)
$query = $this->pdo->prepare($query);
$query->execute($params);
}

if ($entity) {
$query->setFetchMode(\PDO::FETCH_CLASS, $entity);
}

return $query;
}


/**
* @param string $query
* @param array $params
Expand All @@ -94,6 +111,7 @@ public function fetch(string $query, array $params = [], string $entity = null)
return $this->query($query, $params, $entity)->fetch();
}


/**
* @param string $query
* @param array $params
Expand All @@ -104,6 +122,7 @@ public function fetchAll(string $query, array $params = [], string $entity = nul
return $this->query($query, $params, $entity)->fetchAll();
}


/**
* @param string $query
* @param array $params
Expand All @@ -113,13 +132,15 @@ public function fetchColumn(string $query, array $params = [], string $entity =
return $this->query($query, $params, $entity)->fetchColumn();
}


/**
* @return int
*/
public function lastInsertId(): int {
return $this->pdo->lastInsertId();
}


/**
* @param string|null $tableName
* @param $collumns
Expand All @@ -144,6 +165,7 @@ public function insert(string $tableName = null, $collumns, $values) {
}
}


/**
* @param string|null $tableName
* @param null $collums
Expand Down
5 changes: 4 additions & 1 deletion core/Database/DatabaseException.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,8 @@
*/
class DatabaseException extends \Exception
{

public function test()
{
return 'ERROR CONNEXIOn DATABASE';
}
}
3 changes: 1 addition & 2 deletions public/css/app.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 8 additions & 1 deletion public/css/app.scss
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ body {
margin-top: 7%;
color: #3B445B;
text-align: center;
font-size: 2em;
font-size: 1.8em;
height: 250px;
width: 85%;
a {
Expand Down Expand Up @@ -58,6 +58,13 @@ body {
}
}

.block_table {
width: 90%;
a {
text-decoration: none;
}
}

footer {
position: fixed;
bottom: 20px;
Expand Down
14 changes: 14 additions & 0 deletions storage/databases/db_mysql.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

CREATE DATABASE framework;

CREATE TABLE bookmark
(
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
url VARCHAR(255) NOT NULL,
nom VARCHAR(100) NOT NULL,
CONSTRAINT bookmark_id_uindex UNIQUE (id)
);

INSERT INTO bookmark (url, nom) VALUES ('www.php.net', 'PHP');
INSERT INTO bookmark (url, nom) VALUES ('www.postgresql.org', 'PostgreSQL');
INSERT INTO bookmark (url, nom) VALUES ('www.debian.org', 'Debian');
19 changes: 19 additions & 0 deletions storage/databases/db_postgresql.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

CREATE DATABASE framework
WITH OWNER = stephane
ENCODING = 'UTF8'
LC_COLLATE = 'fr_FR.UTF-8'
LC_CTYPE = 'fr_FR.UTF-8'
CONNECTION LIMIT = -1;

CREATE TABLE bookmark
(
id SMALLSERIAL,
url VARCHAR(255) NOT NULL,
nom VARCHAR(100) NOT NULL,
PRIMARY KEY (id)
);

INSERT INTO bookmark (url, nom) VALUES ('www.php.net', 'PHP');
INSERT INTO bookmark (url, nom) VALUES ('www.postgresql.org', 'PostgreSQL');
INSERT INTO bookmark (url, nom) VALUES ('www.debian.org', 'Debian');
Loading

0 comments on commit 9381846

Please sign in to comment.