Skip to content

Commit c3843af

Browse files
author
Fosco Marotto
committed
Merge pull request parse-community#2 from niraj-shah/master
Added autoload for those that don't want to use Compose
2 parents cb53890 + f2fcdd5 commit c3843af

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,17 @@ and then require it from your PHP script:
2525
require 'vendor/autoload.php';
2626
```
2727

28+
Alternative Method
29+
------------------
30+
31+
If you don't want to use Composer, you can include the ```autoload.php```
32+
file in your code to automatically load the Parse SDK classes.
33+
34+
```php
35+
require 'autoload.php';
36+
```
37+
38+
2839
Usage
2940
-----
3041

autoload.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
/**
4+
* You only need this file if you are not using composer.
5+
*/
6+
7+
if (version_compare(PHP_VERSION, '5.4.0', '<')) {
8+
throw new Exception('The Parse SDK requires PHP version 5.4 or higher.');
9+
}
10+
11+
/**
12+
* Register the autoloader for the Parse SDK
13+
* Based off the official PSR-4 autoloader example found here:
14+
* https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-4-autoloader-examples.md
15+
*
16+
* @param string $class The fully-qualified class name.
17+
* @return void
18+
*/
19+
spl_autoload_register(function ($class)
20+
{
21+
// Parse class prefix
22+
$prefix = 'Parse\\';
23+
24+
// base directory for the namespace prefix
25+
$base_dir = defined('PARSE_SDK_DIR') ? PARSE_SDK_DIR : __DIR__ . '/src/Parse/';
26+
27+
// does the class use the namespace prefix?
28+
$len = strlen( $prefix );
29+
if ( strncmp($prefix, $class, $len) !== 0 ) {
30+
// no, move to the next registered autoloader
31+
return;
32+
}
33+
34+
// get the relative class name
35+
$relative_class = substr( $class, $len );
36+
37+
// replace the namespace prefix with the base directory, replace namespace
38+
// separators with directory separators in the relative class name, append
39+
// with .php
40+
$file = $base_dir . str_replace( '\\', '/', $relative_class ) . '.php';
41+
42+
// echo $relative_class . '<br/>';
43+
44+
// if the file exists, require it
45+
if ( file_exists( $file ) ) {
46+
require $file;
47+
}
48+
});

0 commit comments

Comments
 (0)