1st commit

This commit is contained in:
Наиль Мазитов 2024-05-15 14:00:13 +05:00
commit b146e3b554
11 changed files with 191 additions and 0 deletions

4
.env Normal file
View File

@ -0,0 +1,4 @@
DB_HOST=localhost
DB_USER=root
DB_PASSWORD=
DB_NAME=php-task1

0
.env.example Normal file
View File

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
.env
.idea

View File

@ -0,0 +1,14 @@
<?php
namespace ps\app\Controllers;
use ps\STE\STEView;
class Controller
{
protected function getViewAction($filename)
{
$view = new STEView();
$view->viewPath = 'views/';
$view->render(''.$filename.'.php');
}
}

View File

@ -0,0 +1,14 @@
<?php
namespace ps\app\Controllers;
class LoginController extends Controller
{
public $filename = 'login';
public function actionGetLoginForm()
{
parent::getViewAction($this->filename);
}
}

View File

@ -0,0 +1,11 @@
<?php
namespace ps\app\Controllers;
class PostsController
{
public function getPosts()
{
}
}

19
bootstrap/db.php Normal file
View File

@ -0,0 +1,19 @@
<?php
require "vendor/autoload.php";
use Dotenv\Dotenv;
use Illuminate\Database\Capsule\Manager as Capsule;
$dotenv = Dotenv::createImmutable(ROOT_PATH);
$dotenv->load();
$capsule = new Capsule;
$capsule->addConnection([
"driver" => "mysql",
"host" => $_ENV['DB_HOST'],
"database" => $_ENV['DB_NAME'],
"username" => $_ENV['DB_USER'],
"password" => $_ENV['DB_PASSWORD']
]);
$capsule->setAsGlobal();
$capsule->bootEloquent();

66
index.php Normal file
View File

@ -0,0 +1,66 @@
<?php
ini_set("display_errors", 1);
error_reporting(-1);
const ROOT_PATH = __DIR__;
const BASE_URL = "http://php-task1";
include __DIR__ . '/vendor/autoload.php';
include __DIR__ . '/bootstrap/db.php';
use Phroute\Phroute\RouteCollector;
use Phroute\Phroute\Dispatcher;
use Illuminate\Database\Eloquent;
use Illuminate\Database\Eloquent\Model;
use ps\app\Models\Posts;
function processInput($uri){
$uri = urldecode(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH));
return $uri;
}
function processOutput($response){
echo json_encode($response);
}
// function getPDOInstance(){
// return new PDO('mysql:host=localhost;dbname=booksapi;charset=utf8', 'root', '');
// }
$router = new RouteCollector();
$router->get('/', function(){
return 'Like at Home!';
});
$router->get('posts', function(){
echo Posts::all();
});
$router->get('login', [\ps\app\Controllers\LoginController::class, 'actionGetLoginForm']);
$dispatcher = new Dispatcher($router->getData());
try {
$response = $dispatcher->dispatch($_SERVER['REQUEST_METHOD'], processInput($_SERVER['REQUEST_URI']));
} catch (Phroute\Exception\HttpRouteNotFoundException $e) {
var_dump($e);
die();
} catch (Phroute\Exception\HttpMethodNotAllowedException $e) {
var_dump($e);
die();
}
processOutput($response);

3
routes.php Normal file
View File

@ -0,0 +1,3 @@
<?php
use app\models\Posts;

View File

@ -0,0 +1,56 @@
<?php
namespace ps\STE;
class STEView
{
public string $viewPath = '';
public bool|string $layout = false;
public function __construct()
{
}
public function render(string $view, array $data = []): void
{
$content = $this->createContent($view, $data);
echo $content;
}
public function fetch(string $view, array $data = []): false|string
{
$content = $this->createContent($view, $data);
return $content;
}
private function createContent(string $view, array $data = []): false|string
{
ob_start();
foreach ($data as $key => $datum){
${"$key"} = $datum;
}
include ($this->viewPath . $view);
$content = ob_get_contents();
ob_end_clean ();
ob_start();
$file_content = $content;
if ($this->layout){
if (file_exists($this->viewPath . $this->layout)){
include ($this->viewPath . $this->layout);
$file_content = ob_get_contents();
}
}
ob_end_clean ();
return $file_content;
}
}

2
views/login.php Normal file
View File

@ -0,0 +1,2 @@
<?php
echo 'LoginPage';