Skip to content

Commit 91c1863

Browse files
committed
0.0.1
1 parent cb41cf1 commit 91c1863

8 files changed

Lines changed: 227 additions & 0 deletions

File tree

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#IDE
2+
/.idea/
3+
4+
#Composer
5+
/vendor/
6+
/composer.lock

README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# CodeMommy AutoloadPHP
2+
3+
[![License](https://poser.pugx.org/CodeMommy/AutoloadPHP/license?format=flat-square)](LICENSE)
4+
[![Download](https://poser.pugx.org/CodeMommy/AutoloadPHP/downloads?format=flat-square)](https://packagist.org/packages/CodeMommy/AutoloadPHP)
5+
[![Stable](https://poser.pugx.org/CodeMommy/AutoloadPHP/version?format=flat-square)](https://packagist.org/packages/CodeMommy/AutoloadPHP)
6+
[![Unstable](https://poser.pugx.org/CodeMommy/AutoloadPHP/v/unstable?format=flat-square)](https://packagist.org/packages/CodeMommy/AutoloadPHP)
7+
[![composer.lock Available](https://poser.pugx.org/CodeMommy/AutoloadPHP/composerlock?format=flat-square)](https://packagist.org/packages/CodeMommy/AutoloadPHP)
8+
9+
10+
> CodeMommy AutoloadPHP is a autoload helper for web development.
11+
12+
Visit [CodeMommy Website](http://www.codemommy.com) or [Packagist](https://packagist.org/packages/CodeMommy/AutoloadPHP) to get more information.
13+
14+
## Authors
15+
16+
| Name | Identity | Social |
17+
| :--- | :------- | :----- |
18+
| Candison November | Creator | [Website](http://www.kandisheng.com) - [GitHub](https://github.com/KanDisheng) |
19+
20+
## More
21+
22+
- [Feedback](https://github.com/CodeMommy/AutoloadPHP/issues)
23+
- [About CodeMommy](https://github.com/CodeMommy/CodeMommy)

composer.json

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"name": "codemommy/autoloadphp",
3+
"version": "0.0.1",
4+
"description": "CodeMommy AutoloadPHP is a autoload helper for web development.",
5+
"keywords": [
6+
"CodeMommy",
7+
"AutoloadPHP",
8+
"Autoload",
9+
"PHP"
10+
],
11+
"license": "Apache 2.0",
12+
"homepage": "http://www.codemommy.com",
13+
"support": {
14+
"issues": "https://github.com/CodeMommy/AutoloadPHP/issues",
15+
"source": "https://github.com/CodeMommy/AutoloadPHP"
16+
},
17+
"authors": [
18+
{
19+
"name": "Candison November",
20+
"email": "kandisheng@163.com",
21+
"homepage": "http://www.kandisheng.com"
22+
}
23+
],
24+
"autoload": {
25+
"psr-4": {
26+
"CodeMommy\\AutoloadPHP\\": "source/"
27+
}
28+
},
29+
"require": {
30+
"php": ">=5.3.0"
31+
}
32+
}

source/Autoload.php

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
<?php
2+
3+
/**
4+
* CodeMommy AutoloadPHP
5+
* @author Candison November <www.kandisheng.com>
6+
*/
7+
8+
namespace CodeMommy\AutoloadPHP;
9+
10+
/**
11+
* Class Autoload
12+
* @package CodeMommy\AutoloadPHP
13+
*/
14+
class Autoload
15+
{
16+
/**
17+
* Remove First Slash
18+
*
19+
* @param $string
20+
*/
21+
private static function removeFirstSlash(&$string)
22+
{
23+
if (substr($string, 0, 1) == '/') {
24+
$string = substr($string, 1);
25+
}
26+
}
27+
28+
/**
29+
* Remove Last Slash
30+
*
31+
* @param $string
32+
*/
33+
private static function removeLastSlash(&$string)
34+
{
35+
if (substr($string, -1) == '/') {
36+
$string = substr($string, 0, -1);
37+
}
38+
}
39+
40+
/**
41+
* Replace Slash
42+
*
43+
* @param $string
44+
*/
45+
private static function replaceSlash(&$string)
46+
{
47+
$string = str_replace('\\', '/', $string);
48+
}
49+
50+
/**
51+
* Load
52+
*
53+
* @param $path
54+
* @param $namespaceRoot
55+
*/
56+
public static function load($path, $namespaceRoot)
57+
{
58+
spl_autoload_register(function ($className) use ($path, $namespaceRoot) {
59+
self::replaceSlash($path);
60+
self::removeLastSlash($path);
61+
self::replaceSlash($namespaceRoot);
62+
self::removeFirstSlash($namespaceRoot);
63+
self::removeLastSlash($namespaceRoot);
64+
self::replaceSlash($className);
65+
self::removeFirstSlash($className);
66+
self::removeLastSlash($className);
67+
if (substr($className, 0, strlen($namespaceRoot)) == $namespaceRoot) {
68+
$className = substr($className, strlen($namespaceRoot));
69+
self::removeFirstSlash($className);
70+
}
71+
$file = $path . '/' . $className . '.php';
72+
if (is_file($file)) {
73+
require_once($file);
74+
}
75+
});
76+
}
77+
78+
/**
79+
* File
80+
*
81+
* @param $path
82+
* @param $name
83+
*/
84+
public static function file($path, $name)
85+
{
86+
spl_autoload_register(function ($className) use ($path, $name) {
87+
self::replaceSlash($name);
88+
self::removeFirstSlash($name);
89+
self::removeLastSlash($name);
90+
self::replaceSlash($className);
91+
self::removeFirstSlash($className);
92+
self::removeLastSlash($className);
93+
if ($name == $className) {
94+
if (is_file($path)) {
95+
require_once($path);
96+
}
97+
}
98+
});
99+
}
100+
}

test/NamespaceOne/ClassOne.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
/**
4+
* @author Candison November <www.kandisheng.com>
5+
*/
6+
7+
namespace NamespaceOne;
8+
9+
class ClassOne
10+
{
11+
public static function show()
12+
{
13+
echo 'ClassOne';
14+
}
15+
}

test/NamespaceThree/ClassThree.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
/**
4+
* @author Candison November <www.kandisheng.com>
5+
*/
6+
7+
namespace Root\NamespaceThree;
8+
9+
class ClassThree
10+
{
11+
public static function show()
12+
{
13+
echo 'ClassThree';
14+
}
15+
}

test/NamespaceTwo/ClassTwo.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
/**
4+
* @author Candison November <www.kandisheng.com>
5+
*/
6+
7+
namespace Root\NamespaceTwo;
8+
9+
class ClassTwo
10+
{
11+
public static function show()
12+
{
13+
echo 'ClassTwo';
14+
}
15+
}

test/autoload.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
/**
4+
* @author Candison November <www.kandisheng.com>
5+
*/
6+
7+
require_once(__DIR__ . '/../source/Autoload.php');
8+
9+
use CodeMommy\AutoloadPHP\Autoload;
10+
11+
Autoload::load(__DIR__, '');
12+
Autoload::load(__DIR__, 'Root');
13+
Autoload::file(__DIR__ . '/NamespaceThree/ClassThree.php', 'Root\NamespaceThree\ClassThree');
14+
15+
use NamespaceOne\ClassOne;
16+
use Root\NamespaceTwo\ClassTwo;
17+
use Root\NamespaceThree\ClassThree;
18+
19+
ClassOne::show();
20+
ClassTwo::show();
21+
ClassThree::show();

0 commit comments

Comments
 (0)