From 86b8f71d31abb8774a66ab82509a9b166909a747 Mon Sep 17 00:00:00 2001 From: Dominik Garbulski Date: Sat, 26 Jun 2021 06:22:50 +0200 Subject: [PATCH] [WIP] fix file loading --- index.php | 6 ++ src/ClassLoader.php | 51 ++++++++++++++-- src/Controller/DatabaseController.php | 9 ++- src/Entity/AvatarImage.php | 24 ++++++++ src/Entity/Entity.php | 1 - src/Entity/File.php | 83 +++++++++++++++++++++++++++ 6 files changed, 165 insertions(+), 9 deletions(-) create mode 100644 src/Entity/AvatarImage.php create mode 100644 src/Entity/File.php diff --git a/index.php b/index.php index 1e66fa2..0edeb2d 100644 --- a/index.php +++ b/index.php @@ -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(), diff --git a/src/ClassLoader.php b/src/ClassLoader.php index 15fb91a..bd36bc9 100644 --- a/src/ClassLoader.php +++ b/src/ClassLoader.php @@ -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); } @@ -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]); @@ -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) @@ -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 ''; } } diff --git a/src/Controller/DatabaseController.php b/src/Controller/DatabaseController.php index 4fe3e67..2a60425 100644 --- a/src/Controller/DatabaseController.php +++ b/src/Controller/DatabaseController.php @@ -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([ @@ -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)); } diff --git a/src/Entity/AvatarImage.php b/src/Entity/AvatarImage.php new file mode 100644 index 0000000..c12b37f --- /dev/null +++ b/src/Entity/AvatarImage.php @@ -0,0 +1,24 @@ +userId; + } + + /** + * @param int $userId + */ + public function setUserId(int $userId): void + { + $this->userId = $userId; + } +} diff --git a/src/Entity/Entity.php b/src/Entity/Entity.php index 41f327e..dd3b074 100644 --- a/src/Entity/Entity.php +++ b/src/Entity/Entity.php @@ -2,7 +2,6 @@ namespace App\Entity; - class Entity { private function serialize(): array diff --git a/src/Entity/File.php b/src/Entity/File.php new file mode 100644 index 0000000..aa23c34 --- /dev/null +++ b/src/Entity/File.php @@ -0,0 +1,83 @@ +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; + } +}