From 655a0afa58f1fe9b7fd0991aca95c2ec0f678279 Mon Sep 17 00:00:00 2001 From: Justin Foell Date: Fri, 23 Feb 2018 12:14:05 -0600 Subject: [PATCH] Added composer for autoloader and moved files to conform to PSR-0 --- composer.json | 12 + composer.lock | 19 + lib/{Strava.class.php => WPStrava.php} | 12 - lib/{API.class.php => WPStrava/API.php} | 0 .../Activity.php} | 0 .../ActivityShortcode.php} | 0 .../LatestActivities.php} | 2 +- .../LatestActivitiesShortcode.php} | 4 +- .../LatestActivitiesWidget.php} | 4 +- .../LatestMapWidget.php} | 0 .../RouteShortcode.php} | 0 lib/{Routes.class.php => WPStrava/Routes.php} | 0 lib/{SOM.class.php => WPStrava/SOM.php} | 2 - .../SOMEnglish.php} | 0 .../SOMMetric.php} | 0 .../Settings.php} | 1 - .../StaticMap.php} | 0 vendor/autoload.php | 7 + vendor/composer/ClassLoader.php | 445 ++++++++++++++++++ vendor/composer/LICENSE | 21 + vendor/composer/autoload_classmap.php | 9 + vendor/composer/autoload_namespaces.php | 10 + vendor/composer/autoload_psr4.php | 9 + vendor/composer/autoload_real.php | 52 ++ vendor/composer/autoload_static.php | 26 + vendor/composer/installed.json | 1 + wp-strava.php | 9 +- 27 files changed, 619 insertions(+), 26 deletions(-) create mode 100644 composer.json create mode 100644 composer.lock rename lib/{Strava.class.php => WPStrava.php} (71%) rename lib/{API.class.php => WPStrava/API.php} (100%) rename lib/{Activity.class.php => WPStrava/Activity.php} (100%) rename lib/{ActivityShortcode.class.php => WPStrava/ActivityShortcode.php} (100%) rename lib/{LatestActivities.class.php => WPStrava/LatestActivities.php} (98%) rename lib/{LatestActivitiesShortcode.class.php => WPStrava/LatestActivitiesShortcode.php} (82%) rename lib/{LatestActivitiesWidget.class.php => WPStrava/LatestActivitiesWidget.php} (96%) rename lib/{LatestMapWidget.class.php => WPStrava/LatestMapWidget.php} (100%) rename lib/{RouteShortcode.class.php => WPStrava/RouteShortcode.php} (100%) rename lib/{Routes.class.php => WPStrava/Routes.php} (100%) rename lib/{SOM.class.php => WPStrava/SOM.php} (88%) rename lib/{SOMEnglish.class.php => WPStrava/SOMEnglish.php} (100%) rename lib/{SOMMetric.class.php => WPStrava/SOMMetric.php} (100%) rename lib/{Settings.class.php => WPStrava/Settings.php} (99%) rename lib/{StaticMap.class.php => WPStrava/StaticMap.php} (100%) create mode 100644 vendor/autoload.php create mode 100644 vendor/composer/ClassLoader.php create mode 100644 vendor/composer/LICENSE create mode 100644 vendor/composer/autoload_classmap.php create mode 100644 vendor/composer/autoload_namespaces.php create mode 100644 vendor/composer/autoload_psr4.php create mode 100644 vendor/composer/autoload_real.php create mode 100644 vendor/composer/autoload_static.php create mode 100644 vendor/composer/installed.json diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..a304514 --- /dev/null +++ b/composer.json @@ -0,0 +1,12 @@ +{ + "name": "wpackagist-plugin/wp-strava", + "description": "Show your strava.com activity on your WordPress site.", + "require":{ + "php": ">=5.2.4" + }, + "autoload": { + "psr-0": { + "WPStrava_": "lib/" + } + } +} \ No newline at end of file diff --git a/composer.lock b/composer.lock new file mode 100644 index 0000000..23603e5 --- /dev/null +++ b/composer.lock @@ -0,0 +1,19 @@ +{ + "_readme": [ + "This file locks the dependencies of your project to a known state", + "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", + "This file is @generated automatically" + ], + "content-hash": "724eef3de7c6e2ee812ac6b4f92edb80", + "packages": [], + "packages-dev": [], + "aliases": [], + "minimum-stability": "stable", + "stability-flags": [], + "prefer-stable": false, + "prefer-lowest": false, + "platform": { + "php": ">=5.2.4" + }, + "platform-dev": [] +} diff --git a/lib/Strava.class.php b/lib/WPStrava.php similarity index 71% rename from lib/Strava.class.php rename to lib/WPStrava.php index 45d4844..e323aca 100644 --- a/lib/Strava.class.php +++ b/lib/WPStrava.php @@ -1,13 +1,5 @@ api[ $token ] ) ) { - require_once WPSTRAVA_PLUGIN_DIR . 'lib/API.class.php'; $this->api[ $token ] = new WPStrava_API( $token ); } @@ -77,7 +68,6 @@ class WPStrava { public function get_routes() { if ( ! $this->routes ) { - require_once WPSTRAVA_PLUGIN_DIR . 'lib/Routes.class.php'; $this->routes = new WPStrava_Routes(); } return $this->routes; @@ -89,8 +79,6 @@ class WPStrava { } public function register_widgets() { - require_once WPSTRAVA_PLUGIN_DIR . 'lib/LatestActivitiesWidget.class.php'; - require_once WPSTRAVA_PLUGIN_DIR . 'lib/LatestMapWidget.class.php'; register_widget( 'WPStrava_LatestActivitiesWidget' ); register_widget( 'WPStrava_LatestMapWidget' ); } diff --git a/lib/API.class.php b/lib/WPStrava/API.php similarity index 100% rename from lib/API.class.php rename to lib/WPStrava/API.php diff --git a/lib/Activity.class.php b/lib/WPStrava/Activity.php similarity index 100% rename from lib/Activity.class.php rename to lib/WPStrava/Activity.php diff --git a/lib/ActivityShortcode.class.php b/lib/WPStrava/ActivityShortcode.php similarity index 100% rename from lib/ActivityShortcode.class.php rename to lib/WPStrava/ActivityShortcode.php diff --git a/lib/LatestActivities.class.php b/lib/WPStrava/LatestActivities.php similarity index 98% rename from lib/LatestActivities.class.php rename to lib/WPStrava/LatestActivities.php index 83972d1..683ab97 100644 --- a/lib/LatestActivities.class.php +++ b/lib/WPStrava/LatestActivities.php @@ -1,6 +1,6 @@ settings->som; if ( 'english' === $som ) { - require_once WPSTRAVA_PLUGIN_DIR . 'lib/SOMEnglish.class.php'; return new WPStrava_SOMEnglish(); } // Default to metric. - require_once WPSTRAVA_PLUGIN_DIR . 'lib/SOMMetric.class.php'; return new WPStrava_SOMMetric(); } diff --git a/lib/SOMEnglish.class.php b/lib/WPStrava/SOMEnglish.php similarity index 100% rename from lib/SOMEnglish.class.php rename to lib/WPStrava/SOMEnglish.php diff --git a/lib/SOMMetric.class.php b/lib/WPStrava/SOMMetric.php similarity index 100% rename from lib/SOMMetric.class.php rename to lib/WPStrava/SOMMetric.php diff --git a/lib/Settings.class.php b/lib/WPStrava/Settings.php similarity index 99% rename from lib/Settings.class.php rename to lib/WPStrava/Settings.php index 190e23a..8e42e7a 100644 --- a/lib/Settings.class.php +++ b/lib/WPStrava/Settings.php @@ -289,7 +289,6 @@ class WPStrava_Settings { delete_option( 'strava_client_secret' ); if ( $client_id && $client_secret ) { - require_once WPSTRAVA_PLUGIN_DIR . 'lib/API.class.php'; $api = new WPStrava_API(); $data = array( 'client_id' => $client_id, diff --git a/lib/StaticMap.class.php b/lib/WPStrava/StaticMap.php similarity index 100% rename from lib/StaticMap.class.php rename to lib/WPStrava/StaticMap.php diff --git a/vendor/autoload.php b/vendor/autoload.php new file mode 100644 index 0000000..2375b1e --- /dev/null +++ b/vendor/autoload.php @@ -0,0 +1,7 @@ + + * Jordi Boggiano + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Composer\Autoload; + +/** + * ClassLoader implements a PSR-0, PSR-4 and classmap class loader. + * + * $loader = new \Composer\Autoload\ClassLoader(); + * + * // register classes with namespaces + * $loader->add('Symfony\Component', __DIR__.'/component'); + * $loader->add('Symfony', __DIR__.'/framework'); + * + * // activate the autoloader + * $loader->register(); + * + * // to enable searching the include path (eg. for PEAR packages) + * $loader->setUseIncludePath(true); + * + * In this example, if you try to use a class in the Symfony\Component + * namespace or one of its children (Symfony\Component\Console for instance), + * the autoloader will first look for the class under the component/ + * directory, and it will then fallback to the framework/ directory if not + * found before giving up. + * + * This class is loosely based on the Symfony UniversalClassLoader. + * + * @author Fabien Potencier + * @author Jordi Boggiano + * @see http://www.php-fig.org/psr/psr-0/ + * @see http://www.php-fig.org/psr/psr-4/ + */ +class ClassLoader +{ + // PSR-4 + private $prefixLengthsPsr4 = array(); + private $prefixDirsPsr4 = array(); + private $fallbackDirsPsr4 = array(); + + // PSR-0 + private $prefixesPsr0 = array(); + private $fallbackDirsPsr0 = array(); + + private $useIncludePath = false; + private $classMap = array(); + private $classMapAuthoritative = false; + private $missingClasses = array(); + private $apcuPrefix; + + public function getPrefixes() + { + if (!empty($this->prefixesPsr0)) { + return call_user_func_array('array_merge', $this->prefixesPsr0); + } + + return array(); + } + + public function getPrefixesPsr4() + { + return $this->prefixDirsPsr4; + } + + public function getFallbackDirs() + { + return $this->fallbackDirsPsr0; + } + + public function getFallbackDirsPsr4() + { + return $this->fallbackDirsPsr4; + } + + public function getClassMap() + { + return $this->classMap; + } + + /** + * @param array $classMap Class to filename map + */ + public function addClassMap(array $classMap) + { + if ($this->classMap) { + $this->classMap = array_merge($this->classMap, $classMap); + } else { + $this->classMap = $classMap; + } + } + + /** + * Registers a set of PSR-0 directories for a given prefix, either + * appending or prepending to the ones previously set for this prefix. + * + * @param string $prefix The prefix + * @param array|string $paths The PSR-0 root directories + * @param bool $prepend Whether to prepend the directories + */ + public function add($prefix, $paths, $prepend = false) + { + if (!$prefix) { + if ($prepend) { + $this->fallbackDirsPsr0 = array_merge( + (array) $paths, + $this->fallbackDirsPsr0 + ); + } else { + $this->fallbackDirsPsr0 = array_merge( + $this->fallbackDirsPsr0, + (array) $paths + ); + } + + return; + } + + $first = $prefix[0]; + if (!isset($this->prefixesPsr0[$first][$prefix])) { + $this->prefixesPsr0[$first][$prefix] = (array) $paths; + + return; + } + if ($prepend) { + $this->prefixesPsr0[$first][$prefix] = array_merge( + (array) $paths, + $this->prefixesPsr0[$first][$prefix] + ); + } else { + $this->prefixesPsr0[$first][$prefix] = array_merge( + $this->prefixesPsr0[$first][$prefix], + (array) $paths + ); + } + } + + /** + * Registers a set of PSR-4 directories for a given namespace, either + * appending or prepending to the ones previously set for this namespace. + * + * @param string $prefix The prefix/namespace, with trailing '\\' + * @param array|string $paths The PSR-4 base directories + * @param bool $prepend Whether to prepend the directories + * + * @throws \InvalidArgumentException + */ + public function addPsr4($prefix, $paths, $prepend = false) + { + if (!$prefix) { + // Register directories for the root namespace. + if ($prepend) { + $this->fallbackDirsPsr4 = array_merge( + (array) $paths, + $this->fallbackDirsPsr4 + ); + } else { + $this->fallbackDirsPsr4 = array_merge( + $this->fallbackDirsPsr4, + (array) $paths + ); + } + } elseif (!isset($this->prefixDirsPsr4[$prefix])) { + // Register directories for a new namespace. + $length = strlen($prefix); + if ('\\' !== $prefix[$length - 1]) { + throw new \InvalidArgumentException("A non-empty PSR-4 prefix must end with a namespace separator."); + } + $this->prefixLengthsPsr4[$prefix[0]][$prefix] = $length; + $this->prefixDirsPsr4[$prefix] = (array) $paths; + } elseif ($prepend) { + // Prepend directories for an already registered namespace. + $this->prefixDirsPsr4[$prefix] = array_merge( + (array) $paths, + $this->prefixDirsPsr4[$prefix] + ); + } else { + // Append directories for an already registered namespace. + $this->prefixDirsPsr4[$prefix] = array_merge( + $this->prefixDirsPsr4[$prefix], + (array) $paths + ); + } + } + + /** + * Registers a set of PSR-0 directories for a given prefix, + * replacing any others previously set for this prefix. + * + * @param string $prefix The prefix + * @param array|string $paths The PSR-0 base directories + */ + public function set($prefix, $paths) + { + if (!$prefix) { + $this->fallbackDirsPsr0 = (array) $paths; + } else { + $this->prefixesPsr0[$prefix[0]][$prefix] = (array) $paths; + } + } + + /** + * Registers a set of PSR-4 directories for a given namespace, + * replacing any others previously set for this namespace. + * + * @param string $prefix The prefix/namespace, with trailing '\\' + * @param array|string $paths The PSR-4 base directories + * + * @throws \InvalidArgumentException + */ + public function setPsr4($prefix, $paths) + { + if (!$prefix) { + $this->fallbackDirsPsr4 = (array) $paths; + } else { + $length = strlen($prefix); + if ('\\' !== $prefix[$length - 1]) { + throw new \InvalidArgumentException("A non-empty PSR-4 prefix must end with a namespace separator."); + } + $this->prefixLengthsPsr4[$prefix[0]][$prefix] = $length; + $this->prefixDirsPsr4[$prefix] = (array) $paths; + } + } + + /** + * Turns on searching the include path for class files. + * + * @param bool $useIncludePath + */ + public function setUseIncludePath($useIncludePath) + { + $this->useIncludePath = $useIncludePath; + } + + /** + * Can be used to check if the autoloader uses the include path to check + * for classes. + * + * @return bool + */ + public function getUseIncludePath() + { + return $this->useIncludePath; + } + + /** + * Turns off searching the prefix and fallback directories for classes + * that have not been registered with the class map. + * + * @param bool $classMapAuthoritative + */ + public function setClassMapAuthoritative($classMapAuthoritative) + { + $this->classMapAuthoritative = $classMapAuthoritative; + } + + /** + * Should class lookup fail if not found in the current class map? + * + * @return bool + */ + public function isClassMapAuthoritative() + { + return $this->classMapAuthoritative; + } + + /** + * APCu prefix to use to cache found/not-found classes, if the extension is enabled. + * + * @param string|null $apcuPrefix + */ + public function setApcuPrefix($apcuPrefix) + { + $this->apcuPrefix = function_exists('apcu_fetch') && ini_get('apc.enabled') ? $apcuPrefix : null; + } + + /** + * The APCu prefix in use, or null if APCu caching is not enabled. + * + * @return string|null + */ + public function getApcuPrefix() + { + return $this->apcuPrefix; + } + + /** + * Registers this instance as an autoloader. + * + * @param bool $prepend Whether to prepend the autoloader or not + */ + public function register($prepend = false) + { + spl_autoload_register(array($this, 'loadClass'), true, $prepend); + } + + /** + * Unregisters this instance as an autoloader. + */ + public function unregister() + { + spl_autoload_unregister(array($this, 'loadClass')); + } + + /** + * Loads the given class or interface. + * + * @param string $class The name of the class + * @return bool|null True if loaded, null otherwise + */ + public function loadClass($class) + { + if ($file = $this->findFile($class)) { + includeFile($file); + + return true; + } + } + + /** + * Finds the path to the file where the class is defined. + * + * @param string $class The name of the class + * + * @return string|false The path if found, false otherwise + */ + public function findFile($class) + { + // class map lookup + if (isset($this->classMap[$class])) { + return $this->classMap[$class]; + } + if ($this->classMapAuthoritative || isset($this->missingClasses[$class])) { + return false; + } + if (null !== $this->apcuPrefix) { + $file = apcu_fetch($this->apcuPrefix.$class, $hit); + if ($hit) { + return $file; + } + } + + $file = $this->findFileWithExtension($class, '.php'); + + // Search for Hack files if we are running on HHVM + if (false === $file && defined('HHVM_VERSION')) { + $file = $this->findFileWithExtension($class, '.hh'); + } + + if (null !== $this->apcuPrefix) { + apcu_add($this->apcuPrefix.$class, $file); + } + + if (false === $file) { + // Remember that this class does not exist. + $this->missingClasses[$class] = true; + } + + return $file; + } + + private function findFileWithExtension($class, $ext) + { + // PSR-4 lookup + $logicalPathPsr4 = strtr($class, '\\', DIRECTORY_SEPARATOR) . $ext; + + $first = $class[0]; + if (isset($this->prefixLengthsPsr4[$first])) { + $subPath = $class; + while (false !== $lastPos = strrpos($subPath, '\\')) { + $subPath = substr($subPath, 0, $lastPos); + $search = $subPath.'\\'; + if (isset($this->prefixDirsPsr4[$search])) { + foreach ($this->prefixDirsPsr4[$search] as $dir) { + $length = $this->prefixLengthsPsr4[$first][$search]; + if (file_exists($file = $dir . DIRECTORY_SEPARATOR . substr($logicalPathPsr4, $length))) { + return $file; + } + } + } + } + } + + // PSR-4 fallback dirs + foreach ($this->fallbackDirsPsr4 as $dir) { + if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr4)) { + return $file; + } + } + + // PSR-0 lookup + if (false !== $pos = strrpos($class, '\\')) { + // namespaced class name + $logicalPathPsr0 = substr($logicalPathPsr4, 0, $pos + 1) + . strtr(substr($logicalPathPsr4, $pos + 1), '_', DIRECTORY_SEPARATOR); + } else { + // PEAR-like class name + $logicalPathPsr0 = strtr($class, '_', DIRECTORY_SEPARATOR) . $ext; + } + + if (isset($this->prefixesPsr0[$first])) { + foreach ($this->prefixesPsr0[$first] as $prefix => $dirs) { + if (0 === strpos($class, $prefix)) { + foreach ($dirs as $dir) { + if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr0)) { + return $file; + } + } + } + } + } + + // PSR-0 fallback dirs + foreach ($this->fallbackDirsPsr0 as $dir) { + if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr0)) { + return $file; + } + } + + // PSR-0 include paths. + if ($this->useIncludePath && $file = stream_resolve_include_path($logicalPathPsr0)) { + return $file; + } + + return false; + } +} + +/** + * Scope isolated include. + * + * Prevents access to $this/self from included files. + */ +function includeFile($file) +{ + include $file; +} diff --git a/vendor/composer/LICENSE b/vendor/composer/LICENSE new file mode 100644 index 0000000..f27399a --- /dev/null +++ b/vendor/composer/LICENSE @@ -0,0 +1,21 @@ + +Copyright (c) Nils Adermann, Jordi Boggiano + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is furnished +to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. + diff --git a/vendor/composer/autoload_classmap.php b/vendor/composer/autoload_classmap.php new file mode 100644 index 0000000..7a91153 --- /dev/null +++ b/vendor/composer/autoload_classmap.php @@ -0,0 +1,9 @@ + array($baseDir . '/lib'), +); diff --git a/vendor/composer/autoload_psr4.php b/vendor/composer/autoload_psr4.php new file mode 100644 index 0000000..b265c64 --- /dev/null +++ b/vendor/composer/autoload_psr4.php @@ -0,0 +1,9 @@ += 50600 && !defined('HHVM_VERSION') && (!function_exists('zend_loader_file_encoded') || !zend_loader_file_encoded()); + if ($useStaticLoader) { + require_once __DIR__ . '/autoload_static.php'; + + call_user_func(\Composer\Autoload\ComposerStaticInit270e8bb56ffe1d33754fb3c59cf60d66::getInitializer($loader)); + } else { + $map = require __DIR__ . '/autoload_namespaces.php'; + foreach ($map as $namespace => $path) { + $loader->set($namespace, $path); + } + + $map = require __DIR__ . '/autoload_psr4.php'; + foreach ($map as $namespace => $path) { + $loader->setPsr4($namespace, $path); + } + + $classMap = require __DIR__ . '/autoload_classmap.php'; + if ($classMap) { + $loader->addClassMap($classMap); + } + } + + $loader->register(true); + + return $loader; + } +} diff --git a/vendor/composer/autoload_static.php b/vendor/composer/autoload_static.php new file mode 100644 index 0000000..c6c70d8 --- /dev/null +++ b/vendor/composer/autoload_static.php @@ -0,0 +1,26 @@ + + array ( + 'WPStrava_' => + array ( + 0 => __DIR__ . '/../..' . '/lib', + ), + ), + ); + + public static function getInitializer(ClassLoader $loader) + { + return \Closure::bind(function () use ($loader) { + $loader->prefixesPsr0 = ComposerStaticInit270e8bb56ffe1d33754fb3c59cf60d66::$prefixesPsr0; + + }, null, ClassLoader::class); + } +} diff --git a/vendor/composer/installed.json b/vendor/composer/installed.json new file mode 100644 index 0000000..fe51488 --- /dev/null +++ b/vendor/composer/installed.json @@ -0,0 +1 @@ +[] diff --git a/wp-strava.php b/wp-strava.php index 02573da..b265d9c 100755 --- a/wp-strava.php +++ b/wp-strava.php @@ -2,9 +2,9 @@ /** * Plugin Name: WP Strava * Plugin URI: https://wordpress.org/plugins/wp-strava/ - * Description: Plugin to show your strava.com information in your WordPress blog. Some Icons are Copyright © Yusuke Kamiyamane. All rights reserved. Licensed under a Creative Commons Attribution 3.0 license. - * Version: 1.4.3 - * Author: Carlos Santa Cruz, Justin Foell, Lance Willet, Daniel Lintott + * Description: Show your strava.com activity on your WordPress site. Some Icons are Copyright © Yusuke Kamiyamane. All rights reserved. Licensed under a Creative Commons Attribution 3.0 license. + * Version: 1.5.0-r1 + * Author: Carlos Santa Cruz, Justin Foell, Lance Willett, Daniel Lintott * License: GPL2 * Text Domain: wp-strava * Domain Path: /lang @@ -40,5 +40,6 @@ function wp_strava_load_plugin_textdomain() { } add_action( 'plugins_loaded', 'wp_strava_load_plugin_textdomain' ); -require_once WPSTRAVA_PLUGIN_DIR . 'lib/Strava.class.php'; +require_once WPSTRAVA_PLUGIN_DIR . 'vendor/autoload.php'; +require_once WPSTRAVA_PLUGIN_DIR . 'lib/WPStrava.php'; $wpstrava = WPStrava::get_instance();