Skip to content

Commit

Permalink
Merge pull request #29 from nox7/2.0.0
Browse files Browse the repository at this point in the history
2.0.0
  • Loading branch information
nox7 authored Aug 11, 2022
2 parents 0de601d + 81345ac commit d6a5d9d
Show file tree
Hide file tree
Showing 52 changed files with 1,311 additions and 886 deletions.
30 changes: 24 additions & 6 deletions example/cli-scripts/sync-models.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,31 @@
<?php
require_once __DIR__ . "/../../vendor/autoload.php";
require_once __DIR__ . "/../nox-env.php";

use \Nox\ORM\Abyss;
use Nox\ClassLoader\ClassLoader;
use Nox\Nox;
use Nox\ORM\Abyss;
use Nox\ORM\DatabaseCredentials;

// Load the source directory
$nox = new Nox();
$nox->setSourceCodeDirectory(__DIR__ . "/../src");

// Get the model reflections
$modelReflections = ClassLoader::$modelClassReflections;

// Load the credentials for any and all databases used by the models
Abyss::addCredentials(new DatabaseCredentials(
host: NoxEnv::MYSQL_HOST,
username: NoxEnv::MYSQL_USERNAME,
password: NoxEnv::MYSQL_PASSWORD,
database: NoxEnv::MYSQL_DB_NAME,
port: NoxEnv::MYSQL_PORT,
));

// Setup Abyss from this directory.
// This is only necessary when not being used with the Router
// such as from CLI (this script) or on its own
Abyss::loadConfig(__DIR__ . "/..");
$abyss = new Abyss();

// Sync the models to the current local MySQL database
$abyss->syncModels();
print("Synchronizing Models to MySQL database.\n");
$abyss->syncModels($modelReflections);
print("Synchronization finished.\n");
10 changes: 0 additions & 10 deletions example/nox-cache.json

This file was deleted.

14 changes: 0 additions & 14 deletions example/nox-mime.json

This file was deleted.

79 changes: 62 additions & 17 deletions example/nox-request.php
Original file line number Diff line number Diff line change
@@ -1,29 +1,74 @@
<?php

use Nox\Nox;
use Nox\ORM\Abyss;
use Nox\ORM\DatabaseCredentials;
use Nox\Router\BaseController;
use Nox\Router\Exceptions\NoMatchingRoute;

require_once __DIR__ . "/../vendor/autoload.php";
require_once __DIR__ . "/nox-env.php";

$requestPath = urldecode(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH));
$requestMethod = $_SERVER['REQUEST_METHOD'];
$nox = new Nox();
BaseController::$noxInstance = $nox;

// Load the router
$router = new Nox\Router\Router(
requestPath:$requestPath,
requestMethod:$requestMethod,
);
$router->loadAll(
fromDirectory: __DIR__,
// Set a static file serving directory
$nox->addStaticDirectory(
uriStub: "/",
directoryPath: __DIR__ . "/resources/static",
);

// Load the Abyss ORM configuration
// Comment this out to disable using Abyss and require a MySQL connection
Abyss::loadConfig(__DIR__);
// Support static file mime types so the browser can recognize the static files
$nox->mapExtensionToMimeType("css", "text/css");
$nox->mapExtensionToMimeType("map", "text/plain");
$nox->mapExtensionToMimeType("png", "image/png");
$nox->mapExtensionToMimeType("jpg", "image/jpeg");
$nox->mapExtensionToMimeType("jpeg", "image/jpeg");
$nox->mapExtensionToMimeType("js", "text/javascript");
$nox->mapExtensionToMimeType("mjs", "text/javascript");
$nox->mapExtensionToMimeType("gif", "image/gif");
$nox->mapExtensionToMimeType("weba", "audio/webm");
$nox->mapExtensionToMimeType("webm", "video/webm");
$nox->mapExtensionToMimeType("webp", "image/webp");
$nox->mapExtensionToMimeType("pdf", "application/pdf");
$nox->mapExtensionToMimeType("svg", "image/svg+xml");

// Mime caches
$nox->addCacheTimeForMime("image/png", 86400 * 60);
$nox->addCacheTimeForMime("image/jpeg", 86400 * 60);
$nox->addCacheTimeForMime("text/css", 86400 * 60);
$nox->addCacheTimeForMime("text/plain", 86400 * 60);
$nox->addCacheTimeForMime("text/javascript", 86400 * 60);
$nox->addCacheTimeForMime("text/gif", 86400 * 60);
$nox->addCacheTimeForMime("text/svg", 86400 * 60);
$nox->addCacheTimeForMime("image/webp", 86400 * 60);

// Process static files before anything else, to keep static file serving fast
$nox->router->processRequestAsStaticFile();

// If the code gets here, then it's not a static file. Load the rest of the setting directories
$nox->setViewsDirectory(__DIR__ . "/resources/views");
$nox->setLayoutsDirectory(__DIR__ . "/resources/layouts");
$nox->setSourceCodeDirectory(__DIR__ . "/src");

// Load the request handler
$requestHandler = new \Nox\Router\RequestHandler($router);
\Nox\Router\BaseController::$requestHandler = $requestHandler;
// Setup the Abyss ORM (MySQL ORM)
// Comment the Abyss credentials out if you do not need MySQL
// Multiple credentials can be added if you have multiple databases/schemas
Abyss::addCredentials(new DatabaseCredentials(
host: NoxEnv::MYSQL_HOST,
username: NoxEnv::MYSQL_USERNAME,
password: NoxEnv::MYSQL_PASSWORD,
database: NoxEnv::MYSQL_DB_NAME,
port: NoxEnv::MYSQL_PORT,
));

// Process the request
$requestHandler->processRequest();
// Process the request as a routable request
try {
$nox->router->processRoutableRequest();
} catch (NoMatchingRoute $e) {
// 404
http_response_code(404);
// Process a new routable request, but change the path to our known 404 controller method route
$nox->router->requestPath = "/404";
$nox->router->processRoutableRequest();
}
9 changes: 0 additions & 9 deletions example/nox.json

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
<?php
/** @var string $htmlHead Head contents from the view file */
/** @var string $htmlBody Body contents from the view file */
?>
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
Expand Down
File renamed without changes
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
<?php

// No need to require the autoload in the Controller.
// The controller has the scope of the request.php file

require_once __DIR__ . "/../attributes/NeverUsable.php";

use Nox\RenderEngine\Exceptions\LayoutDoesNotExist;
use Nox\RenderEngine\Exceptions\ParseError;
use Nox\RenderEngine\Exceptions\ViewFileDoesNotExist;
use Nox\RenderEngine\Renderer;
use Nox\Router\Attributes\Controller;
use Nox\Router\Attributes\Route;
use Nox\RenderEngine\Exceptions\ParseError;
use Nox\Router\BaseController;

#[Controller]
class HomeController extends \Nox\Router\BaseController{
class HomeController extends BaseController{

/**
* @throws ParseError
Expand Down Expand Up @@ -42,8 +38,7 @@ public function error404View(): string{
*/
#[Route("GET", "/always-404")]
#[NeverUsable()]
public function always404View(): string{
return "uh-oh";
// return Renderer::renderView("home.html");
public function always404View(): void{
// Never runs
}
}
File renamed without changes.
4 changes: 2 additions & 2 deletions example/classes/User.php → example/src/Users/User.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php

require_once __DIR__ . "/../models/UsersModel.php";
namespace Users;

use Nox\ORM\Interfaces\ModelInstance;
use Nox\ORM\ModelClass;
use Nox\ORM\Interfaces\MySQLModelInterface;
use Nox\ORM\ModelClass;

class User extends ModelClass implements ModelInstance
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?php

// No need to require the autoload in the Controller.
// The controller has the scope of the request.php file
namespace Users;

use Nox\Http\Attributes\ProcessRequestBody;
use Nox\Http\Attributes\UseJSON;
Expand All @@ -13,18 +12,18 @@
use Nox\Router\BaseController;

#[Controller]
#[RouteBase("/\/api\/(?<version>v\d)/", true)]
class ExampleAPIController extends BaseController{
#[RouteBase("/users")]
class UsersController extends BaseController{

#[Route("GET", "/")]
public function apiHomeView(): string{
return "API home view example.";
public function usersHomeView(): string{
return "Users microservice home view..";
}

#[Route("PUT", "/some/resource/to/put")]
#[Route("PUT", "/users")]
#[UseJSON] // Lets the router know to put the response content-type as JSON and to send JSONResult as a JSON string
#[ProcessRequestBody] // Parses the raw request body if it is a non-GET request
public function subView(): JSONResult{
public function addUser(): JSONResult{
// Payload is the request body parsed into an array
$payload = BaseController::$requestPayload;

Expand Down
21 changes: 16 additions & 5 deletions example/models/UsersModel.php → example/src/Users/UsersModel.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
<?php

require_once __DIR__ . "/../classes/User.php";
namespace Users;

use \Nox\ORM\ColumnDefinition;
use \Nox\ORM\Interfaces\MySQLModelInterface;
use \Nox\ORM\MySQLDataTypes\Integer;
use \Nox\ORM\MySQLDataTypes\VariableCharacter;
use Nox\ORM\Attributes\Model;
use Nox\ORM\ColumnDefinition;
use Nox\ORM\Interfaces\MySQLModelInterface;
use Nox\ORM\MySQLDataTypes\Integer;
use Nox\ORM\MySQLDataTypes\VariableCharacter;

#[Model]
class UsersModel implements MySQLModelInterface {

/**
* The name of the database this table belongs to
*/
private string $mysqlDatabaseName = "test";

/**
* The name of this Model in the MySQL database as a table
*/
Expand All @@ -19,6 +26,10 @@ class UsersModel implements MySQLModelInterface {
*/
private string $representingClassName = User::class;

public function getDatabaseName(): string{
return $this->mysqlDatabaseName;
}

public function getName(): string{
return $this->mysqlTableName;
}
Expand Down
35 changes: 35 additions & 0 deletions src/ClassLoader/ClassLoader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php
namespace Nox\ClassLoader;

use Nox\ClassLoader\Exceptions\ControllerMissingExtension;
use Nox\ORM\Attributes\Model;
use ReflectionClass;
use ReflectionException;

class ClassLoader{

/** @var string[] All classes loaded by the Nox framework from the user's app */
public static array $allAppLoadedClasses = [];

/** @var ReflectionClass[] Reflections of classes identified as Controllers */
public static array $controllerClassReflections = [];

/** @var ReflectionClass[] Reflections of classes identified as Models */
public static array $modelClassReflections = [];

/**
* Will loop through self::$allAppLoadedClasses and determine which type of Nox-identified class
* they are. E.g., a Controller or a Model class. Then it will sort them into the necessary static properties
* @return void
* @throws ControllerMissingExtension
* @throws ReflectionException
* @throws Exceptions\ModelMissingImplementation
*/
public static function runClassFiltersAndSorting(){
$controllerClassIdentifier = new ControllerClassIdentifier();
$modelClassIdentifier = new ModelClassIdentifier();
self::$controllerClassReflections = $controllerClassIdentifier->getControllerClassReflections(self::$allAppLoadedClasses);
self::$modelClassReflections = $modelClassIdentifier->getModelClassReflections(self::$allAppLoadedClasses);
}

}
17 changes: 17 additions & 0 deletions src/ClassLoader/ClassScopeHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
namespace Nox\ClassLoader;

class ClassScopeHelper{

/** @var string[] Of currently declared classnames after cacheCurrentClassNames is called */
public static array $currentClassNameCache = [];

public static function cacheCurrentClassNames(): void{
self::$currentClassNameCache = get_declared_classes();
}

public static function getNewClassNamesDefined(): array{
$nowDefinedClasses = get_declared_classes();
return array_diff($nowDefinedClasses, self::$currentClassNameCache);
}
}
Loading

0 comments on commit d6a5d9d

Please sign in to comment.