Skip to content

Commit 25e374b

Browse files
committed
New Configuration style
1 parent 18573ac commit 25e374b

File tree

6 files changed

+124
-30
lines changed

6 files changed

+124
-30
lines changed
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
* @link http://paulorocha.tk/devbr
1313
*/
1414

15-
namespace Config\Lib;
15+
namespace Config\Devbr;
1616

1717
/**
1818
* Config\Database Class
@@ -34,7 +34,7 @@ class Database
3434
];
3535
static $default = 'mysql';
3636

37-
//Configuração da tabela de usuário | Lib\User
37+
//Configuração da tabela de usuário | Devbr\User
3838
static $userTable = ['table'=>'usuario',
3939
'id'=>'id',
4040
'name'=>'nome',

Db.php renamed to Database.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
/**
3-
* Lib\Db
3+
* Devbr\Database
44
* PHP version 7
55
*
66
* @category Database
@@ -12,20 +12,20 @@
1212
* @link http://paulorocha.tk/devbr
1313
*/
1414

15-
namespace Lib;
15+
namespace Devbr;
1616

1717
use PDO;
1818

1919
/**
20-
* Db Class
20+
* Database Class
2121
*
2222
* @category Database
2323
* @package Data
2424
* @author Bill Rocha <[email protected]>
2525
* @license <https://opensource.org/licenses/MIT> MIT
2626
* @link http://paulorocha.tk/devbr
2727
*/
28-
class Db
28+
class Database
2929
{
3030
private $config = null;
3131

@@ -46,8 +46,8 @@ function __construct($config = null)
4646
{
4747
if (is_array($config)) {
4848
$this->config = $config;
49-
} elseif (method_exists('Config\Lib\Database', 'get')) {
50-
$this->config = \Config\Lib\Database::get($config);
49+
} elseif (method_exists('Config\Devbr\Database\Database', 'get')) {
50+
$this->config = \Config\Devbr\Database\Database::get($config);
5151
} else {
5252
trigger_error('DataBase configurations not found!');
5353
}
@@ -99,7 +99,7 @@ function query($sql, $parms = array(), $alias = null)
9999
$this->error[$sql] = $sth->errorInfo();
100100

101101
if ($sth->columnCount() > 0) {
102-
return $this->result = $sth->fetchAll(PDO::FETCH_CLASS, "Lib\Row", [$this->sql, $parms]);
102+
return $this->result = $sth->fetchAll(PDO::FETCH_CLASS, "Devbr\Row", [$this->sql, $parms]);
103103
} else {
104104
$this->result = false;
105105
return $this->rows;

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ Uma simples utilização do Database:
1313
```php
1414
$cfg = [];
1515

16-
$db = new Lib\Db($cfg);
16+
$db = new Devbr\Database($cfg);
1717
print_r($db->query('SELECT * FROM TABELA'));
1818
```
1919

2020
Se você estiver usando o 'devbr/website' como base do seu projeto é possível extender a configuração na classe 'Config\Database', ficando mais simples o uso:
2121

2222
```php
23-
$db = new Lib\Db;
23+
$db = new Devbr\Database;
2424

2525
$db->query('SELECT * FROM TABELA');
2626

User.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
/**
3-
* Lib\User
3+
* Devbr\User
44
* PHP version 7
55
*
66
* @category Database
@@ -12,9 +12,9 @@
1212
* @link http://paulorocha.tk/devbr
1313
*/
1414

15-
namespace Lib;
15+
namespace Devbr;
1616

17-
use Lib\Db;
17+
use Devbr\Database;
1818

1919
/**
2020
* User Class
@@ -63,11 +63,11 @@ public function __construct($config = null)
6363
$this->_dbConfig[$i] = $d;
6464
}
6565
}
66-
} elseif (method_exists('Config\Lib\Database', 'getUserConfig')) {
67-
$this->_dbConfig = \Config\Lib\Database::getUserConfig();
66+
} elseif (method_exists('Config\Devbr\Database\Database', 'getUserConfig')) {
67+
$this->_dbConfig = \Config\Devbr\Database\Database::getUserConfig();
6868
}
6969

70-
$this->_db = new Db;
70+
$this->_db = new Database;
7171
}
7272

7373
/**

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@
1919
"php": ">=5.6.28"
2020
},
2121
"autoload": {
22-
"psr-4": {"Lib\\": ""}
22+
"psr-4": {"Devbr\\": ""}
2323
}
2424
}

install.php

Lines changed: 106 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,119 @@
11
<?php
22

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

7+
$composer_psr4 = dirname(dirname(__DIR__)).'/composer/autoload_psr4.php';
8+
if (!file_exists($composer_psr4)) {
9+
ret("I can't find Composer data!");
10+
}
11+
12+
$composer = require_once $composer_psr4;
13+
if (!isset($composer['Config\\'][0])) {
14+
ret("I can't find Composer data!");
15+
}
16+
17+
//load composer.json and get the "name" of pack
18+
$namespace = @json_decode(file_get_contents(__DIR__.'/composer.json'))->name;
19+
$appConfig = $composer['Config\\'][0].'/'.$namespace.'/';
20+
721
$thisConfig = __DIR__.'/Config/';
22+
//exit("\n\n\---> $appConfig\n");
823

924
if (!is_dir($thisConfig)) {
10-
return;
25+
ret("I can't find 'Config' directory in this pack!");
1126
}
1227

13-
$namespace = @json_decode(file_get_contents(__DIR__.'/composer.json'))->name;
14-
/* OPTIONAL
15-
* load composer.json and get the "name" of pack
16-
* $appConfig = _CONFIG.$namespace;
17-
*/
18-
19-
$appConfig = _CONFIG;
28+
if (is_dir($appConfig)) {
29+
ret("Configuration already exists - ignored.");
30+
}
2031

2132
//Coping all files (and directorys) in /Config
22-
$copy = \Lib\Cli\Main::copyDirectoryContents($thisConfig, $appConfig);
33+
$copy = copyDirectoryContents($thisConfig, $appConfig);
2334

2435
//Return to application installer
25-
return "\n---".($copy === true ? " $namespace instaled!" : $copy);
36+
ret($copy === true ? " $namespace instaled!" : $copy);
37+
38+
// THE END ...
39+
40+
41+
42+
/**
43+
* Check or create a directory
44+
*
45+
* @param string $dir path of the directory
46+
* @param boolean $create False/true for create
47+
* @param string $perm indiucates a permission - default 0777
48+
*
49+
* @return bool status of directory (exists/created = false or true)
50+
*/
51+
function checkAndOrCreateDir($dir, $create = false, $perm = 0777)
52+
{
53+
if (is_dir($dir) && is_writable($dir)) {
54+
return true;
55+
} elseif ($create === false) {
56+
return false;
57+
}
58+
59+
@mkdir($dir, $perm, true);
60+
@chmod($dir, $perm);
61+
62+
if (is_writable($dir)) {
63+
return true;
64+
}
65+
return false;
66+
}
67+
68+
/**
69+
* Copy entire content of the $dir[ectory]
70+
*
71+
* @param string $dir Origin
72+
* @param string $target Destination
73+
*
74+
* @return bool True/false success
75+
*/
76+
function copyDirectoryContents($dir, $target)
77+
{
78+
$dir = rtrim($dir, "\\/ ").'/';
79+
$target = rtrim($target, "\\/ ").'/';
80+
81+
if (!checkAndOrCreateDir($target, true, 0777)) {
82+
return "ERROR: can't create directory '$taget'!";
83+
}
84+
85+
foreach (scandir($dir) as $file) {
86+
if ($file == '.' || $file == '..') {
87+
continue;
88+
}
89+
90+
if (is_dir($dir.$file)) {
91+
if (!checkAndOrCreateDir($target.$file, true, 0777)) {
92+
return "ERROR: can't create directory '$taget$file'!";
93+
} else {
94+
$copy = copyDirectoryContents($dir.$file, $target.$file);
95+
if ($copy !== true) {
96+
return $copy;
97+
}
98+
}
99+
} elseif (is_file($dir.$file)) {
100+
if (!copy($dir.$file, $target.$file)) {
101+
echo "\n ERROR: can't copy '$target$file'!";
102+
}
103+
}
104+
}
105+
return true;
106+
}
107+
108+
/**
109+
* Return with message
110+
*
111+
* @param string $msg message
112+
*
113+
* @return void print and exit
114+
*/
115+
function ret($msg)
116+
{
117+
echo "\n - $msg";
118+
return true;
119+
}

0 commit comments

Comments
 (0)