Skip to content

Commit 6e75fbb

Browse files
committed
Merge pull request googleapis#342 from ltrebing/autoload
Use autoloading instead of include path
2 parents 1c8ddc4 + 7c4088e commit 6e75fbb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+94
-150
lines changed

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ For the latest installation and setup instructions, see [the documentation](http
2525
See the examples/ directory for examples of the key client features.
2626
```PHP
2727
<?php
28-
require_once 'Google/Client.php';
29-
require_once 'Google/Service/Books.php';
28+
require_once 'google-api-php-client/autoload.php'; // or wherever autoload.php is located
3029
$client = new Google_Client();
3130
$client->setApplicationName("Client_Library_Examples");
3231
$client->setDeveloperKey("YOUR_APP_KEY");

autoload.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
/*
3+
* Copyright 2014 Google Inc.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
function google_api_php_client_autoload($className) {
19+
$classPath = explode('_', $className);
20+
if ($classPath[0] != 'Google') {
21+
return;
22+
}
23+
if (count($classPath) > 3) {
24+
// Maximum class file path depth in this project is 3.
25+
$classPath = array_slice($className, 0, 3);
26+
}
27+
$filePath = dirname(__FILE__) . '/src/' . implode('/', $classPath) . '.php';
28+
if (file_exists($filePath)) {
29+
require_once($filePath);
30+
}
31+
}
32+
33+
spl_autoload_register('google_api_php_client_autoload');

composer.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
"src/"
1717
]
1818
},
19-
"include-path": ["src/"],
2019
"extra": {
2120
"branch-alias": {
2221
"dev-master": "1.0.x-dev"

examples/appengineauth.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,7 @@
2121
Make an API request authenticated via the
2222
AppIdentity service on AppEngine.
2323
************************************************/
24-
set_include_path("../src/" . PATH_SEPARATOR . get_include_path());
25-
require_once 'Google/Client.php';
26-
require_once 'Google/Auth/AppIdentity.php';
27-
require_once 'Google/Service/Storage.php';
24+
require_once realpath(dirname(__FILE__) . '/../autoload.php');
2825

2926
echo pageHeader("AppIdentity Account Access");
3027

examples/batch.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,7 @@
2222
books API again as an example, but this time we
2323
will batch up two queries into a single call.
2424
************************************************/
25-
set_include_path("../src/" . PATH_SEPARATOR . get_include_path());
26-
require_once 'Google/Client.php';
27-
require_once 'Google/Service/Books.php';
28-
require_once 'Google/Http/Batch.php';
25+
require_once realpath(dirname(__FILE__) . '/../autoload.php');
2926

3027
/************************************************
3128
We create the client and set the simple API

examples/fileupload.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,7 @@
1717
include_once "templates/base.php";
1818
session_start();
1919

20-
set_include_path("../src/" . PATH_SEPARATOR . get_include_path());
21-
require_once 'Google/Client.php';
22-
require_once 'Google/Http/MediaFileUpload.php';
23-
require_once 'Google/Service/Drive.php';
20+
require_once realpath(dirname(__FILE__) . '/../autoload.php');
2421

2522
/************************************************
2623
We'll setup an empty 20MB file to upload.

examples/idtoken.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@
1717
include_once "templates/base.php";
1818
session_start();
1919

20-
set_include_path("../src/" . PATH_SEPARATOR . get_include_path());
21-
require_once 'Google/Client.php';
20+
require_once realpath(dirname(__FILE__) . '/../autoload.php');
2221

2322
/************************************************
2423
ATTENTION: Fill in these values! Make sure

examples/multi-api.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,7 @@
1717
include_once "templates/base.php";
1818
session_start();
1919

20-
set_include_path("../src/" . PATH_SEPARATOR . get_include_path());
21-
require_once 'Google/Client.php';
22-
require_once 'Google/Service/YouTube.php';
23-
require_once 'Google/Service/Drive.php';
20+
require_once realpath(dirname(__FILE__) . '/../autoload.php');
2421

2522
/************************************************
2623
ATTENTION: Fill in these values! Make sure

examples/service-account.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,7 @@
2121
Make an API request authenticated with a service
2222
account.
2323
************************************************/
24-
set_include_path("../src/" . PATH_SEPARATOR . get_include_path());
25-
require_once 'Google/Client.php';
26-
require_once 'Google/Service/Books.php';
24+
require_once realpath(dirname(__FILE__) . '/../autoload.php');
2725

2826
/************************************************
2927
ATTENTION: Fill in these values! You can get

examples/simple-query.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,7 @@
2525
should use our quota, which is higher than the
2626
anonymous quota (which is limited per IP).
2727
************************************************/
28-
set_include_path("../src/" . PATH_SEPARATOR . get_include_path());
29-
require_once 'Google/Client.php';
30-
require_once 'Google/Service/Books.php';
28+
require_once realpath(dirname(__FILE__) . '/../autoload.php');
3129

3230
/************************************************
3331
We create the client and set the simple API

0 commit comments

Comments
 (0)