Compare commits

..

79 Commits

Author SHA1 Message Date
kj
e9126e7cde Fix: Implicitly marking parameter as nullable is deprecated.
PHP 8.4 deprecation.
2025-06-07 14:27:47 -03:00
kj
7169d2cae3 Fix: render is not using the defined extension. 2025-06-07 14:17:11 -03:00
kj
66b2bc0d91 Remove unnecesary php close. 2025-06-07 14:14:16 -03:00
kj
c8ab2aa2cc Remove unnecesary echo. 2025-05-20 12:49:20 -03:00
kj
1e302a9ea7 BREAKING CHANGE: Change unnecesary false return type. 2025-04-19 15:44:16 -03:00
kj
d0d0d4dc76 Verify if a valid http query string after run parse_str. 2025-02-20 08:22:47 -03:00
kj
595e9c1316 Save body request as a property. 2025-02-20 08:22:37 -03:00
kj
45abea5301 Add delete request params. 2025-02-20 06:28:03 -03:00
kj
d441f001ec Add type of items on array on dockblock for "all" method. 2025-02-03 16:04:39 -03:00
kj
19da122e05 Add type of items on array on dockblock for get method. 2025-02-03 16:02:27 -03:00
KJ
1a0164c8ed Change static methods to non-static and made onInvalid public. 2024-10-30 11:53:44 -04:00
KJ
ad9f8ec67d Remove unnecesary brackets. 2024-10-29 19:12:25 -04:00
KJ
31c5c63952 Remove innecesary return. 2024-10-29 19:10:47 -04:00
KJ
6aef212350 Fix className not returning the classname in the right format. 2024-10-25 10:40:57 -04:00
KJ
c600688725 Improve return array dockblocks. 2024-09-23 18:09:38 -04:00
KJ
3e27b1b7af Allow null on enum properties. 2024-09-23 15:06:44 -04:00
KJ
73b7b8f72a Change required valitator to not allow empty values and add exists.
The exists validator do the same as the old required.
2024-09-18 14:33:33 -04:00
KJ
7baad428ec Refactor request library. 2024-09-08 14:43:56 -04:00
KJ
3d2a607768 Fix where_in is wiping previous where/and/or.
For now, works as an AND, but maybe later, same as where will exists
new methods: AndIn and OrIN.
2024-08-30 16:26:03 -04:00
KJ
df424ffab5 Model properties now can be typed as enums.
With this PHP 8.0 support is dropped.
2024-08-27 19:01:02 -04:00
KJ
daf7250882 Catch and verify put and patch input values. 2024-08-13 10:22:44 -04:00
KJ
05cd83fd10 Remove unused variable. 2024-07-31 03:29:49 -04:00
KJ
6b470a181d Fix: Remove unnecesary parameter. 2024-07-10 09:06:51 -04:00
KJ
7beb161d2b Ensure db is in transaction to commit or rollback. 2024-06-04 07:18:30 -04:00
KJ
701caae7eb Change route method to static. 2024-05-29 13:24:20 -04:00
KJ
100bdfe006 Change private method to protected instead. 2024-05-28 22:27:20 -04:00
KJ
f1b79fdbc0 Add http code 422 on verification failed. 2024-05-25 17:41:33 -04:00
KJ
406f9a10a1 Add head comment. 2024-05-25 17:19:08 -04:00
KJ
cc3cb6be41 Fix: a return was forgot. 2024-05-25 17:11:07 -04:00
KJ
59fff2a586 Add validation on Request. 2024-05-25 16:59:59 -04:00
KJ
cd1685d2e7 fix on a docblock. 2024-05-21 15:06:02 -04:00
KJ
b85fb7e034 Allow configure SITE_URL with or without slash at end. 2024-05-16 13:48:49 -04:00
KJ
a10308a8f6 Fix route and redirect methods error when path not start with slash. 2024-05-16 13:20:35 -04:00
KJ
9a1e5a2379 Add some explanatory comments to config. 2024-05-14 02:54:23 -04:00
KJ
fa60ec5bb4 Move constant definition to config.php 2024-05-14 02:53:39 -04:00
KJ
b294b1d627 Update readme.
Fix description and add links to starting manual.
2024-05-13 01:08:44 -04:00
KJ
27b5ed6015 Convert model name from PascalCase to snake_case (table name). 2024-05-12 04:38:45 -04:00
KJ
f9b2c678f7 Support multiple databases at same time. 2024-05-12 03:47:09 -04:00
KJ
e294eaa7af Add route function for View objects. 2024-05-09 23:12:59 -04:00
KJ
186d640c8f fix: an empty SITE_URL return a incorrect value of basePath. 2024-05-09 23:10:58 -04:00
KJ
c65fcd27c8 Create new ROOT_CORE constant. 2024-05-09 18:36:48 -04:00
KJ
f5f803dde2 Reorganize code for PSR and others code prettify. 2024-05-09 15:13:52 -04:00
KJ
6a1085b224 Allow Neuron as $params on view render. 2024-05-09 14:56:07 -04:00
KJ
64bcfa0b00 Add Lib\Request. 2024-05-05 09:38:10 -04:00
KJ
cd01ab9e72 Change switch to match. 2023-10-20 16:26:17 -04:00
KJ
e2094ccb4a Add option to use another extensions, js and css methods. 2023-09-15 21:26:53 -04:00
KJ
341d7837a1 Add Router::reconfigure method. 2023-09-08 22:42:50 -04:00
KJ
030e1079dc add mising types, patch method and a new way to set middleware
- Removed resumed way to set controllers and middlewares:
  Now, the method of routers and middlewares need to be
  callables.
- Add patch method in order to support PATCH http method.
- Add a way to get add more middlewares after. For Example:

  <?php
    use Libs\Router;

    Router::get('/some-route', 'somefunction')
      ->middleware('firstmiddleware');
    Router::get('/other-route', 'otherfunction');

    // Recover first router and add a middleware
    Router::get('/some-route')->middleware('secondmiddleware');
  ?>
2023-08-22 01:48:04 -04:00
KJ
af673a68b8 Add exit after redirect in order to avoid other erros. 2023-08-09 08:54:17 -04:00
KJ
eff0b86762 Allow construct Neuron with named arguments.
Example:

<?php
$instance = new Neuron(stringValue: 'Hello world', integerValue: 50,
boolValue: false);

echo $instance->stringValue; // "Hello world" will be printed.
?>

Also, is possible to send infinite params without names and his names
will be numeric similar as a non-asociatie array, but as Neuron object.

Example:

<?php
$str = 'Hello world';
$int = 50;
$con = false;

$instance = new Neuron($str, $int);

echo $instance->{0}; // "Hello world" will be printed.
?>
2023-06-04 14:47:58 -04:00
KJ
39a1f9d85a Improve magic function __get.
Is not necessary another conditionals. When __get is called
is only when the the property is not defined, so
only need return null in order to avoid the
PHP notice of undefined property.
2023-06-04 14:34:24 -04:00
kj
d48f24ed98 Improve return types. 2023-04-22 05:32:37 -04:00
kj
b326c8e1d0 Fix mising use. 2023-04-16 23:47:49 -04:00
kj
1267d6b2f6 Fix deprecation warnings on PHP8.2 2023-04-15 13:17:00 -04:00
kj
a8d95cb55b Fix empty application/json. 2023-04-14 16:43:50 -04:00
kj
06b7907f7c Define mixed type to param $id on getById method. 2023-03-25 21:28:25 -04:00
kj
4d052efba6 Change return type from mixed to ?Model on getFirts and getById methods. 2023-03-25 18:28:21 -04:00
kj
11141a0eee Allow send string to setNull method. 2023-03-25 12:27:33 -04:00
kj
3c8a21161f Add missing docblocks and return types. 2023-03-25 12:26:42 -04:00
kj
1bde430251 Improve Neuron and allow use to clone another object. 2023-03-15 22:16:05 -04:00
kj
7d3d1615d5 Fix View:txt and add charset. 2022-11-22 00:59:20 -04:00
kj
404bd59569 Improve where, and, or and search method.
- static::$querySelect['AndOr'] is REMOVED.
- where, and, or and search now use only the
  static::$querySelect['where'] index.
- removed unnecesary brackets on buildQuery.
2022-08-22 15:26:32 -04:00
kj
53bdc92344 Add bindValue method to Libs\Model. 2022-08-18 16:38:40 -04:00
kj
ad7b78f427 Fix prepare variables when special where/and/or columns. 2022-08-12 06:31:54 -04:00
kj
38d2a90318 Fix count when using joins. 2022-08-12 05:46:24 -04:00
kj
2411704662 Add sqlite support. 2022-08-04 05:30:22 -04:00
kj
08d92a2b81 Allow usage of bool types on the ORM. 2022-08-01 08:27:49 -04:00
kj
8d47e10d7a Fix resetQuery. 2022-07-29 22:23:07 -04:00
kj
2c7938a0c3 Set default fetch mode to fetch assoc. 2022-07-24 00:29:34 -04:00
kj
c57ca191b1 Rename limit() variables with better names and add description. 2022-07-19 12:08:22 -04:00
kj
f4eabe6d54 Minor fixes.
- Add some uses.
- Fix typo and order error con docl block.
2022-07-14 22:39:38 -04:00
kj
83d9ba55aa Update migration notes. 2022-07-14 22:08:43 -04:00
kj
bad7bf0e65 Update config template. 2022-07-14 22:03:07 -04:00
kj
5284be2b1b Update org files. 2022-07-14 21:59:27 -04:00
kj
0db6e4a021 rework to change from MySQLi to PDO. 2022-07-14 21:41:48 -04:00
kj
afb4c914a1 Fix error on where_in with string values. 2022-07-10 12:10:01 -04:00
kj
4c4fe6f1f7 Change View to work as instance and render text and json. 2022-06-25 21:11:11 -04:00
kj
b6555ee039 Fix docblocks. 2022-06-07 23:06:37 -04:00
kj
a641248453 Add middleware priorities. 2022-06-07 23:05:49 -04:00
12 changed files with 1296 additions and 517 deletions

View File

@ -1,10 +1,14 @@
<?php
define('dbhost', 'localhost');
define('dbname', '');
define('dbuser', '');
define('dbpass', '');
// Configuración de la base de datos
define('DB_TYPE', 'mysql');
define('DB_HOST', 'localhost');
define('DB_NAME', '');
define('DB_USER', '');
define('DB_PASS', '');
// Configuración del sitio
define('SITE_URL', '');
// Configuración avanzada
define('ROOT_DIR', __DIR__);
?>
define('ROOT_CORE', ROOT_DIR.'/src');

View File

@ -6,19 +6,15 @@ spl_autoload_register(function ($className) {
$fp = str_replace('\\','/',$className);
$name = basename($fp);
$dir = dirname($fp);
$file = ROOT_DIR.'/src/'.$dir.'/'.$name.'.php';
if (file_exists($file)) {
$file = ROOT_CORE.'/'.$dir.'/'.$name.'.php';
if (file_exists($file))
require_once $file;
return;
}
});
// Incluir routers
$routers = glob(ROOT_DIR.'/src/Routers/*.php');
$routers = glob(ROOT_CORE.'/Routers/*.php');
foreach($routers as $file){
foreach($routers as $file)
require_once($file);
}
\Libs\Router::apply();
?>

View File

@ -1,21 +0,0 @@
# DuckBrain - PHP Microframework
Este microframework PHP tiene el objetivo de presentar un framework sencillo y potente que sea válido especialmente para proyectos pequeños o simples, sin limitar demasiado, ni depender de cosas que agranden innecesariamente proyectos.
Esto no es un intento de mi parte de reinventar la rueda, de hecho los primeros commits los hice con la intención de que solo fuera una prueba de concepto, pero me gustó como quedó y decidí darle continuidad hasta hacerlo usable para mi mismo.
Tuve la idea de hacer este conjunto de librerías/microframework luego de ver como proyectos relativamente pequeños eran hechos con frameworks demasiado pesados para lo que eran.
También revisé otros microframeworks y si bien me parecen una buena opción para hacer los trabajos, aún me quedaba la espinita de que seguían siendo muy grandes para que un programador novato pudiera leerlo, entenderlo y modificarlo, así que intenté hacer algo más simple aún y de ahí salió este proyecto.
Lo ideal sería mantener el código sencillo, lo suficiente como para que cualquier novato que sepa POO y PHP pueda leerlo rápido, entenderlo y modificarlo a gusto. Por este motivo también he decidido desligarlo en lo posible de composer o cualquier cosa similar, ya que no pocos programadores en etapa de aprendizaje al encontrarse con frameworks más complicados o con herramientas como composer, terminan trabajando con cosas que no comprenden ni pueden arreglar por si mismos en caso de fallo, ya que son completamente dependientes de dichas herramientas.
El código no es perfecto, pero lo iré perfeccionando, ya que es algo que estoy usando actualmente para mis trabajos, de modo que puedo ir viendo que cosas se pueden ir mejorando.
## Uso / Documentación
Queda pendiente, si quieres usarlo ya mismo, puedes leer los comeentarios que he colocado en el código.
## Contacto
Puedes encontrame en telegram como [@keyjay](https://telegram.me/keyjay) o contactarme mi correo: webmaster@outcontrol.net

107
readme.org Normal file
View File

@ -0,0 +1,107 @@
#+TITLE: Duckbrain
#+AUTHOR: KJ
#+OPTIONS: toc:nil
Este conjunto de librerías (microframework, si así lo prefieres) tiene el objetivo de presentar una herramienta de trabajo sencilla y potente que sea válida especialmente para proyectos pequeños o simples, sin limitar, ni depender de cosas que agranden innecesariamente dichos proyectos.
Tuve la idea de hacer esta herramienta luego de ver como proyectos relativamente pequeños eran hechos con frameworks demasiado grandes para la envergadura de dichos proyectos. No es bueno matar moscas a cañonazos.
Del mismo modo revisé algunos microframeworks y si bien me parecen una buena opción, aún me quedaba la espinita de que seguían siendo muy complejos para que un programador novato pudiera leer su código, entenderlo y modificarlo, así que intenté hacer algo más simple
Lo que busco es mantener un código sencillo, lo suficiente como para que cualquier novato que sepa POO y PHP pueda leer su código rápidamente, entenderlo y modificarlo a gusto. Por este motivo también he decidido desligarlo en lo posible de composer o cualquier cosa similar, ya que no pocos programadores en etapa de aprendizaje al encontrarse con frameworks más complicados o con herramientas como composer, terminan trabajando con cosas que no comprenden ni pueden arreglar por si mismos en caso de fallo, llevándolos a la dependencia total de dichas herramientas.
* Uso / Documentación
Actualmente existe un manual de inicio que puedes leerlo desde [[https://tools.kj2.me/duckbrain/starting-manual.html][aquí]] o desde [[https://git.kj2.me/kj/duckbrain-docs/src/branch/master/starting-manual.org][aquí]].
Si quieres leer el manual de inicio en su formato original (necesitas un editor/lector que soporte org-mode) puedes descargarla desde [[https://tools.kj2.me/duckbrain/starting-manual.org][aquí]].
Para el resto de la documentación, el código usa [[https://www.phpdoc.org/][phpdoc]], por lo que puedes tener toda la documentación API desde tu IDE o Editor de texto preferido siempre que soporte dicha característica. O si lo prefieres, puedes generarla en unbonito htm local usando la herramienta [[https://docs.phpdoc.org/3.0/packages/phpDocumentor.html][phpDocumentor]].
* Soporte de gestores de bases de datos
En la siguiente tabla se encuentra la lista de estados de los gestores de bases de datos que he probado o que planeo probar para asegurarme de que sean realmente compatibles.
*Entiéndase*:
+ *ok* como que ha sido probado y funciona.
+ *En blanco* como que no ha sido probado aún.
+ *error* como que fue probado, no funciona y no ha sido aún arreglado.
+ *not supported* como no soportado por el gestor de bases de datos.
+ *fixed* para aquello que no existe en el gestor de DB, pero la librería lo traduce a un equivalente.
|------------------+---------------+---------------+------------|
| method | MySQL/MariaDB | sqlite3 | postgreSQL |
|------------------+---------------+---------------+------------|
| db | ok | ok | |
|------------------+---------------+---------------+------------|
| query | ok | ok | |
|------------------+---------------+---------------+------------|
| resetQuery | ok | ok | |
|------------------+---------------+---------------+------------|
| buildQuery | ok | ok | |
|------------------+---------------+---------------+------------|
| getInstance | ok | ok | |
|------------------+---------------+---------------+------------|
| getVars | ok | ok | |
|------------------+---------------+---------------+------------|
| className | ok | ok | |
|------------------+---------------+---------------+------------|
| table | ok | ok | |
|------------------+---------------+---------------+------------|
| update | ok | ok | |
|------------------+---------------+---------------+------------|
| beginTransaction | ok | ok | |
|------------------+---------------+---------------+------------|
| rollBack | ok | ok | |
|------------------+---------------+---------------+------------|
| commit | ok | ok | |
|------------------+---------------+---------------+------------|
| add | ok | ok | |
|------------------+---------------+---------------+------------|
| save | ok | ok | |
|------------------+---------------+---------------+------------|
| delete | ok | ok | |
|------------------+---------------+---------------+------------|
| select | ok | ok | |
|------------------+---------------+---------------+------------|
| from | ok | ok | |
|------------------+---------------+---------------+------------|
| where | ok | ok | |
|------------------+---------------+---------------+------------|
| where_in | ok | ok | |
|------------------+---------------+---------------+------------|
| leftJoin | ok | ok | |
|------------------+---------------+---------------+------------|
| rightJoin | ok | not supported | |
|------------------+---------------+---------------+------------|
| innerJoin | ok | ok | |
|------------------+---------------+---------------+------------|
| and | ok | ok | |
|------------------+---------------+---------------+------------|
| or | ok | ok | |
|------------------+---------------+---------------+------------|
| groupBy | ok | ok | |
|------------------+---------------+---------------+------------|
| limit | ok | ok | |
|------------------+---------------+---------------+------------|
| orderBy | ok | ok | |
|------------------+---------------+---------------+------------|
| count | ok | ok | |
|------------------+---------------+---------------+------------|
| getById | ok | ok | |
|------------------+---------------+---------------+------------|
| search | ok | ok | |
|------------------+---------------+---------------+------------|
| get | ok | ok | |
|------------------+---------------+---------------+------------|
| getFirst | ok | ok | |
|------------------+---------------+---------------+------------|
| all | ok | ok | |
|------------------+---------------+---------------+------------|
| setNull | ok | ok | |
|------------------+---------------+---------------+------------|
* Contacto
Puedes encontrame en telegram como [[https://telegram.me/keyjay][@keyjay]] o contactarme mi correo: webmaster@outcontrol.net

View File

@ -2,9 +2,7 @@
/**
* Database - DuckBrain
*
* Clase diseñada para crear y devolver una única instancia mysqli (database).
* Depende de manera forzada de que estén definidas las constantes:
* dbhost, dbname, dbpass y dbuser
* Clase diseñada para crear y devolver una única instancia PDO (database).
*
* @author KJ
* @website https://kj2.me
@ -12,27 +10,49 @@
*/
namespace Libs;
use mysqli;
class Database extends \mysqli {
static private $db;
use PDO;
use PDOException;
use Exception;
class Database extends PDO {
static private array $databases = [];
private function __construct() {}
/**
* Devuelve una instancia homogénea (singlenton) a la base de datos.
* Devuelve una instancia homogénea (singlenton) de la base de datos (PDO).
*
* @return mysqli
* @return PDO
*/
static public function getConnection() : mysqli {
if (!isset(self::$db)) {
self::$db = new mysqli(dbhost, dbuser, dbpass, dbname);
if (self::$db->connect_errno) {
echo '<style>body{white-space: pre-line;}</style>';
throw new \Exception('No se ha podido conectar a la base de datos.');
static public function getInstance(
string $type = 'mysql',
string $host = 'localhost',
string $name = '',
string $user = '',
string $pass = '',
): PDO
{
$key = $type.'/'.$host.'/'.$name.'/'.$user;
if (empty(static::$databases[$key])) {
if ($type == 'sqlite') {
$dsn = $type .':'. $name;
} else
$dsn = $type.':dbname='.$name.';host='.$host;
try {
static::$databases[$key] = new PDO($dsn, $user, $pass);
} catch (PDOException $e) {
throw new Exception(
'Error at connect to database: ' . $e->getMessage()
);
}
static::$databases[$key]->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
static::$databases[$key]->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
}
return self::$db;
return static::$databases[$key];
}
}
?>

View File

@ -20,7 +20,8 @@ class Middleware {
*
* @return mixed
*/
public static function next(Neuron $req) {
public static function next(Neuron $req): mixed
{
$next = array_pop($req->next);
return call_user_func_array($next, [$req]);
}

File diff suppressed because it is too large Load Diff

View File

@ -17,22 +17,37 @@
*/
namespace Libs;
use AllowDynamicProperties;
#[AllowDynamicProperties]
class Neuron {
private $data;
/**
* __construct
*
* @param array $data
*/
public function __construct(...$data)
{
if (count($data) === 1 &&
isset($data[0]) &&
(is_array($data[0]) ||
is_object($data[0])))
$data = $data[0];
public function __construct($data = []){
$this->data = (array) $data;
foreach($data as $key => $value)
$this->{$key} = $value;
}
public function __isset($index) {
return isset($this->data[$index]);
}
public function __get($index){
return (isset($this->data[$index]) && $this->data[$index] != '')
? $this->data[$index] : null;
/**
* __get
*
* @param string $index
* @return null
*/
public function __get(string $index): null
{
return null;
}
}

149
src/Libs/Request.php Normal file
View File

@ -0,0 +1,149 @@
<?php
/**
* Request - DuckBrain
*
* Libería complementaria de la libería Router.
* Contiene el cuerpo básico de la petición http (POST, GET, JSON, etc).
*
* @author KJ
* @website https://kj2.me
* @licence MIT
*/
namespace Libs;
class Request extends Neuron {
public Neuron $get;
public Neuron $post;
public Neuron $put;
public Neuron $patch;
public Neuron $delete;
public Neuron $json;
public Neuron $params;
public string $path;
public string $error;
public string $body;
public array $next;
/**
* __construct
*
* @param string $path Ruta actual tomando como raíz la instalación de DuckBrain.
*/
public function __construct()
{
$this->path = Router::currentPath();
$this->get = new Neuron($_GET);
$this->post = new Neuron($_POST);
$this->put = new Neuron();
$this->patch = new Neuron();
$this->delete = new Neuron();
$this->body = file_get_contents("php://input");
$contentType = isset($_SERVER["CONTENT_TYPE"]) ? trim($_SERVER["CONTENT_TYPE"]) : '';
if ($contentType === "application/json")
$this->json = new Neuron(
(object) json_decode(trim($this->body), false)
);
else {
$this->json = new Neuron();
if (in_array($_SERVER['REQUEST_METHOD'], ['PUT', 'PATCH', 'DELETE']) &&
preg_match('/^[^;?\/:@&=+$,]{1,255}[=]/', $this->body, $matches)) { // Con la expresión regular verificamos que sea un http query string válido y evitamos errores de memoria en caso de que el body tenga algo más grande que eso.
parse_str($this->body, $input_vars);
$this->{strtolower($_SERVER['REQUEST_METHOD'])} = new Neuron($input_vars);
}
}
$this->params = new Neuron();
}
/**
* Corre las validaciones e intenta continuar con la pila de callbacks.
*
* @return mixed
*/
public function handle(): mixed
{
if ($this->validate())
return Middleware::next($this);
return null;
}
/**
* Inicia la validación que se haya configurado.
*
* @return bool
*/
public function validate(): bool
{
$actual = match($_SERVER['REQUEST_METHOD']) {
'POST', 'PUT', 'PATCH', 'DELETE' => $this->{strtolower($_SERVER['REQUEST_METHOD'])},
default => $this->get
};
if (Validator::validateList(static::paramRules(), $this->params) &&
Validator::validateList(static::getRules(), $this->get ) &&
Validator::validateList(static::rules(), $actual))
return true;
if (isset(static::messages()[Validator::$lastFailed]))
$error = static::messages()[Validator::$lastFailed];
else {
$error = 'Error: validation failed of '.preg_replace('/\./', ' as ', Validator::$lastFailed, 1);
}
static::onInvalid($error);
return false;
}
/**
* Reglas para el método actual.
*
* @return array
*/
public function rules(): array {
return [];
}
/**
* Reglas para los parámetros por URL.
*
* @return array
*/
public function paramRules(): array {
return [];
}
/**
* Reglas para los parámetros GET.
*
* @return array
*/
public function getRules(): array {
return [];
}
/**
* Mensajes de error en caso de fallar una validación.
*
* @return array
*/
public function messages(): array {
return [];
}
/**
* Función a ejecutar cuando se ha detectado un valor no válido.
*
* @param string $error
*
* @return void
*/
public function onInvalid(string $error): void
{
http_response_code(422);
print($error);
}
}

View File

@ -17,24 +17,35 @@ class Router {
private static $get = [];
private static $post = [];
private static $put = [];
private static $patch = [];
private static $delete = [];
private static $last;
public static $notFoundCallback = 'Libs\Router::defaultNotFound';
public static function defaultNotFound () {
/**
* Función callback por defectio para cuando
* no se encuentra configurada la ruta.
*
* @return void
*/
public static function defaultNotFound (): void
{
header("HTTP/1.0 404 Not Found");
echo '<h2 style="text-align: center;margin: 25px 0px;">Error 404 - Página no encontrada</h2>';
}
/**
* __construct
*/
private function __construct() {}
/*
/**
* Parsea para deectar las pseudovariables (ej: {variable})
*
* @param string $path
* Ruta con pseudovariables.
*
* @param mixed $callback
* @param callable $callback
* Callback que será llamado cuando la ruta configurada en $path coincida.
*
* @return array
@ -42,7 +53,8 @@ class Router {
* path - Contiene la ruta con las pseudovariables reeplazadas por expresiones regulares.
* callback - Contiene el callback en formato Namespace\Clase::Método.
*/
private static function parse(string $path, $callback) : array {
private static function parse(string $path, callable $callback): array
{
preg_match_all('/{(\w+)}/s', $path, $matches, PREG_PATTERN_ORDER);
$paramNames = $matches[1];
@ -52,10 +64,6 @@ class Router {
['([^\/]+)'],
$path);
if (!is_callable($callback)) {
$callback = 'Controllers\\'.$callback;
}
return [
'path' => $path,
'callback' => [$callback],
@ -64,7 +72,7 @@ class Router {
}
/*
/**
* Devuelve el ruta base o raiz del proyecto sobre la que trabajará el router.
*
* Ej: Si la url del sistema está en "https://ejemplo.com/duckbrain"
@ -72,13 +80,14 @@ class Router {
*
* @return string
*/
public static function basePath() : string {
if (defined('SITE_URL'))
return parse_url(SITE_URL, PHP_URL_PATH);
public static function basePath(): string
{
if (defined('SITE_URL') && !empty(SITE_URL))
return rtrim(parse_url(SITE_URL, PHP_URL_PATH), '/').'/';
return str_replace($_SERVER['DOCUMENT_ROOT'], '/', ROOT_DIR);
}
/*
/**
* Redirije a una ruta relativa interna.
*
* @param string $path
@ -87,205 +96,269 @@ class Router {
* Ej: Si nuesto sistema está en "https://ejemplo.com/duckbrain"
* llamamos a Router::redirect('/docs'), entonces seremos
* redirigidos a "https://ejemplo.com/duckbrain/docs".
* @return void
*/
public static function redirect(string $path) {
header('Location: '.static::basePath().substr($path,1));
public static function redirect(string $path): void
{
header('Location: '.static::basePath().ltrim($path, '/'));
exit;
}
/*
/**
* Añade un middleware a la última ruta usada.
* Solo se puede usar un middleware a la vez.
*
* @param mixed $callback
* @param callable $callback
* @param int $prioriry
*
* @return Router
* Devuelve un enlace estático.
* @return static
* Devuelve la instancia actual.
*/
public static function middleware($callback) : Router{
public static function middleware(callable $callback, ?int $priority = null): static
{
if (!isset(static::$last))
return new static();
$method = static::$last[0];
$index = static::$last[1];
if (!is_callable($callback)) {
$callback = 'Middlewares\\'.$callback;
}
if (isset($priority) && $priority <= 0)
$priority = 1;
if (is_null($priority) || $priority >= count(static::$$method[$index]['callback']))
static::$$method[$index]['callback'][] = $callback;
else {
static::$$method[$index]['callback'] = array_merge(
array_slice(static::$$method[$index]['callback'], 0, $priority),
[$callback],
array_slice(static::$$method[$index]['callback'], $priority)
);
}
return new static();
}
/*
* @return Neuron
* Devuelve un objeto que contiene los atributos:
* post - Donde se encuentran los valores enviados por $_POST.
* get - Donde se encuentran los valores enviados por $_GET.
* json - Donde se encuentran los valores JSON enviados en el body.
/**
* Reconfigura el callback final de la última ruta.
*
* @param callable $callback
*
* @return static
*/
private static function getReq() : Neuron {
$req = new Neuron();
$req->get = new Neuron($_GET);
$req->post = new Neuron($_POST);
$req->json = new Neuron(static::get_json());
$req->params = new Neuron();
$req->path = static::currentPath();
return $req;
public static function reconfigure(callable $callback): static
{
if (empty(static::$last))
return new static();
$method = static::$last[0];
$index = static::$last[1];
static::$$method[$index]['callback'][0] = $callback;
return new static();
}
/*
* @return object
* Devuelve un objeto con los datos recibidos en JSON.
/**
* Configura calquier método para todas las rutas.
*
* En caso de no recibir un callback, busca la ruta actual
* solo configura la ruta como la última configurada
* siempre y cuando la misma haya sido configurada previamente.
*
* @param string $method
* Método http.
* @param string $path
* Ruta con pseudovariables.
* @param callable|null $callback
*
* @return
* Devuelve la instancia actual.
*/
private static function get_json() : object {
$contentType = isset($_SERVER["CONTENT_TYPE"]) ? trim($_SERVER["CONTENT_TYPE"]) : '';
if ($contentType === "application/json") {
return json_decode(trim(file_get_contents("php://input")));
}
return (object) '';
public static function configure(string $method, string $path, ?callable $callback = null): static
{
if (is_null($callback)) {
$path = preg_quote($path, '/');
$path = preg_replace(
['/\\\{\w+\\\}/s'],
['([^\/]+)'],
$path);
foreach(static::$$method as $index => $router)
if ($router['path'] == $path) {
static::$last = [$method, $index];
break;
}
/*
return new static();
}
static::$$method[] = static::parse($path, $callback);
static::$last = [$method, count(static::$$method)-1];
return new static();
}
/**
* Define los routers para el método GET.
*
* @param string $path
* Ruta con pseudovariables.
*
* @param mixed $callback
* @param callable|null $callback
* Callback que será llamado cuando la ruta configurada en $path coincida.
*
* @return Router
* Devuelve un enlace estático.
* @return static
* Devuelve la instancia actual.
*/
public static function get(string $path, $callback) {
static::$get[] = static::parse($path, $callback);
static::$last = ['get', count(static::$get)-1];
return new static();
public static function get(string $path, ?callable $callback = null): static
{
return static::configure('get', $path, $callback);
}
/*
/**
* Define los routers para el método POST.
*
* @param string $path
* Ruta con pseudovariables.
*
* @param mixed $callback
* @param callable|null $callback
* Callback que será llamado cuando la ruta configurada en $path coincida.
*
* @return Router
* Devuelve un enlace estático.
* @return static
* Devuelve la instancia actual.
*/
public static function post(string $path, $callback) : Router {
static::$post[] = static::parse($path, $callback);
static::$last = ['post', count(static::$post)-1];
return new static();
public static function post(string $path, ?callable $callback = null): static
{
return static::configure('post', $path, $callback);
}
/*
/**
* Define los routers para el método PUT.
*
* @param string $path
* Ruta con pseudovariables.
*
* @param mixed $callback
* @param callable|null $callback
* Callback que será llamado cuando la ruta configurada en $path coincida.
*
* @return Router
* Devuelve un enlace estático
* @return static
* Devuelve la instancia actual
*/
public static function put(string $path, $callback) : Router {
static::$put[] = static::parse($path, $callback);
static::$last = ['put', count(static::$put)-1];
return new static();
public static function put(string $path, ?callable $callback = null): static
{
return static::configure('put', $path, $callback);
}
/*
/**
* Define los routers para el método PATCH.
*
* @param string $path
* Ruta con pseudovariables.
* @param callable|null $callback
* Callback que será llamado cuando la ruta configurada en $path coincida.
*
* @return static
* Devuelve la instancia actual
*/
public static function patch(string $path, ?callable $callback = null): static
{
return static::configure('patch', $path, $callback);
}
/**
* Define los routers para el método DELETE.
*
* @param string $path
* Ruta con pseudovariables
*
* @param callable $callback
* @param callable|null $callback
* Callback que será llamado cuando la ruta configurada en $path coincida.
*
* @return static
* Devuelve un enlace estático
* Devuelve la instancia actual
*/
public static function delete(string $path, $callback) : Router {
static::$delete[] = static::parse($path, $callback);
static::$last = ['delete', count(static::$delete)-1];
return new static();
public static function delete(string $path, ?callable $callback = null): static
{
return static::configure('delete', $path, $callback);
}
/*
* Devuelve la ruta actual.
/**
* Devuelve la ruta actual tomando como raíz la ruta de instalación de DuckBrain.
*
* @return string
*/
public static function currentPath() : string {
public static function currentPath() : string
{
return preg_replace('/'.preg_quote(static::basePath(), '/').'/',
'/', strtok($_SERVER['REQUEST_URI'], '?'), 1);
}
/*
* Aplica los routers.
/**
* Aplica la configuración de rutas.
*
* Este método ha de ser llamado luego de que todos los routers hayan sido configurados.
* @param string|null $path (opcional) Ruta a usar. Si no se define, detecta la ruta actual.
*
* En caso que la ruta actual coincida con un router configurado, se comprueba si hay middleware; Si hay
* middleware, se enviará el callback y los datos de la petición como un Neuron. Caso contrario, se enviarán
* los datos directamente al callback.
*
* Con middleware:
* $middleware($callback, $req)
*
* Sin middleware:
* $callback($req)
*
* $req es una instancia de Neuron que tiene los datos de la petición.
*
* Si no la ruta no coincide con ninguna de las rutas configuradas, ejecutará el callback $notFoundCallback
* @return void
*/
public static function apply() {
$path = static::currentPath();
$routers = [];
switch ($_SERVER['REQUEST_METHOD']){ // Según el método selecciona un arreglo de routers configurados
case 'POST':
$routers = static::$post;
break;
case 'PUT':
$routers = static::$put;
break;
case 'DELETE':
$routers = static::$delete;
break;
default:
$routers = static::$get;
break;
}
$req = static::getReq();
public static function apply(?string $path = null): void
{
$path = $path ?? static::currentPath();
$routers = match($_SERVER['REQUEST_METHOD']) { // Según el método selecciona un arreglo de routers
'POST' => static::$post,
'PUT' => static::$put,
'PATCH' => static::$patch,
'DELETE' => static::$delete,
default => static::$get
};
foreach ($routers as $router) { // revisa todos los routers para ver si coinciden con la ruta actual
if (preg_match_all('/^'.$router['path'].'\/?$/si',$path, $matches, PREG_PATTERN_ORDER)) {
unset($matches[0]);
// Comprobando pseudo variables en la ruta
// Objtener un reflection del callback
$lastCallback = $router['callback'][0];
if ($lastCallback instanceof \Closure) { // si es función anónima
$reflectionCallback = new \ReflectionFunction($lastCallback);
} else {
if (is_string($lastCallback))
$lastCallback = preg_split('/::/', $lastCallback);
// Revisamos su es un método o solo una función
if (count($lastCallback) == 2)
$reflectionCallback = new \ReflectionMethod($lastCallback[0], $lastCallback[1]);
else
$reflectionCallback = new \ReflectionFunction($lastCallback[0]);
}
// Obtener los parámetros
$arguments = $reflectionCallback->getParameters();
if (isset($arguments[0])) {
// Obtenemos la clase del primer parámetro
$argumentClass = strval($arguments[0]->getType());
// Verificamos si la clase está o no tipada
if (empty($argumentClass)) {
$request = new Request;
} else {
$request = new $argumentClass;
// Verificamos que sea instancia de Request (requerimiento)
if (!($request instanceof Request))
throw new \Exception('Bad argument type on router callback.');
}
} else {
$request = new Request;
}
// Comprobando y guardando los parámetros variables de la ruta
if (isset($matches[1])) {
foreach ($matches as $index => $match) {
$paramName = $router['paramNames'][$index-1];
$req->params->$paramName = urldecode($match[0]);
$request->params->$paramName = urldecode($match[0]);
}
}
// Llamar al último callback configurado
$next = array_pop($router['callback']);
$req->next = $router['callback'];
$data = call_user_func_array($next, [$req]);
// Llama a la validación y luego procesa la cola de callbacks
$request->next = $router['callback'];
$data = $request->handle();
// Por defecto imprime como JSON si se retorna algo
if (isset($data)) {
header('Content-Type: application/json');
print(json_encode($data));
@ -296,7 +369,6 @@ class Router {
}
// Si no hay router que coincida llamamos a $notFoundCallBack
call_user_func_array(static::$notFoundCallback, [$req]);
call_user_func_array(static::$notFoundCallback, [new Request]);
}
}
?>

201
src/Libs/Validator.php Normal file
View File

@ -0,0 +1,201 @@
<?php
/**
* Validator - DuckBrain
*
* Libería complementaria de la libería Request.
* Sirve para simplpificar la verificación de valores.
*
* Tiene la posibilida de verificar tanto reglas individuales como en lote.
*
* |----------+--------------------------------------------------------|
* | Regla | Descripción |
* |----------+--------------------------------------------------------|
* | not | Niega la siguiente regla. Ej: not:float |
* | exists | Es requerido; debe estar definido y puede estar vacío |
* | required | Es requerido; debe estar definido y no vacío |
* | number | Es numérico |
* | int | Es entero |
* | float | Es un float |
* | bool | Es booleano |
* | email | Es un correo |
* | enum | Esta en un lista ve valores. Ej: enum:admin,user,guest |
* | url | Es una url válida |
* |----------+--------------------------------------------------------|
*
* Las listas de reglas están separadas por |, Ej: required|email
*
* @author KJ
* @website https://kj2.me
* @licence MIT
*/
namespace Libs;
class Validator {
public static string $lastFailed = '';
/**
* Validar lista de reglas sobre las propiedades de un objeto.
*
* @param array $rulesList Lista de reglas.
* @param Neuron $haystack Objeto al que se le verificarán las reglas.
*
* @return bool Retorna true solo si todas las reglas se cumplen y false en cuanto una falle.
*/
public static function validateList(array $rulesList, Neuron $haystack): bool
{
foreach ($rulesList as $target => $rules) {
$rules = preg_split('/\|/', $rules);
foreach ($rules as $rule) {
if (static::checkRule($haystack->{$target}, $rule))
continue;
static::$lastFailed = $target.'.'.$rule;
return false;
}
}
return true;
}
/**
* Revisa si una regla se cumple.
*
* @param mixed $subject Lo que se va a verfificar.
* @param string $rule La regla a probar.
*
* @return bool
*/
public static function checkRule(mixed $subject, string $rule): bool
{
$arguments = preg_split('/[:,]/', $rule);
$rule = [static::class, $arguments[0]];
$arguments[0] = $subject;
if (is_callable($rule))
return call_user_func_array($rule, $arguments);
throw new \Exception('Bad rule: "'.preg_split('/::/', $rule)[1].'"' );
}
/**
* Verifica la regla de manera negativa.
*
* @param mixed $subject Lo que se va a verfificar.
* @param mixed $rule La regla a probar.
*
* @return bool
*/
public static function not(mixed $subject, ...$rule): bool
{
return !static::checkRule($subject, join(':', $rule));
}
/**
* Comprueba que que esté definido/exista.
*
* @param mixed $subject
*
* @return bool
*/
public static function exists(mixed $subject): bool
{
return isset($subject);
}
/**
* Comprueba que que esté definido y no esté vacío.
*
* @param mixed $subject
*
* @return bool
*/
public static function required(mixed $subject): bool
{
return isset($subject) && !empty($subject);
}
/**
* number
*
* @param mixed $subject
*
* @return bool
*/
public static function number(mixed $subject): bool
{
return is_numeric($subject);
}
/**
* int
*
* @param mixed $subject
*
* @return bool
*/
public static function int(mixed $subject): bool
{
return filter_var($subject, FILTER_VALIDATE_INT);
}
/**
* float
*
* @param mixed $subject
*
* @return bool
*/
public static function float(mixed $subject): bool
{
return filter_var($subject, FILTER_VALIDATE_FLOAT);
}
/**
* bool
*
* @param mixed $subject
*
* @return bool
*/
public static function bool(mixed $subject): bool
{
return filter_var($subject, FILTER_VALIDATE_BOOLEAN);
}
/**
* email
*
* @param mixed $subject
*
* @return bool
*/
public static function email(mixed $subject): bool
{
return filter_var($subject, FILTER_VALIDATE_EMAIL);
}
/**
* url
*
* @param mixed $subject
*
* @return bool
*/
public static function url(mixed $subject): bool
{
return filter_var($subject, FILTER_VALIDATE_URL);
}
/**
* enum
*
* @param mixed $subject
* @param mixed $values
*
* @return bool
*/
public static function enum(mixed $subject, ...$values): bool
{
return in_array($subject, $values);
}
}

View File

@ -11,28 +11,154 @@
namespace Libs;
class View {
class View extends Neuron {
/**
* Incluye el archivo.
*
* @param string $viewName Ruta relativa y el nommbre sin extensión del archivo.
* @param string|null $viewPath (opcional) Ruta donde se encuentra la vista.
* @param string $extension (opcional) Extensión del archivo.
*
* @return void
*/
protected function include(
string $viewName,
?string $viewPath = null,
string $extension = 'php'
): void
{
$view = $this;
if (isset($viewPath) &&
file_exists("$viewPath$viewName.$extension")) {
include("$viewPath$viewName.$extension");
return;
}
include(ROOT_CORE."/Views/$viewName.$extension");
}
/**
* Función que "renderiza" las vistas
*
* @param string $viewName
* Ruta relativa y el nommbre sin extensión del archivo ubicado en src/Views
* @param string $viewName Ruta relativa y el nommbre sin extensión del archivo.
* @param array|Neuron $params (opcional) Arreglo que podrá ser usado en la vista mediante $view ($param['index'] se usaría así: $view->index)
* @param string|null $viewPath (opcional) Ruta donde se encuentra la vista. En caso de que la vista no se encuentre en esa ruta, se usará la ruta por defecto "src/Views/".
* @param string $extension (opcional) Extensión del archivo.
*
* @param array $params
* (opcional) Arreglo que podrá ser usado en la vista mediante $view ($param['index'] se usaría así: $view->index)
*
* @param string $viewPath
* (opcional) Ruta donde se encuentra la vista. En caso de que la vista no se encuentre en esa ruta, se usará la ruta por defecto "src/Views/".
* @return void
*/
public static function render(string $viewName, array $params = [], string $viewPath = null) {
$view = new Neuron($params);
unset($params);
public static function render(
string $viewName,
array|Neuron $params = [],
?string $viewPath = null,
string $extension = 'php'
): void
{
$instance = new View($params);
$instance->html($viewName, $viewPath, $extension);
}
if (isset($viewPath) && file_exists($viewPath.$viewName.'.php'))
return include($viewPath.$viewName.'.php');
/**
* Renderiza las vistas HTML
*
* @param string $viewName Ruta relativa y el nommbre sin extensión del archivo ubicado en src/Views
* @param string|null $viewPath (opcional) Ruta donde se encuentra la vista. En caso de que la vista no se encuentre en esa ruta, se usará la ruta por defecto "src/Views/".
* @param string $extension (opcional) Extensión del archivo.
*
* @return void
*/
public function html(
string $viewName,
?string $viewPath = null,
string $extension = 'php'
): void
{
$this->include(
$viewName,
$viewPath,
$extension
);
}
include(ROOT_DIR.'/src/Views/'.$viewName.'.php');
/**
* Renderiza código CSS.
*
* @param string $viewName Ruta relativa y el nommbre sin extensión del archivo ubicado en src/Views
* @param string|null $viewPath (opcional) Ruta donde se encuentra la vista. En caso de que la vista no se encuentre en esa ruta, se usará la ruta por defecto "src/Views/".
* @param string $extension (opcional) Extensión del archivo.
*
* @return void
*/
public function css(
string $viewName,
?string $viewPath = null,
string $extension = 'css'
): void
{
header("Content-type: text/css");
$this->include($viewName, $viewPath, $extension);
}
/**
* Renderiza código Javascript.
*
* @param string $viewName Ruta relativa y el nommbre sin extensión del archivo ubicado en src/Views
* @param string|null $viewPath (opcional) Ruta donde se encuentra la vista. En caso de que la vista no se encuentre en esa ruta, se usará la ruta por defecto "src/Views/".
* @param string $extension (opcional) Extensión del archivo.
*
* @return void
*/
public function js(
string $viewName,
?string $viewPath = null,
string $extension = 'js'
): void
{
header("Content-type: application/javascript");
$this->include($viewName, $viewPath, $extension);
}
/**
* Imprime los datos en Json.
*
* @param object|array $data Objeto o array que se imprimirá a JSON.
*
* @return void
*/
public function json(object|array $data): void
{
header('Content-Type: application/json; charset=utf-8');
print(json_encode($data));
}
/**
* Imprime los datos en texto plano
*
* @param string $txt Contenido de texto.
*
* @return void
*/
public function text(string $txt): void
{
header('Content-Type: text/plain; charset=utf-8');
print($txt);
}
/**
* Intenta devolver la url absoluta a partir de una ruta relativa.
*
* @param string $path
*
* @return string
*/
public static function route(string $path = '/'): string
{
if (defined('SITE_URL') && !empty(SITE_URL))
return rtrim(SITE_URL, '/').'/'.ltrim($path, '/');
return $path;
}
}
?>