Skip to content

Commit c9209ba

Browse files
author
Fredrick Peter
committed
Autoload Register, Server Path -- Critical Update
1 parent 15411f1 commit c9209ba

File tree

7 files changed

+234
-1303
lines changed

7 files changed

+234
-1303
lines changed

README.md

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ was pretty tough. So i decided to create a much more easier way of communicating
8989
* [Get Database Connection](#get-database-connection)
9090
* [Database Import](#database-import)
9191
* [Update Env Variable](#update-env-variable)
92+
* [OrmDotEnv Servers](#OrmDotEnv-servers)
93+
* [Autoload Register](#autoload-register)
9294
* [Collation And Charset](#collation-and-charset)
9395
* [Extend DB Class](#extend-db-class)
9496
* [Helpers](#helpers)
@@ -108,7 +110,7 @@ Prior to installing `php-orm-database` get the [Composer](https://getcomposer.or
108110
**Step 1** — update your `composer.json`:
109111
```composer.json
110112
"require": {
111-
"peterson/php-orm-database": "^4.0.1"
113+
"peterson/php-orm-database": "^4.1.1"
112114
}
113115
```
114116

@@ -1241,6 +1243,34 @@ Returns - Boolean
12411243
true|false
12421244
```
12431245

1246+
## OrmDotEnv Servers
1247+
- Returns assoc arrays of Server
1248+
- `server/|domain/|protocol`
1249+
1250+
```
1251+
use builder\Database\OrmDotEnv;
1252+
1253+
OrmDotEnv::getServers();
1254+
1255+
or
1256+
dot_env()::getServers('server');
1257+
dot_env()->getServers('domain');
1258+
```
1259+
1260+
## Autoload Register
1261+
- You can use register collection of all folder files
1262+
- This automatically includes all file in that folder and sub-folders
1263+
1264+
```
1265+
use builder\Database\AutoloadRegister;
1266+
1267+
AutoloadRegister::load('folder');
1268+
1269+
or
1270+
autoload_register()::load('folder');
1271+
autoload_register()->load('folder');
1272+
```
1273+
12441274
## Collation And Charset
12451275
- Collation and Charset Data `listing`
12461276

@@ -1295,6 +1325,7 @@ class PostClass extends DB{
12951325
| migration() | Return instance of `(new Migration)` class |
12961326
| schema() | Return instance of `(new Schema)` class |
12971327
| dot_env() | Return instance of `(new OrmDotEnv)` class |
1328+
| autoload_register() | Return instance of `(new AutoloadRegister)` class |
12981329
| autoload_env() | Return instance of `(new AutoloadEnv)` class |
12991330
| env_start() | Same as `AutoloadEnv::start()` |
13001331
| config_database() | Same as `Direct DB Connection` get access to `DATABASE_CONNECTION` Constant |

src/AutoloadEnv.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,14 @@ static public function start(?array $options = [])
3333
'bg' => $options['bg'] ?? 'default',
3434
];
3535

36+
3637
/*
3738
|--------------------------------------------------------------------------
3839
| Instance of class
3940
|--------------------------------------------------------------------------
4041
*/
4142
$ormDotEnv = new OrmDotEnv($default['path']);
42-
43+
4344

4445
/*
4546
|--------------------------------------------------------------------------

src/AutoloadRegister.php

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace builder\Database;
6+
7+
use RecursiveIteratorIterator;
8+
use RecursiveDirectoryIterator;
9+
use builder\Database\Schema\OrmDotEnv;
10+
11+
class AutoloadRegister{
12+
13+
/**
14+
* Autoload All Folder and Sub-Folder files
15+
*
16+
* @param string $path_to_folder
17+
* - Specify the folder to autoload
18+
* - Do not include the root path, as The Application already have a copy of your path
19+
* - e.g [classes] or [app/main]
20+
*
21+
* @return void
22+
*/
23+
static public function load(?string $path_to_folder = null)
24+
{
25+
// If path is not null
26+
if(!empty($path_to_folder)){
27+
spl_autoload_register(function ($className) use($path_to_folder) {
28+
self::register($path_to_folder);
29+
});
30+
}
31+
}
32+
33+
/**
34+
* Register method
35+
*
36+
* @param string $path_to_folder
37+
*
38+
* @return void
39+
*/
40+
static private function register($path_to_folder)
41+
{
42+
// directory full path
43+
$directory = self::convertPath($path_to_folder);
44+
45+
// Create a recursive iterator to iterate through the directory
46+
$iterator = new RecursiveIteratorIterator(
47+
new RecursiveDirectoryIterator(
48+
$directory,
49+
RecursiveDirectoryIterator::SKIP_DOTS
50+
),
51+
RecursiveIteratorIterator::LEAVES_ONLY
52+
);
53+
54+
// Loop through the iterator
55+
foreach ($iterator as $file) {
56+
// Check if the item is a file and has a PHP extension
57+
if ($file->isFile() && $file->getExtension() === 'php') {
58+
include_once $file->getPathname();
59+
}
60+
}
61+
}
62+
63+
/**
64+
* Convert path
65+
*
66+
* @param string $path_to_folder
67+
*
68+
* @return string
69+
*/
70+
static private function convertPath($path_to_folder)
71+
{
72+
$ormDotEnv = new OrmDotEnv();
73+
return str_replace(
74+
'/',
75+
'\\',
76+
$ormDotEnv->getDirectory() . "/{$path_to_folder}"
77+
);
78+
}
79+
80+
}

src/Schema/OrmDotEnv.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,11 @@ class OrmDotEnv extends Constants{
2828
public function __construct(?string $path = null)
2929
{
3030
// if base path was presented
31-
if(!is_null($path) && !empty($path)){
31+
if(!empty($path)){
3232
$this->base_dir = $path;
3333
}
34+
35+
// auto set the base dir property
3436
$this->getDirectory($this->base_dir);
3537

3638
// add to global property

src/Traits/ServerTrait.php

Lines changed: 92 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,14 @@ public function __call( $key, $value )
3737
*/
3838
public function getDirectory()
3939
{
40-
if(empty($this->base_dir) || is_null($this->base_dir)){
41-
// get default project root document path
40+
if(empty($this->base_dir)){
41+
// get default project root path
4242
$this->base_dir = $this->clean_path( $this->server_root() );
4343
}else{
4444
$this->base_dir = $this->clean_path($this->base_dir);
4545
}
4646

47-
// remove .env string if found along with directory path
48-
return str_replace('.env', '', $this->base_dir);
47+
return $this->base_dir;
4948
}
5049

5150
/**
@@ -55,7 +54,95 @@ public function getDirectory()
5554
*/
5655
private function server_root()
5756
{
58-
return rtrim(str_replace('src', '', str_replace('\\', '/', realpath('.')) ), '/');
57+
return $this->getServers('server');
58+
}
59+
60+
/**
61+
* Get the base URL and domain information.
62+
*
63+
* @param string $mode
64+
* - [optional] get direct info of data
65+
* - server|domain|protocol
66+
*
67+
* @return mixed
68+
* - An associative array containing\ server|domain|protocol
69+
*/
70+
static public function getServers(?string $mode = null)
71+
{
72+
// Determine the protocol (http or https)
73+
$protocol = isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !== 'off' ? 'https://' : 'http://';
74+
75+
// Construct the server root path
76+
$docRoot = $_SERVER['DOCUMENT_ROOT'];
77+
78+
// Get the server name (hostname)
79+
$serverName = $_SERVER['SERVER_NAME'];
80+
81+
// get Base directory path
82+
$basePath = self::getMatchedData('init.php');
83+
84+
// if false, then get absolute path
85+
if($basePath === false){
86+
$basePath = self::getRootPathToProject(realpath('.'));
87+
}
88+
89+
// Construct the server root URL
90+
$serverPath = rtrim("{$docRoot}{$basePath}", '/');
91+
92+
// Construct the domain
93+
$domainPath = rtrim("{$protocol}{$serverName}{$basePath}", '/');
94+
95+
// Data
96+
$data = [
97+
'server' => $serverPath,
98+
'domain' => $domainPath,
99+
'protocol' => $protocol,
100+
];
101+
102+
return $data[$mode] ?? $data;
103+
}
104+
105+
/**
106+
* Get the data of the string that matches the search string in the given backtrace.
107+
*
108+
* @param string $searchString The string to search for.
109+
*
110+
* @return string|false
111+
* -The matched data or null if no match is found.
112+
*/
113+
static private function getMatchedData(?string $searchString)
114+
{
115+
// backtrace file column data
116+
$backtrace = array_column(debug_backtrace(), 'file');
117+
118+
if(is_array($backtrace)){
119+
foreach ($backtrace as $trace) {
120+
if (strpos($trace, $searchString) !== false) {
121+
return self::getRootPathToProject($trace, $searchString);
122+
}
123+
}
124+
}
125+
126+
return false;
127+
}
128+
129+
/**
130+
* Clean and get Root Path to Project
131+
*
132+
* @param string $path
133+
* @param string $string
134+
*
135+
* @return string|null
136+
*/
137+
static private function getRootPathToProject(?string $path = null, ?string $string = null)
138+
{
139+
// Construct the server root path
140+
$docRoot = $_SERVER['DOCUMENT_ROOT'];
141+
142+
$traceData = str_replace('\\', '/', (string) $path);
143+
$traceData = str_replace([$docRoot, (string) $string], '', $traceData);
144+
145+
return $traceData;
59146
}
60147

61148
/**

src/helpers.php

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44
use builder\Database\DBImport;
55
use builder\Database\AutoloadEnv;
66
use builder\Database\Query\MySqlExec;
7+
use builder\Database\AutoloadRegister;
78
use builder\Database\Schema\OrmDotEnv;
8-
use builder\Database\Migrations\Migration;
99
use builder\Database\Migrations\Schema;
10+
use builder\Database\Migrations\Migration;
1011

1112
if (! function_exists('db')) {
1213
/**
@@ -29,7 +30,7 @@ function db(?array $options = [])
2930
*/
3031
function import()
3132
{
32-
return (new DBImport);
33+
return new DBImport();
3334
}
3435
}
3536

@@ -41,7 +42,7 @@ function import()
4142
*/
4243
function dot_env()
4344
{
44-
return (new OrmDotEnv);
45+
return new OrmDotEnv();
4546
}
4647
}
4748

@@ -53,7 +54,7 @@ function dot_env()
5354
*/
5455
function migration()
5556
{
56-
return (new Migration);
57+
return new Migration();
5758
}
5859
}
5960

@@ -65,7 +66,24 @@ function migration()
6566
*/
6667
function schema()
6768
{
68-
return (new Schema);
69+
return new Schema();
70+
}
71+
}
72+
73+
if (! function_exists('autoload_register')) {
74+
/**
75+
* Autoload All Folder and Sub-Folder files
76+
*
77+
* @param string $path_to_folder
78+
* - Specify the folder to autoload
79+
* - Do not include the root path, as The Application already have a copy of your path
80+
* - e.g [classes] or [app/main]
81+
*
82+
* @return void\builder\Database\AutoloadRegister
83+
*/
84+
function autoload_register()
85+
{
86+
return new AutoloadRegister();
6987
}
7088
}
7189

@@ -77,7 +95,7 @@ function schema()
7795
*/
7896
function autoload_env()
7997
{
80-
return (new AutoloadEnv);
98+
return new AutoloadEnv();
8199
}
82100
}
83101

@@ -89,7 +107,7 @@ function autoload_env()
89107
*/
90108
function db_exec()
91109
{
92-
return (new MySqlExec);
110+
return new MySqlExec();
93111
}
94112
}
95113

0 commit comments

Comments
 (0)