NEWS: add release notes for the v1.2.0 release
[tweeper.git] / autoload.php
1 <?php
2 /**
3  * @file
4  * Tweeper - some logic to allow tweeper to run with or without composer.
5  *
6  * Copyright (C) 2016  Antonio Ospite <ao2@ao2.it>
7  *
8  * This program is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20  */
21
22 $package_name = 'ao2/tweeper';
23
24 if (file_exists(__DIR__ . '/vendor/autoload.php')) {
25   /*
26    * If "composer install" has been executed, use the composer autoloader.
27    *
28    * Using __DIR__ is OK as long as this file is on the same level of the
29    * project "vendor/" directory (usually the project root directory).
30    */
31   require __DIR__ . '/vendor/autoload.php';
32 }
33 elseif (preg_match('/' . preg_quote('/vendor/' . $package_name, '/') . '$/', __DIR__)) {
34   /*
35    * If running from a "vendor/" directory of another project use the
36    * autoloader of the parent project.
37    *
38    * This covers the case of running from a symlink in ./vendor/bin/ because
39    * __DIR__ contains the *real path* of this file.
40    *
41    * Note that using __DIR__ here and going back two levels is OK under the
42    * assumptions that this file is in the project root directory, and that the
43    * package name has the structure VENDOR/PROJECT_NAME.
44    */
45   require __DIR__ . '/../../autoload.php';
46 }
47 else {
48   /*
49    * Otherwise, run without composer:
50    *
51    *  1. register our own autoloader function for the Tweeper class
52    *
53    * The implementation follows the one suggested in:
54    * http://www.php-fig.org/psr/psr-4/
55    */
56   spl_autoload_register(function ($fully_qualified_class_name) {
57     /* This matches the data defined for the PSR-4 autoloader in composer.json */
58     $namespace_prefix = 'Tweeper\\';
59     $base_directory = 'src/';
60
61     $len = strlen($namespace_prefix);
62     if (strncmp($namespace_prefix, $fully_qualified_class_name, $len) !== 0) {
63       return;
64     }
65
66     $class_relative = substr($fully_qualified_class_name, $len);
67
68     $file_path = $base_directory . str_replace('\\', '/', $class_relative) . '.php';
69
70     require_once $file_path;
71   });
72
73   /*
74    *  2. load the system-wide autoloader from php-symphony-serializer
75    *
76    * This allows to run tweeper without composer, as long as the Symphony
77    * dependencies are available system-wide.
78    *
79    * For example, the Debian package takes care of that.
80    */
81   require_once 'Symfony/Component/Serializer/autoload.php';
82 }