/** * Fonction permettant de lancer les fonctions de rappel. * * @param Request $req * @param array $namespaces * @param Application $app * * @return mixed */ public function call(Request $req, $namespaces, Application $app = null) { $params = []; // Association des parmatres à la request foreach ($this->keys as $key => $value) { if (!is_int($this->match[$key])) { $params[$value] = $this->match[$key]; } else { $tmp = (int) $this->match[$key]; $params[$value] = $tmp; $this->match[$key] = $tmp; } } if ($app !== null) { array_unshift($this->match, $app); } $req::$params = (object) $params; return Util::launchCallback($this->cb, $this->match, $namespaces); }
/** * connection, lance la connection sur la DB * * @param null $zone * @param null $cb * @return null|Database */ public static function connection($zone = null, $cb = null) { if (static::$db instanceof PDO) { return static::takeInstance(); } if (!static::$config instanceof StdClass) { return Util::launchCallback($cb, [new ConnectionException("Le fichier database.php est mal configurer")]); } if (is_callable($zone)) { $cb = $zone; $zone = null; } if ($zone != null) { static::$zone = $zone; } $c = isset(static::$config->connections[static::$zone]) ? static::$config->connections[static::$zone] : null; if (is_null($c)) { Util::launchCallback($cb, [new ConnectionException("La clé '" . static::$zone . "' n'est pas définir dans l'entre database.php")]); } $db = null; try { // Variable contenant les informations sur // utilisateur $username = null; $password = null; // Configuration suppelement coté PDO $pdoPostConfiguation = [PDO::ATTR_DEFAULT_FETCH_MODE => static::$config->fetch]; switch ($c["scheme"]) { case "mysql": // Construction de la dsn $dns = "mysql:host=" . $c["mysql"]['hostname'] . ($c["mysql"]['port'] !== null ? ":" . $c["mysql"]["port"] : "") . ";dbname=" . $c["mysql"]['database']; $username = $c["mysql"]["username"]; $password = $c["mysql"]["password"]; $pdoPostConfiguation[PDO::MYSQL_ATTR_INIT_COMMAND] = "SET NAMES " . Str::upper($c["mysql"]["charset"]); break; case "sqlite": $dns = $c["sqlite"]["driver"] . ":" . $c["sqlite"]["database"]; break; default: throw new DatabaseException("Vérifiez la configuration de la base de donnée.", E_USER_ERROR); break; } // Connection à la base de donnée. static::$db = new PDO($dns, $username, $password, $pdoPostConfiguation); } catch (PDOException $e) { /** * Lancement d'exception */ static::$errorInfo = [$e->getCode(), true, $e->getMessage()]; Util::launchCallback($cb, [$e]); } Util::launchCallback($cb, [false]); return static::class; }
/** * REST API Maker. * * @param string $url * @param string|array $controllerName * @param array $where * @return $this * @throws ApplicationException */ public function resources($url, $controllerName, array $where = []) { if (!is_string($controllerName) && !is_array($controllerName)) { throw new ApplicationException('Le premier paramètre doit être un array ou une chaine de caractère', 1); } $controller = ''; $internalMiddleware = null; $ignoreMethod = []; $valideMethod = [['url' => '/', 'call' => 'index', 'method' => 'get'], ['url' => '/', 'call' => 'store', 'method' => 'post'], ['url' => '/:id', 'call' => 'show', 'method' => 'get'], ['url' => '/:id', 'call' => 'update', 'method' => 'put'], ['url' => '/:id', 'call' => 'destroy', 'method' => 'delete'], ['url' => '/:id/edit', 'call' => 'edit', 'method' => 'get'], ['url' => '/create', 'call' => 'create', 'method' => 'get']]; if (is_array($controllerName)) { if (isset($controllerName['middleware'])) { $internalMiddleware = $controllerName['middleware']; unset($controllerName['middleware']); $next = Util::launchCallback(['middleware' => $internalMiddleware], $this->request); if ($next === false) { return $this; } } if (isset($controllerName['use'])) { $controller = $controllerName['use']; unset($controllerName['use']); } if (isset($controllerName['ignores'])) { $ignoreMethod = $controllerName['ignores']; unset($controllerName['ignores']); } } else { $controller = $controllerName; } // normalize url $url = preg_replace('/\\/+$/', '', $url); // Association de url prédéfinie foreach ($valideMethod as $key => $value) { // on vérifie si la methode de appelé est ignoré if (!in_array($value['call'], $ignoreMethod)) { // Formate controlleur $bindController = $controller . '@' . $value['call']; $path = $url . $value['url']; $this->namedRoute($path, strtolower(preg_replace('/controller/i', '', $controller)) . '.' . $value['call']); // Lancement de la methode de mapping de route. call_user_func_array([$this, $value['method']], [rtrim($path, '/'), $bindController]); // Association des critères définies if (!empty($where)) { $data = []; if (preg_match('/:id/', $path)) { if (isset($where['id'])) { $data = $where; } else { $data = ['id' => $where[0]]; } } $this->where(array_merge($data, $where)); } } } return $this; }
/** * off supprime un event enregistre * * @param string $event * @param Callable $cb */ public static function off($event, $cb = null) { if (static::$events->has($event)) { static::$events->delete($event); Util::launchCallback($cb); } }