Skip to content

Commit a461824

Browse files
committed
Add .travis.yml
1 parent b25e05c commit a461824

File tree

7 files changed

+109
-70
lines changed

7 files changed

+109
-70
lines changed

.editorconfig

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
root=true
2+
3+
[*]
4+
charset=utf-8
5+
end_of_line=crlf
6+
indent_style=space
7+
indent_size=2
8+
insert_final_newline=false
9+
trim_trailing_whitespace=true
10+
11+
[*.php]
12+
indent_size=4

.travis.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
language: php
2+
php:
3+
- 5.3
4+
- 5.4
5+
- 5.5
6+
- 5.6
7+
- 7.0
8+
- 7.1
9+
before_script:
10+
- composer install
11+
script:
12+
- vendor/bin/phpunit -v

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "codemommy/autoloadphp",
3-
"version": "0.0.1",
3+
"version": "0.0.2",
44
"description": "Automatic load namespaces, classes and files",
55
"keywords": [
66
"CodeMommy",

phpunit.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<phpunit>
1+
<phpunit bootstrap="vendor/autoload.php">
22
<testsuites>
33
<testsuite name="CodeMommy AutoloadPHP">
44
<directory>test</directory>

source/Autoload.php

Lines changed: 27 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -14,61 +14,27 @@
1414
class Autoload
1515
{
1616
/**
17-
* Remove First Slash
17+
* Directory
1818
*
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
19+
* @param $directory
5420
* @param $namespaceRoot
5521
*/
56-
public static function load($path, $namespaceRoot)
22+
public static function directory($directory, $namespaceRoot)
5723
{
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);
24+
spl_autoload_register(function ($className) use ($directory, $namespaceRoot) {
25+
Tool::replaceSlash($directory);
26+
Tool::removeLastSlash($directory);
27+
Tool::replaceSlash($namespaceRoot);
28+
Tool::removeFirstSlash($namespaceRoot);
29+
Tool::removeLastSlash($namespaceRoot);
30+
Tool::replaceSlash($className);
31+
Tool::removeFirstSlash($className);
32+
Tool::removeLastSlash($className);
6733
if (substr($className, 0, strlen($namespaceRoot)) == $namespaceRoot) {
6834
$className = substr($className, strlen($namespaceRoot));
69-
self::removeFirstSlash($className);
35+
Tool::removeFirstSlash($className);
7036
}
71-
$file = $path . '/' . $className . '.php';
37+
$file = $directory . '/' . $className . '.php';
7238
if (is_file($file)) {
7339
require_once($file);
7440
}
@@ -78,21 +44,21 @@ public static function load($path, $namespaceRoot)
7844
/**
7945
* File
8046
*
81-
* @param $path
82-
* @param $name
47+
* @param $file
48+
* @param $className
8349
*/
84-
public static function file($path, $name)
50+
public static function file($file, $className)
8551
{
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);
52+
spl_autoload_register(function ($name) use ($file, $className) {
53+
Tool::replaceSlash($className);
54+
Tool::removeFirstSlash($className);
55+
Tool::removeLastSlash($className);
56+
Tool::replaceSlash($name);
57+
Tool::removeFirstSlash($name);
58+
Tool::removeLastSlash($name);
59+
if ($className == $name) {
60+
if (is_file($file)) {
61+
require_once($file);
9662
}
9763
}
9864
});

source/Tool.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
/**
4+
* CodeMommy AutoloadPHP
5+
* @author Candison November <www.kandisheng.com>
6+
*/
7+
8+
namespace CodeMommy\AutoloadPHP;
9+
10+
/**
11+
* Class Tool
12+
* @package CodeMommy\AutoloadPHP
13+
*/
14+
class Tool
15+
{
16+
/**
17+
* Remove First Slash
18+
*
19+
* @param $string
20+
*/
21+
public 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+
public 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+
public static function replaceSlash(&$string)
46+
{
47+
$string = str_replace('\\', '/', $string);
48+
}
49+
}

test/AutoloadTest.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
declare(strict_types=1);
88

9-
require_once(__DIR__ . '/../vendor/autoload.php');
9+
namespace Test;
1010

1111
use PHPUnit\Framework\TestCase;
1212
use CodeMommy\AutoloadPHP\Autoload;
@@ -17,22 +17,22 @@
1717
class AutoloadTest extends TestCase
1818
{
1919
/**
20-
* Test Load
20+
* Test Directory
2121
* @return void
2222
*/
23-
public function testLoad1()
23+
public function testDirectoryClassOne()
2424
{
25-
Autoload::load(__DIR__, '');
25+
Autoload::directory(__DIR__, '');
2626
$this->assertEquals(ClassOne::show(), 'ClassOne');
2727
}
2828

2929
/**
30-
* Test Load
30+
* Test Directory
3131
* @return void
3232
*/
33-
public function testLoad2()
33+
public function testDirectoryClassTwo()
3434
{
35-
Autoload::load(__DIR__, 'Root');
35+
Autoload::directory(__DIR__, 'Root');
3636
$this->assertEquals(ClassTwo::show(), 'ClassTwo');
3737
}
3838

0 commit comments

Comments
 (0)