Skip to content

Commit

Permalink
Merge pull request #8 from LaswitchTech/dev
Browse files Browse the repository at this point in the history
Publishing v0.0.9
  • Loading branch information
LouisOuellet authored Jan 30, 2025
2 parents ae416b8 + 81645ed commit 8fef7b2
Show file tree
Hide file tree
Showing 12 changed files with 1,163 additions and 8 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ composer.phar
# Exclude Files
cli
index.php
api.php

# Exclude Locales Configurations and Translations
!Locale/*/*.cfg
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

## Description
**Author(s)**: [Louis Ouellet]([email protected])

The `Core` package is a collection of classes that provide the basic functionality for the LaswitchTech Core Framework. The classes are designed to be extended and used in other projects. Additional `Core` packages can be added to extend the functionality of the framework.

## License
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v0.0.8
v0.0.9
129 changes: 129 additions & 0 deletions src/API.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
<?php

/**
* Core Framework - API
*
* @license MIT (https://mit-license.org/)
* @author Louis Ouellet <[email protected]>
*/

// Declaring namespace
namespace LaswitchTech\Core;

// Import additionnal class into the global namespace
use Exception;

class API {

// Global Properties
protected $Output;
protected $Request;
protected $Config;
protected $Auth;

/**
* Constructor
*/
public function __construct() {

// Import Global Variables
global $OUTPUT, $REQUEST, $CONFIG, $AUTH;

// Initialize Properties
$this->Output = $OUTPUT;
$this->Request = $REQUEST;
$this->Config = $CONFIG;
$this->Auth = $AUTH;
}

/**
* Execute the API Request
*/
public function request(){

// Retrieve the namespace
$namespace = $this->Request->getNamespace();

// Parse the namespace
$parts = explode("/", trim($namespace, "/"));

// Check if the namespace is valid
if(count($parts) > 1){

// Set the endpoint and method
$endpoint = $parts[0];
$class = ucfirst($endpoint) . "Endpoint";
$method = $parts[1] . 'Action';

// Set the endpoint path
$path = $this->Config->root() . "/Endpoint/" . ucfirst($endpoint) . "Endpoint.php";

// Check if the endpoint exists
if(!is_file($path)){

// Set the path to the plugin path
$path = $this->Config->root() . "/lib/plugins/" . $endpoint . "/Endpoint.php";
}

// Check if the endpoint exists
if(is_file($path)){

// Load the endpoint
require_once $path;

// Check if the class exists
if(class_exists($class)){

// Initialize the endpoint
$object = new $class();

// Check if the method exists
if(method_exists($object, $method)){

// Retrieve Auth Properties
$public = $object->getPublic();
$level = $object->getLevel();
$permission = "Endpoint>" . $namespace;

// Check if Auth is available and if the endpoint is public
if(get_class($this->Auth) !== "Strap" && !$public){

// Check if the user is authenticated
if(!$this->Auth->isAuthenticated()){

// Send unauthorized
$this->Output->print('Unauthorized', array('HTTP/1.1 401 Unauthorized'));
}

// Check if the user has the required permission
if(!$this->Auth->hasPermission($permission, $level)){

// Send forbidden
$this->Output->print('Forbidden', array('HTTP/1.1 403 Forbidden'));
}
}

// Call the method
$object->{$method}();
} else {

// Could not find the method, send not implemented
$this->Output->print('Could not find the method', array('HTTP/1.1 501 Not Implemented'));
}
} else {

// Could not find the class, send not implemented
$this->Output->print('Could not find the endpoint', array('HTTP/1.1 501 Not Implemented'));
}
} else {

// Could not find the endpoint
$this->Output->print('Could not find the endpoint', array('HTTP/1.1 404 Not Found'));
}
} else {

// Could not identify the Controller and/or Method, send bad request
$this->Output->print('Could not identify the Controller and/or Action', array('HTTP/1.1 400 Bad Request'));
}
}
}
122 changes: 122 additions & 0 deletions src/Connector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
<?php

/**
* Core Framework - Connector
*
* @license MIT (https://mit-license.org/)
* @author Louis Ouellet <[email protected]>
*/

// Declaring namespace
namespace LaswitchTech\Core;

// Import additionnal class into the global namespace
use Exception;

class Connector {

/**
* @var mixed
*/
protected $Config;

/**
* Constructor
*
* Here you might load global config or do other setup tasks.
* For this example, we assume there's a $CONFIG global object
* that stores DB credentials, etc.
*/
public function __construct()
{
global $CONFIG;
$this->Config = $CONFIG;

// If you have a method $CONFIG->add('database') to load DB config, do that here.
if (method_exists($this->Config, 'add')) {
$this->Config->add('database');
}
}

/**
* Connect method to be overridden by child classes
*/
public function connect()
{
// Implementation in child classes
}

/**
* Close method to be overridden by child classes
*/
public function close()
{
// Implementation in child classes
}

/**
* Check if connected
*
* @return bool
*/
public function isConnected(): bool
{
// Implementation in child classes
return false;
}

/**
* Execute a query
*
* @param string $sql
* @return mixed
*/
public function query(string $sql)
{
// Implementation in child classes
}

/**
* Describe a table
*
* @param string $table
* @return mixed
*/
public function describe(string $table)
{
// Implementation in child classes
}

/**
* Get the ID of the last inserted row
*
* @return int
*/
public function lastId(): int
{
// Implementation in child classes
return 0;
}

/**
* Get the number of affected rows
*
* @return int
*/
public function affectedRows(): int
{
// Implementation in child classes
return 0;
}

/**
* Prepare a query
*
* @param string $sql
*/
public function prepare(string $sql, array $params = [])
{
// Implementation in child classes
return false;
}
}
Loading

0 comments on commit 8fef7b2

Please sign in to comment.