Skip to content

Commit 1725b48

Browse files
committed
Add autoload.php
1 parent a081a5a commit 1725b48

5 files changed

Lines changed: 100 additions & 13 deletions

File tree

autoload.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
/**
4+
* CodeMommy AutoloadPHP
5+
* @author Candison November <www.kandisheng.com>
6+
*/
7+
8+
if (!file_exists('autoloadDirectory')) {
9+
/**
10+
* Autoload Directory
11+
* @param string $directory
12+
* @param string $namespaceRoot
13+
*/
14+
function autoloadDirectory($directory = '.', $namespaceRoot = '')
15+
{
16+
spl_autoload_register(function ($className) use ($directory, $namespaceRoot) {
17+
$directory = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $directory);
18+
$directory = rtrim($directory, '/\\');
19+
$namespaceRoot = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $namespaceRoot);
20+
$namespaceRoot = trim($namespaceRoot, '/\\');
21+
$className = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $className);
22+
$className = trim($className, '/\\');
23+
if (substr($className, 0, strlen($namespaceRoot)) == $namespaceRoot) {
24+
$className = substr($className, strlen($namespaceRoot));
25+
$className = ltrim($className, '/\\');
26+
}
27+
$extensionList = array('php', 'class.php');
28+
foreach ($extensionList as $extension) {
29+
$file = $directory . DIRECTORY_SEPARATOR . $className . '.' . $extension;
30+
if (is_file($file) && is_readable($file)) {
31+
require_once($file);
32+
}
33+
}
34+
});
35+
}
36+
}
37+
38+
autoloadDirectory(sprintf('%s%ssource', __DIR__, DIRECTORY_SEPARATOR), 'CodeMommy\AutoloadPHP');
39+
autoloadDirectory(sprintf('%s%sinterface', __DIR__, DIRECTORY_SEPARATOR), 'CodeMommy\AutoloadPHP');

composer.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@
2424
],
2525
"autoload": {
2626
"psr-4": {
27-
"CodeMommy\\AutoloadPHP\\": "source/"
27+
"CodeMommy\\AutoloadPHP\\": [
28+
"interface/",
29+
"source/"
30+
]
2831
}
2932
},
3033
"autoload-dev": {

interface/AutoloadInterface.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
/**
4+
* CodeMommy AutoloadPHP
5+
* @author Candison November <www.kandisheng.com>
6+
*/
7+
8+
namespace CodeMommy\AutoloadPHP;
9+
10+
/**
11+
* Interface AutoloadInterface
12+
* @package CodeMommy\AutoloadPHP
13+
*/
14+
interface AutoloadInterface
15+
{
16+
/**
17+
* AutoloadInterface constructor.
18+
*/
19+
public function __construct();
20+
21+
/**
22+
* Directory
23+
* @param string $directory
24+
* @param string $namespaceRoot
25+
* @return mixed
26+
*/
27+
public static function directory($directory = '.', $namespaceRoot = '');
28+
29+
/**
30+
* File
31+
* @param string $file
32+
* @param string $className
33+
* @return mixed
34+
*/
35+
public static function file($file = '', $className = '');
36+
37+
/**
38+
* Basic
39+
* @param string $file
40+
* @param bool $isOnce
41+
* @return mixed
42+
*/
43+
public static function basic($file = '', $isOnce = true);
44+
}

phpunit.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
</testsuites>
99
<filter>
1010
<whitelist processUncoveredFilesFromWhitelist="true">
11+
<directory suffix=".php">./interface</directory>
1112
<directory suffix=".php">./source</directory>
1213
</whitelist>
1314
</filter>

source/Autoload.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
* Class Autoload
1212
* @package CodeMommy\AutoloadPHP
1313
*/
14-
class Autoload
14+
class Autoload implements AutoloadInterface
1515
{
1616
/**
1717
* Autoload constructor.
@@ -22,10 +22,10 @@ public function __construct()
2222

2323
/**
2424
* Directory
25-
* @param $directory
26-
* @param $namespaceRoot
25+
* @param string $directory
26+
* @param string $namespaceRoot
2727
*/
28-
public static function directory($directory, $namespaceRoot)
28+
public static function directory($directory = '.', $namespaceRoot = '')
2929
{
3030
spl_autoload_register(function ($className) use ($directory, $namespaceRoot) {
3131
$directory = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $directory);
@@ -41,7 +41,7 @@ public static function directory($directory, $namespaceRoot)
4141
$extensionList = array('php', 'class.php');
4242
foreach ($extensionList as $extension) {
4343
$file = $directory . DIRECTORY_SEPARATOR . $className . '.' . $extension;
44-
if (is_file($file)) {
44+
if (is_file($file) && is_readable($file)) {
4545
require_once($file);
4646
}
4747
}
@@ -50,18 +50,18 @@ public static function directory($directory, $namespaceRoot)
5050

5151
/**
5252
* File
53-
* @param $file
54-
* @param $className
53+
* @param string $file
54+
* @param string $className
5555
*/
56-
public static function file($file, $className)
56+
public static function file($file = '', $className = '')
5757
{
5858
spl_autoload_register(function ($name) use ($file, $className) {
5959
$className = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $className);
6060
$className = trim($className, '/\\');
6161
$name = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $name);
6262
$name = trim($name, '/\\');
6363
if ($className == $name) {
64-
if (is_file($file)) {
64+
if (is_file($file) && is_readable($file)) {
6565
require_once($file);
6666
}
6767
}
@@ -70,12 +70,12 @@ public static function file($file, $className)
7070

7171
/**
7272
* Basic
73-
* @param $file
73+
* @param string $file
7474
* @param bool $isOnce
7575
*/
76-
public static function basic($file, $isOnce = true)
76+
public static function basic($file = '', $isOnce = true)
7777
{
78-
if (is_file($file)) {
78+
if (is_file($file) && is_readable($file)) {
7979
$isOnce ? require_once($file) : require($file);
8080
}
8181
}

0 commit comments

Comments
 (0)