Skip to content

Commit

Permalink
New Config
Browse files Browse the repository at this point in the history
  • Loading branch information
supremacia committed Jun 27, 2017
1 parent 8814f47 commit 88843c8
Show file tree
Hide file tree
Showing 7 changed files with 128 additions and 30 deletions.
4 changes: 4 additions & 0 deletions Config/About.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php
\Devbr\Router::this()->respond('get', 'about', function () {
echo '<h1>About</h1><p>Welcome</p>';
});
4 changes: 0 additions & 4 deletions Config/Lib/Router/About.php

This file was deleted.

8 changes: 4 additions & 4 deletions Config/Lib/Router/Router.php → Config/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
*/


namespace Config\Lib\Router;
namespace Config\Devbr\Router;

/**
* Config\Router Class
Expand All @@ -30,7 +30,7 @@ class Router
function __construct()
{
//Defaults routers
\Lib\Router::this()->respond('get', '/', 'Resource\Main::index')
\Devbr\Router::this()->respond('get', '/', 'Resource\Main::index')
->respond('options|head', '.*', function () {
header('Access-Control-Allow-Methods: POST, GET, OPTIONS, DELETE, PUT, PATCH, HEAD');
exit();
Expand Down Expand Up @@ -77,8 +77,8 @@ private function loadRouterConfig($dir = null)
<request>: String of the requested URI - ex.: "about/me" ==> http://site.com/about/me
<controller>: Class (object) to manage the request.
Name must be a complete string, with NAMESPACE + CLASSNAME. Ex.: "Lib\User".
Alternatively you can use the following format: "controller::action" - ex.: "Lib\User::login".
Name must be a complete string, with NAMESPACE + CLASSNAME. Ex.: "Devbr\User".
Alternatively you can use the following format: "controller::action" - ex.: "Devbr\User::login".
The Controller can also be an anonymous function that receives (or not)
parameters of the regular expression in <request>.
Ex.: $router->respond('get', '/(*)', function($path){exit($path);});
Expand Down
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
# Access Manager - Router
# Access Manager - Router2

## Install

Use Composer for easy installation:

```php
Composer require devbr/router
Composer require devbr/router2
```

Or install the full base for PHP websites, at "https://github.com/devbr/website".

More info: https://packagist.org/packages/devbr/router
More info: https://packagist.org/packages/devbr/router2

## Access Management

Expand Down Expand Up @@ -46,9 +46,9 @@ $router->respond( <type>, <request>, <controller>, [<action>]);

<controller>: Class (object) to manage the request.
Name must be a complete string, with NAMESPACE + CLASSNAME.
Ex.: "Lib\User".
Ex.: "Devbr\User".
Alternatively you can use the following format: "controller::action".
Ex.: "Lib\User::login".
Ex.: "Devbr\User::login".
The Controller can also be an anonymous function that receives (or not)
parameters of the regular expression in <request>.
Ex.: $router->respond('get',
Expand Down
12 changes: 8 additions & 4 deletions Router.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Lib\Router
* Devbr\Router
* PHP version 7
*
* @category Access
Expand All @@ -11,7 +11,7 @@
* @version GIT: 0.0.1
* @link http://paulorocha.tk/devbr
*/
namespace Lib;
namespace Devbr;

/**
* Router Class
Expand Down Expand Up @@ -130,6 +130,10 @@ function __construct(

$this->method = $this->requestMethod();
$this->mount();

if (!is_object(static::$node)) {
static::$node = $this;
}
}
/**
* Singleton instance
Expand Down Expand Up @@ -160,8 +164,8 @@ static function getCtrl()
function run()
{
//Load configurations
if (class_exists('\Config\Lib\Router\Router')) {
new \Config\Lib\Router\Router;
if (class_exists('\Config\Devbr\Router\Router')) {
new \Config\Devbr\Router\Router;
}

//Resolve request
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@
"php": ">=5.6.28"
},
"autoload": {
"psr-4": {"Lib\\": ""}
"psr-4": {"Devbr\\": ""}
}
}
118 changes: 106 additions & 12 deletions install.php
Original file line number Diff line number Diff line change
@@ -1,25 +1,119 @@
<?php

if (php_sapi_name() !== 'cli' || !defined('_CONFIG')) {
exit("\n\tI can not run out of system!\n");
if (php_sapi_name() !== 'cli') {
ret('It only runs in CLI mode!');
}

$composer_psr4 = dirname(dirname(__DIR__)).'/composer/autoload_psr4.php';
if (!file_exists($composer_psr4)) {
ret("I can't find Composer data!");
}

$composer = require_once $composer_psr4;
if (!isset($composer['Config\\'][0])) {
ret("I can't find Composer data!");
}

//load composer.json and get the "name" of pack
$namespace = @json_decode(file_get_contents(__DIR__.'/composer.json'))->name;
$appConfig = $composer['Config\\'][0].'/'.$namespace.'/';

$thisConfig = __DIR__.'/Config/';
//exit("\n\n\---> $appConfig\n");

if (!is_dir($thisConfig)) {
return;
ret("I can't find 'Config' directory in this pack!");
}

$namespace = @json_decode(file_get_contents(__DIR__.'/composer.json'))->name;
/* OPTIONAL
* load composer.json and get the "name" of pack
* $appConfig = _CONFIG.$namespace;
*/

$appConfig = _CONFIG;
if (is_dir($appConfig)) {
ret("Configuration already exists - ignored.");
}

//Coping all files (and directorys) in /Config
$copy = \Lib\Cli\Main::copyDirectoryContents($thisConfig, $appConfig);
$copy = copyDirectoryContents($thisConfig, $appConfig);

//Return to application installer
return "\n---".($copy === true ? " $namespace instaled!" : $copy);
ret($copy === true ? " $namespace instaled!" : $copy);

// THE END ...



/**
* Check or create a directory
*
* @param string $dir path of the directory
* @param boolean $create False/true for create
* @param string $perm indiucates a permission - default 0777
*
* @return bool status of directory (exists/created = false or true)
*/
function checkAndOrCreateDir($dir, $create = false, $perm = 0777)
{
if (is_dir($dir) && is_writable($dir)) {
return true;
} elseif ($create === false) {
return false;
}

@mkdir($dir, $perm, true);
@chmod($dir, $perm);

if (is_writable($dir)) {
return true;
}
return false;
}

/**
* Copy entire content of the $dir[ectory]
*
* @param string $dir Origin
* @param string $target Destination
*
* @return bool True/false success
*/
function copyDirectoryContents($dir, $target)
{
$dir = rtrim($dir, "\\/ ").'/';
$target = rtrim($target, "\\/ ").'/';

if (!checkAndOrCreateDir($target, true, 0777)) {
return "ERROR: can't create directory '$taget'!";
}

foreach (scandir($dir) as $file) {
if ($file == '.' || $file == '..') {
continue;
}

if (is_dir($dir.$file)) {
if (!checkAndOrCreateDir($target.$file, true, 0777)) {
return "ERROR: can't create directory '$taget$file'!";
} else {
$copy = copyDirectoryContents($dir.$file, $target.$file);
if ($copy !== true) {
return $copy;
}
}
} elseif (is_file($dir.$file)) {
if (!copy($dir.$file, $target.$file)) {
echo "\n ERROR: can't copy '$target$file'!";
}
}
}
return true;
}

/**
* Return with message
*
* @param string $msg message
*
* @return void print and exit
*/
function ret($msg)
{
echo "\n - $msg\n\n";
return true;
}

0 comments on commit 88843c8

Please sign in to comment.