From efd6e189034c7bf376c669349a227ad3bb4f0f67 Mon Sep 17 00:00:00 2001 From: Antonio Ospite Date: Sat, 5 Nov 2016 17:55:56 +0100 Subject: [PATCH] tweeper: allow to run tweeper either with or without composer --- autoload.php | 82 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/Tweeper.php | 2 -- tweeper.php | 2 +- 3 files changed, 83 insertions(+), 3 deletions(-) create mode 100644 autoload.php diff --git a/autoload.php b/autoload.php new file mode 100644 index 0000000..1e11adf --- /dev/null +++ b/autoload.php @@ -0,0 +1,82 @@ + + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +$package_name = 'ao2/tweeper'; + +if (file_exists(__DIR__ . '/vendor/autoload.php')) { + /* + * If "composer install" has been executed, use the composer autoloader. + * + * Using __DIR__ is OK as long as this file is on the same level of the + * project "vendor/" directory (usually the project root directory). + */ + require __DIR__ . '/vendor/autoload.php'; +} +elseif (preg_match('/' . preg_quote('/vendor/' . $package_name, '/') . '$/', __DIR__)) { + /* + * If running from a "vendor/" directory of another project use the + * autoloader of the parent project. + * + * This covers the case of running from a symlink in ./vendor/bin/ because + * __DIR__ contains the *real path* of this file. + * + * Note that using __DIR__ here and going back two levels is OK under the + * assumptions that this file is in the project root directory, and that the + * package name has the structure VENDOR/PROJECT_NAME. + */ + require __DIR__ . '/../../autoload.php'; +} +else { + /* + * Otherwise, run without composer: + * + * 1. register our own autoloader function for the Tweeper class + * + * The implementation follows the one suggested in: + * http://www.php-fig.org/psr/psr-4/ + */ + spl_autoload_register(function ($fully_qualified_class_name) { + /* This matches the data defined for the PSR-4 autoloader in composer.json */ + $namespace_prefix = 'Tweeper\\'; + $base_directory = 'src/'; + + $len = strlen($namespace_prefix); + if (strncmp($namespace_prefix, $fully_qualified_class_name, $len) !== 0) { + return; + } + + $class_relative = substr($fully_qualified_class_name, $len); + + $file_path = $base_directory . str_replace('\\', '/', $class_relative) . '.php'; + + require_once $file_path; + }); + + /* + * 2. load the system-wide autoloader from php-symphony-serializer + * + * This allows to run tweeper without composer, provided that the + * dependencies are available system-wide. + * + * For example, the Debian package takes care of that. + */ + require_once 'Symfony/Component/Serializer/autoload.php'; +} diff --git a/src/Tweeper.php b/src/Tweeper.php index 73cbe81..944b37c 100644 --- a/src/Tweeper.php +++ b/src/Tweeper.php @@ -25,8 +25,6 @@ namespace Tweeper; use DOMDocument; use XSLTProcessor; -require_once 'Symfony/Component/Serializer/autoload.php'; - use Symfony\Component\Serializer\Serializer; use Symfony\Component\Serializer\Encoder\XmlEncoder; use Symfony\Component\Serializer\Normalizer\ObjectNormalizer; diff --git a/tweeper.php b/tweeper.php index ba8b1d7..c9040b0 100644 --- a/tweeper.php +++ b/tweeper.php @@ -19,7 +19,7 @@ * along with this program. If not, see . */ -require_once 'src/Tweeper.php'; +require_once 'autoload.php'; use Tweeper\Tweeper; -- 2.1.4