Skip to content

Commit

Permalink
Correctifs mineur sur l'ajout d'une page Database pour l'exemple
Browse files Browse the repository at this point in the history
  • Loading branch information
fkeloks committed Aug 15, 2017
1 parent 9381846 commit dc078a5
Show file tree
Hide file tree
Showing 14 changed files with 117 additions and 108 deletions.
33 changes: 33 additions & 0 deletions app/controllers/DbController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace App\Controllers;

class DbController extends \BDSCore\BaseController
{

/**
* @var \App\Models\dbModel
*/
private $app;

/**
* DbController constructor.
* @param \Psr\Http\Message\RequestInterface $request
* @param \Psr\Http\Message\ResponseInterface $response
*/
public function __construct(\Psr\Http\Message\RequestInterface $request, \Psr\Http\Message\ResponseInterface $response) {
parent::__construct($request, $response);

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

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

}
27 changes: 0 additions & 27 deletions app/controllers/dbController.php

This file was deleted.

34 changes: 34 additions & 0 deletions app/models/DbModel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace App\Models;

/**
* Class DbModel
* @package App\Models
*/
class DbModel
{

/**
* @var \BDSCore\Database\Database
*/
private $database;

/**
* DbModel constructor.
*/
public function __construct() {
$this->database = new \BDSCore\Database\Database();
}

/**
* @return array
*/
public function bookmarkList() {
$sql = 'SELECT id, url, nom FROM bookmark ORDER BY id ASC';
$result = $this->database->fetchAll($sql);

return $result;
}

}
24 changes: 0 additions & 24 deletions app/models/dbModel.php

This file was deleted.

20 changes: 10 additions & 10 deletions app/views/db.twig
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@

{% block body %}

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

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

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

{% endfor %}
</div>

{% endblock %}
{% 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 style="margin-top: 80px;" class="">
<div class="block-center">
<h1>Error 500 : Internal server error</h1>
<span style="font-family: 'Courier New'; font-size: 0.80em;">
<span>
{% 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' => false, //'cache/twig',
'twigCache' => 'cache/twig',

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

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

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

];
48 changes: 18 additions & 30 deletions core/Database/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace BDSCore\Database;

use BDSCore\Config\Config;

/**
* Class Database
* @package BDSCore\Database
Expand All @@ -14,61 +16,54 @@ class Database
*/
private $pdo;


/**
* Database constructor.
* @param string|null $driver
* @param string|null $databaseName
* @throws DatabaseException
*/
public function __construct()
{
public function __construct() {
return $this->connect();
}


/**
* @param $driver
* @param $databaseName
* @return \PDO
* @throws DatabaseException
*/
public function connect()
{
public function connect() {
$config = Config::getAllConfig();
$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')
'driver' => $config['db_driver'],
'hostname' => $config['db_host'],
'database' => $config['db_name'],
'username' => $config['db_username'],
'password' => $config['db_password']
];

if ($params['driver'] == 'sqlite')
{
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/{$params['database']}.sqlite");
}
elseif ($params['driver'] == 'mysql')
{
Config::getDirectoryRoot();
$this->pdo = new \PDO(
'sqlite:' . Config::getDirectoryRoot("/storage/databases/{$params['database']}.sqlite")
);
} elseif ($params['driver'] == 'mysql') {
try {
$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());
}
}
elseif ($params['driver'] == 'postgresql')
{
} 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
{
} else {
throw new DatabaseException('Use of an unknown driver name in the \BDSCore\Database() class.');
}

Expand All @@ -78,7 +73,6 @@ public function connect()
return $this->pdo;
}


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


/**
* @param string $query
* @param array $params
Expand All @@ -111,7 +104,6 @@ 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 @@ -122,7 +114,6 @@ 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 @@ -132,15 +123,13 @@ 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 @@ -165,7 +154,6 @@ public function insert(string $tableName = null, $collumns, $values) {
}
}


/**
* @param string|null $tableName
* @param null $collums
Expand Down
5 changes: 1 addition & 4 deletions core/Database/DatabaseException.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,5 @@
*/
class DatabaseException extends \Exception
{
public function test()
{
return 'ERROR CONNEXIOn DATABASE';
}

}
2 changes: 1 addition & 1 deletion core/Functions/Functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* @return bool
*/
function debug($item): bool {
\BDSCore\Debug\debugBar::pushElement('Debug#' . substr(uniqid(), 8), $item);
\BDSCore\Debug\DebugBar::pushElement('Debug#' . substr(uniqid(), 8), $item);

return \BDSCore\Debug\Debugger::debug($item);
}
Expand Down
2 changes: 1 addition & 1 deletion public/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@
set_error_handler([$app, 'catchException']);
set_exception_handler([$app, 'catchException']);

require('../core/functions/Functions.php');
require('../core/Functions/Functions.php');

$app->run($request, $response, $timeStart);
4 changes: 4 additions & 0 deletions storage/databases/db_mysql.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@

/*
BDS Framework, For example only
*/

CREATE DATABASE framework;

CREATE TABLE bookmark
Expand Down
4 changes: 4 additions & 0 deletions storage/databases/db_postgresql.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@

/*
BDS Framework, For example only
*/

CREATE DATABASE framework
WITH OWNER = stephane
ENCODING = 'UTF8'
Expand Down

0 comments on commit dc078a5

Please sign in to comment.