4 * Tweeper - some logic to allow tweeper to run with or without composer.
6 * Copyright (C) 2016 Antonio Ospite <ao2@ao2.it>
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.
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.
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/>.
22 $package_name = 'ao2/tweeper';
24 if (file_exists(__DIR__ . '/vendor/autoload.php')) {
26 * If "composer install" has been executed, use the composer autoloader.
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).
31 require __DIR__ . '/vendor/autoload.php';
33 elseif (preg_match('/' . preg_quote('/vendor/' . $package_name, '/') . '$/', __DIR__)) {
35 * If running from a "vendor/" directory of another project use the
36 * autoloader of the parent project.
38 * This covers the case of running from a symlink in ./vendor/bin/ because
39 * __DIR__ contains the *real path* of this file.
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.
45 require __DIR__ . '/../../autoload.php';
49 * Otherwise, run without composer:
51 * 1. register our own autoloader function for the Tweeper class
53 * The implementation follows the one suggested in:
54 * http://www.php-fig.org/psr/psr-4/
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/';
61 $len = strlen($namespace_prefix);
62 if (strncmp($namespace_prefix, $fully_qualified_class_name, $len) !== 0) {
66 $class_relative = substr($fully_qualified_class_name, $len);
68 $file_path = $base_directory . str_replace('\\', '/', $class_relative) . '.php';
70 require_once $file_path;
74 * 2. load the system-wide autoloader from php-symphony-serializer
76 * This allows to run tweeper without composer, provided that the
77 * dependencies are available system-wide.
79 * For example, the Debian package takes care of that.
81 require_once 'Symfony/Component/Serializer/autoload.php';