Skip to content

Commit

Permalink
Add autoloader.php as Composer alternative
Browse files Browse the repository at this point in the history
  • Loading branch information
meyfa committed Sep 20, 2016
1 parent e1bbf3c commit f126b27
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,38 @@ In case you decide to contribute, please honor these things:



---



## Installation

### Composer (recommended)

This package is available through Composer/Packagist:

```
$ composer require jangobrick/php-svg
```

### Manual

[Download](https://github.com/JangoBrick/php-svg/zipball/master) this repo,
or the [latest release](https://github.com/JangoBrick/php-svg/releases),
and put it somewhere in your project. Then do:

```php
<?php
require_once __DIR__.'/<path_where_you_put_it>/autoloader.php';
```

The rest works exactly the same as with Composer, just without things like nice
version management.



---



## Getting Started
Expand Down
34 changes: 34 additions & 0 deletions autoloader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

// PSR-4 autoloader implementation, taken from:
// https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-4-autoloader-examples.md
// which is licensed under MIT.

spl_autoload_register(function ($class) {

// project-specific namespace prefix
$prefix = 'JangoBrick\\SVG\\';

// base directory for the namespace prefix
$base_dir = __DIR__ . '/src/';

// does the class use the namespace prefix?
$len = strlen($prefix);
if (strncmp($prefix, $class, $len) !== 0) {
// no, move to the next registered autoloader
return;
}

// get the relative class name
$relative_class = substr($class, $len);

// replace the namespace prefix with the base directory, replace namespace
// separators with directory separators in the relative class name, append
// with .php
$file = $base_dir . str_replace('\\', '/', $relative_class) . '.php';

// if the file exists, require it
if (file_exists($file)) {
require $file;
}
});

0 comments on commit f126b27

Please sign in to comment.