Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@
use App\Router;
use App\ClassLoader;

$debug = true;

set_exception_handler(function($exception) {
GLOBAL $debug;
if ($debug === true) {
throw $exception;
}
Response::JsonResponse(
[
'message' => $exception->getMessage(),
Expand Down
51 changes: 46 additions & 5 deletions src/ClassLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,14 @@ private function loadFile(string $className, ?string $previousClassName = null):
$className = str_replace('.php', '', $className);
$className = str_replace(' ', '', $className);
$fileName = str_replace('App', 'src', implode('/', $classExploded)). '.php';
$fileName = str_replace(' {', '', $fileName);


if (!array_key_exists($fileName, $this->filesLoaded)) {
$file = file_get_contents($fileName) ?? '';
$file = '';
if(file_exists($fileName)){
$file = file_get_contents($fileName);
}
if (str_contains($file, 'use ')) {
$this->getUse($file);
}
Expand All @@ -102,6 +107,7 @@ private function loadFile(string $className, ?string $previousClassName = null):
$this->filesLoaded[$fileName] = true;
return true;
} catch (\Error $error) {

if(str_contains($file, 'extends')) {
$classToLoad = explode('extends ', $file);
$classToLoad = explode("\r", $classToLoad[1]);
Expand All @@ -113,11 +119,10 @@ private function loadFile(string $className, ?string $previousClassName = null):
$this->loadFile(str_replace($class, $classToLoad[0], $className), $previousClassName);
};
}

return false;
}
}
return false;
return true;
}

private function getUse(string $file)
Expand All @@ -140,12 +145,48 @@ private function getInterface(string $file, $className)
}
}

//TODO generator for namespaces
private function loadEntities()
{
$loads = [];
foreach (glob("src/Entity/*.php") as $filename)
{
$this->filesLoaded[$filename] = true;
include $filename;

$this->loadFile($this->getFullClassName($filename));
$loads[] = $this->getFullClassName($filename);
//// $className =
// $className = str_replace('.php', '', $className);
// $className = str_replace(' ', '', $className);
// $fileName = str_replace('App', 'src', implode('/', $filename)). '.php';
// $this->filesLoaded[$filename] = true;
// include $filename;
}

// dd($this->filesLoaded);
// dd(glob("src/Entity/*.php"));
// dd(get_declared_classes());
dd($loads);
}

private function getFullClassName(string $fileName): string
{
$fileContents = file_get_contents($fileName);

$tokens = token_get_all($fileContents);

$namespace = 'GLOBAL';
foreach ($tokens as $token) {
if (!is_string($token)) {
list($id, $text) = $token;
if ($id == T_NAME_QUALIFIED) {
$namespace = $text;
}
if ($id == T_STRING) {
return $namespace.'\\'.$text;
}
}
}

return '';
}
}
9 changes: 6 additions & 3 deletions src/Controller/DatabaseController.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ function init(): Response {

$this->cache = $entityList;

$this->saveCache();
// $this->saveCache();

$sql = $this->generateSQL($entityClasses);

if($sql !== "") {
$this->databaseRepository->customQuery($sql);
// $this->databaseRepository->customQuery($sql);
}

return Response::JsonResponse([
Expand Down Expand Up @@ -79,9 +79,12 @@ private function generateSQL(array $entityChanges) {
foreach($entityChanges['new'] as $entityName => $entityValue) {
$_sql = 'CREATE TABLE ' . $entityName. '(';

//TODO NULL VALUES ALLOW
foreach ($entityValue as $column => $dataType) {
$nullable = str_contains($dataType, '?');
$dataType = str_replace('?', '', $dataType);
if (array_key_exists($dataType, $this->mapping)) {
$_sql .= $column . ' ' . $this->mapping[$dataType] . ',';
$_sql .= $column . ' ' . $this->mapping[$dataType] . ($nullable ? '' : ' NOT NULL') .',';
} else if($dataType !== 'array') {
throw new \Exception(sprintf('Key %s not found in database mapping!', $dataType));
}
Expand Down
24 changes: 24 additions & 0 deletions src/Entity/AvatarImage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace App\Entity;

class AvatarImage extends File implements EntityInterface
{
private int $userId;

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

/**
* @param int $userId
*/
public function setUserId(int $userId): void
{
$this->userId = $userId;
}
}
1 change: 0 additions & 1 deletion src/Entity/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace App\Entity;


class Entity
{
private function serialize(): array
Expand Down
83 changes: 83 additions & 0 deletions src/Entity/File.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

declare(strict_types=1);

namespace App\Entity;

class File extends Entity
{
private int $id;

/**
* @unique
*/
private string $serverName;

private string $fileName;

private \DateTime $date;

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

/**
* @param int $id
*/
public function setId(int $id): void
{
$this->id = $id;
}

/**
* @return string
*/
public function getServerName(): string
{
return $this->serverName;
}

/**
* @param string $serverName
*/
public function setServerName(string $serverName): void
{
$this->serverName = $serverName;
}

/**
* @return string
*/
public function getFileName(): string
{
return $this->fileName;
}

/**
* @param string $fileName
*/
public function setFileName(string $fileName): void
{
$this->fileName = $fileName;
}

/**
* @return \DateTime
*/
public function getDate(): \DateTime
{
return $this->date;
}

/**
* @param \DateTime $date
*/
public function setDate(\DateTime $date): void
{
$this->date = $date;
}
}