PHP Classes

File: bootstrap/app.php

Recommend this page to a friend!
  Classes of Yerfry Ramirez   Basic Controller Framework   bootstrap/app.php   Download  
File: bootstrap/app.php
Role: Example script
Content type: text/plain
Description: Example script
Class: Basic Controller Framework
Framework to route requests configured controllers
Author: By
Last change:
Date: 6 years ago
Size: 2,006 bytes
 

Contents

Class file image Download
<?php

use Framework\Config\Loader;
use
Framework\Config\Environments;
use
Framework\Config\Config;

use
Framework\Http\Builders\RequestBuilder;
use
Framework\Http\Builders\ResponseBuilder;

use
Framework\Http\Request;
use
Framework\Http\Response;

use
Framework\Routing\Router;

use
Framework\MySQL\Database;
use
Framework\MySQL\Connection;

// require __DIR__ . '/../resources/Smarty/Smarty.class.php';
require __DIR__ . '/../autoloader.php';

$App = new App();

$App->register(Router::CLASS, function (Request $request) {
   
$router = new Router($request);

   
$middleware1 = function () {
        echo
"middleware 1";
    };

   
$middleware2 = function () {
        echo
"middleware 2";
    };

   
$router->any('/', function () {
        echo
'index page';
    });

   
$router->get('/:all', function ($all) {
        echo
$all;
    });

   
$router->get('/(post|posts)/:slug/:id', function($slug, $id) {
        echo
"You are seeing the post nš $id, with title: $slug";
    });

   
$router->run();
});

$App->register(Request::CLASS, function () {
   
$builder = new RequestBuilder();
   
$builder->addContentParser('application/json', function () {
        if(!(
$data = json_decode(file_get_contents('php://input'), true))) {
           
$data = [];
        }

        return
$data;
    });

    return
$builder->create(['get' => $_GET, 'files' => $_FILES, 'server' => $_SERVER, 'post' => $_POST, 'cookies' => $_COOKIE]);
});

$App->register(Database::CLASS, function () {
   
$config = $App->resolve(Config::CLASS, 'mysql');
   
    return
Database::getInstance(
        new
Connection(
           
$config->get('hostname'),
           
$config->get('username'),
           
$config->get('password'),
           
$config->get('database'),
           
$config->get('options')
        )
    );
});

$configDirPath = __DIR__ . '/../bootstrap/config';
$App->register(Config::CLASS, function (
   
string $environment = 'default',
    array
$settings = array(
       
'local' => array(
           
'ubuntu',
           
'localhost',
           
'macbook',
           
'windows'
       
)
    )
) use (
$configDirPath) {
    return new
Config(
        new
Loader($configDirPath),
        new
Environments($settings)
    ,
$environment);
});
?>