فهرست منبع

Changed classloader for unit tests and some recommendations on readme

Gediminas Morkevicius 14 سال پیش
والد
کامیت
979e3c8511

+ 5 - 0
README.markdown

@@ -55,6 +55,11 @@ You can test these extensions on [my blog](http://gediminasm.org/test/ "Test doc
 
 All tutorials for basic usage examples are on [my blog](http://gediminasm.org "Tutorials for extensions") also.
 
+### Recommendations
+
+- Use Symfony/Component/ClassLoader/UniversalClassLoader for autoloading these extensions, it will help
+to avoid triggering fatal error during the check of **class_exists**
+
 ### Running the tests:
 
 PHPUnit 3.4 or newer is required.

+ 2 - 2
lib/Gedmo/Mapping/ExtensionMetadataFactory.php

@@ -116,10 +116,10 @@ class ExtensionMetadataFactory
             $driverName = substr($driverName, 0, strpos($driverName, 'Driver'));
             // create driver instance
             $driverClassName = $this->extensionNamespace . '\Mapping\Driver\\' . $driverName;
-            if (!ClassLoader::classExists($driverClassName)) {
+            if (!class_exists($driverClassName)) {
                 // @TODO: implement XML driver also
                 $driverClassName = $this->extensionNamespace . '\Mapping\Driver\Annotation';
-                if (!ClassLoader::classExists($driverClassName)) {
+                if (!class_exists($driverClassName)) {
                     throw new \Gedmo\Exception\RuntimeException("Failed to fallback to annotation driver: ({$driverClassName}), extension driver was not found.");
                 }
             }

+ 1 - 1
lib/Gedmo/Mapping/MappedEventSubscriber.php

@@ -62,7 +62,7 @@ abstract class MappedEventSubscriber implements EventSubscriber
         if (preg_match('@Doctrine\\\([^\\\]+)@', $class, $m) && in_array($m[1], array('ODM', 'ORM'))) {
             if (!isset($this->adapters[$m[1]])) {
                 $adapterClass = $this->getNamespace() . '\\Mapping\\Event\\Adapter\\' . $m[1];
-                if (!/*ClassLoader::classExists*/class_exists($adapterClass)) {
+                if (!class_exists($adapterClass)) {
                     $adapterClass = 'Gedmo\\Mapping\\Event\\Adapter\\'.$m[1];
                 }
                 $this->adapters[$m[1]] = new $adapterClass;

+ 21 - 63
tests/bootstrap.php

@@ -13,69 +13,27 @@ define('TESTS_PATH', __DIR__);
 define('TESTS_TEMP_DIR', __DIR__.'/temp');
 define('VENDOR_PATH', realpath(__DIR__ . '/../vendor'));
 
-set_include_path(implode(PATH_SEPARATOR, array(
-    VENDOR_PATH,
-    get_include_path(),
-)));
-
-$classLoaderFile = VENDOR_PATH . '/doctrine-common/lib/Doctrine/Common/ClassLoader.php';
-if (!file_exists($classLoaderFile)) {
-    die('cannot find vendor, git submodule init && git submodule update');
-}
+$classLoaderFile = VENDOR_PATH . '/Symfony/Component/ClassLoader/UniversalClassLoader.php';
 
 require_once $classLoaderFile;
-$classLoader = new Doctrine\Common\ClassLoader(
-    'Doctrine\ORM', 'doctrine-orm/lib'
-);
-$classLoader->register();
-
-$classLoader = new Doctrine\Common\ClassLoader(
-    'Doctrine\DBAL', 'doctrine-dbal/lib'
-);
-$classLoader->register();
-
-$classLoader = new Doctrine\Common\ClassLoader(
-    'Doctrine\MongoDB', 'doctrine-mongodb/lib'
-);
-$classLoader->register();
-
-$classLoader = new Doctrine\Common\ClassLoader(
-    'Doctrine\ODM', 'doctrine-mongodb-odm/lib'
-);
-$classLoader->register();
-
-$classLoader = new Doctrine\Common\ClassLoader(
-    'Doctrine', 'doctrine-common/lib'
-);
-$classLoader->register();
-
-$classLoader = new Doctrine\Common\ClassLoader('Gedmo\Mapping\Mock', __DIR__);
-$classLoader->register();
-
-$classLoader = new Doctrine\Common\ClassLoader('Symfony');
-$classLoader->register();
-
-$classLoader = new Doctrine\Common\ClassLoader('Gedmo', __DIR__ . '/../lib');
-$classLoader->register();
-
-$classLoader = new Doctrine\Common\ClassLoader('Tool', __DIR__ . '/Gedmo');
-$classLoader->register();
-
-// fixture autoloaders
-$classLoader = new Doctrine\Common\ClassLoader('Translatable\Fixture', __DIR__ . '/Gedmo');
-$classLoader->register();
-
-$classLoader = new Doctrine\Common\ClassLoader('Tree\Fixture', __DIR__ . '/Gedmo');
-$classLoader->register();
-
-$classLoader = new Doctrine\Common\ClassLoader('Timestampable\Fixture', __DIR__ . '/Gedmo');
-$classLoader->register();
-
-$classLoader = new Doctrine\Common\ClassLoader('Sluggable\Fixture', __DIR__ . '/Gedmo');
-$classLoader->register();
-
-$classLoader = new Doctrine\Common\ClassLoader('Mapping\Fixture', __DIR__ . '/Gedmo');
-$classLoader->register();
+$loader = new Symfony\Component\ClassLoader\UniversalClassLoader;
+$loader->registerNamespaces(array(
+    'Symfony'                    => VENDOR_PATH,
+    'Doctrine\\MongoDB'          => VENDOR_PATH.'/doctrine-mongodb/lib',
+    'Doctrine\\ODM\\MongoDB'     => VENDOR_PATH.'/doctrine-mongodb-odm/lib',
+    'Doctrine\\Common'           => VENDOR_PATH.'/doctrine-common/lib',
+    'Doctrine\\DBAL'             => VENDOR_PATH.'/doctrine-dbal/lib',
+    'Doctrine\\ORM'              => VENDOR_PATH.'/doctrine-orm/lib',
+    'Gedmo\\Mapping\\Mock'       => __DIR__,
+    'Gedmo'                      => __DIR__.'/../lib',
+    'Tool'                       => __DIR__.'/Gedmo',
+    // fixture namespaces
+    'Translatable\\Fixture'      => __DIR__.'/Gedmo',
+    'Timestampable\\Fixture'     => __DIR__.'/Gedmo',
+    'Tree\\Fixture'              => __DIR__.'/Gedmo',
+    'Sluggable\\Fixture'         => __DIR__.'/Gedmo',
+    'Mapping\\Fixture'           => __DIR__.'/Gedmo',
+    'Loggable\\Fixture'          => __DIR__.'/Gedmo',
+));
+$loader->register();
 
-$classLoader = new Doctrine\Common\ClassLoader('Loggable\Fixture', __DIR__ . '/Gedmo');
-$classLoader->register();

+ 19 - 0
vendor/Symfony/Component/ClassLoader/LICENSE

@@ -0,0 +1,19 @@
+Copyright (c) 2004-2011 Fabien Potencier
+
+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.

+ 261 - 0
vendor/Symfony/Component/ClassLoader/UniversalClassLoader.php

@@ -0,0 +1,261 @@
+<?php
+
+/*
+ * This file is part of the Symfony package.
+ *
+ * (c) Fabien Potencier <fabien@symfony.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Symfony\Component\ClassLoader;
+
+/**
+ * UniversalClassLoader implements a "universal" autoloader for PHP 5.3.
+ *
+ * It is able to load classes that use either:
+ *
+ *  * The technical interoperability standards for PHP 5.3 namespaces and
+ *    class names (http://groups.google.com/group/php-standards/web/psr-0-final-proposal);
+ *
+ *  * The PEAR naming convention for classes (http://pear.php.net/).
+ *
+ * Classes from a sub-namespace or a sub-hierarchy of PEAR classes can be
+ * looked for in a list of locations to ease the vendoring of a sub-set of
+ * classes for large projects.
+ *
+ * Example usage:
+ *
+ *     $loader = new UniversalClassLoader();
+ *
+ *     // register classes with namespaces
+ *     $loader->registerNamespaces(array(
+ *         'Symfony\Component' => __DIR__.'/component',
+ *         'Symfony'           => __DIR__.'/framework',
+ *         'Sensio'            => array(__DIR__.'/src', __DIR__.'/vendor'),
+ *     ));
+ *
+ *     // register a library using the PEAR naming convention
+ *     $loader->registerPrefixes(array(
+ *         'Swift_' => __DIR__.'/Swift',
+ *     ));
+ *
+ *     // activate the autoloader
+ *     $loader->register();
+ *
+ * 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.
+ *
+ * @author Fabien Potencier <fabien@symfony.com>
+ *
+ * @api
+ */
+class UniversalClassLoader
+{
+    private $namespaces = array();
+    private $prefixes = array();
+    private $namespaceFallback = array();
+    private $prefixFallback = array();
+
+    /**
+     * Gets the configured namespaces.
+     *
+     * @return array A hash with namespaces as keys and directories as values
+     */
+    public function getNamespaces()
+    {
+        return $this->namespaces;
+    }
+
+    /**
+     * Gets the configured class prefixes.
+     *
+     * @return array A hash with class prefixes as keys and directories as values
+     */
+    public function getPrefixes()
+    {
+        return $this->prefixes;
+    }
+
+    /**
+     * Gets the directory(ies) to use as a fallback for namespaces.
+     *
+     * @return array An array of directories
+     */
+    public function getNamespaceFallback()
+    {
+        return $this->namespaceFallback;
+    }
+
+    /**
+     * Gets the directory(ies) to use as a fallback for class prefixes.
+     *
+     * @return array An array of directories
+     */
+    public function getPrefixFallback()
+    {
+        return $this->prefixFallback;
+    }
+
+    /**
+     * Registers the directory to use as a fallback for namespaces.
+     *
+     * @param string|array $dirs A directory path or an array of directories
+     *
+     * @api
+     */
+    public function registerNamespaceFallback($dirs)
+    {
+        $this->namespaceFallback = (array) $dirs;
+    }
+
+    /**
+     * Registers the directory to use as a fallback for class prefixes.
+     *
+     * @param string|array $dirs A directory path or an array of directories
+     *
+     * @api
+     */
+    public function registerPrefixFallback($dirs)
+    {
+        $this->prefixFallback = (array) $dirs;
+    }
+
+    /**
+     * Registers an array of namespaces
+     *
+     * @param array $namespaces An array of namespaces (namespaces as keys and locations as values)
+     *
+     * @api
+     */
+    public function registerNamespaces(array $namespaces)
+    {
+        foreach ($namespaces as $namespace => $locations) {
+            $this->namespaces[$namespace] = (array) $locations;
+        }
+    }
+
+    /**
+     * Registers a namespace.
+     *
+     * @param string       $namespace The namespace
+     * @param array|string $paths     The location(s) of the namespace
+     *
+     * @api
+     */
+    public function registerNamespace($namespace, $paths)
+    {
+        $this->namespaces[$namespace] = (array) $paths;
+    }
+
+    /**
+     * Registers an array of classes using the PEAR naming convention.
+     *
+     * @param array $classes An array of classes (prefixes as keys and locations as values)
+     *
+     * @api
+     */
+    public function registerPrefixes(array $classes)
+    {
+        foreach ($classes as $prefix => $locations) {
+            $this->prefixes[$prefix] = (array) $locations;
+        }
+    }
+
+    /**
+     * Registers a set of classes using the PEAR naming convention.
+     *
+     * @param string       $prefix  The classes prefix
+     * @param array|string $paths   The location(s) of the classes
+     *
+     * @api
+     */
+    public function registerPrefix($prefix, $paths)
+    {
+        $this->prefixes[$prefix] = (array) $paths;
+    }
+
+    /**
+     * Registers this instance as an autoloader.
+     *
+     * @param Boolean $prepend Whether to prepend the autoloader or not
+     *
+     * @api
+     */
+    public function register($prepend = false)
+    {
+        spl_autoload_register(array($this, 'loadClass'), true, $prepend);
+    }
+
+    /**
+     * Loads the given class or interface.
+     *
+     * @param string $class The name of the class
+     */
+    public function loadClass($class)
+    {
+        if ($file = $this->findFile($class)) {
+            require $file;
+        }
+    }
+
+    /**
+     * Finds the path to the file where the class is defined.
+     *
+     * @param string $class The name of the class
+     *
+     * @return string|null The path, if found
+     */
+    public function findFile($class)
+    {
+        if ('\\' == $class[0]) {
+            $class = substr($class, 1);
+        }
+
+        if (false !== $pos = strrpos($class, '\\')) {
+            // namespaced class name
+            $namespace = substr($class, 0, $pos);
+            foreach ($this->namespaces as $ns => $dirs) {
+                foreach ($dirs as $dir) {
+                    if (0 === strpos($namespace, $ns)) {
+                        $className = substr($class, $pos + 1);
+                        $file = $dir.DIRECTORY_SEPARATOR.str_replace('\\', DIRECTORY_SEPARATOR, $namespace).DIRECTORY_SEPARATOR.str_replace('_', DIRECTORY_SEPARATOR, $className).'.php';
+                        if (file_exists($file)) {
+                            return $file;
+                        }
+                    }
+                }
+            }
+
+            foreach ($this->namespaceFallback as $dir) {
+                $file = $dir.DIRECTORY_SEPARATOR.str_replace('\\', DIRECTORY_SEPARATOR, $class).'.php';
+                if (file_exists($file)) {
+                    return $file;
+                }
+            }
+        } else {
+            // PEAR-like class name
+            foreach ($this->prefixes as $prefix => $dirs) {
+                foreach ($dirs as $dir) {
+                    if (0 === strpos($class, $prefix)) {
+                        $file = $dir.DIRECTORY_SEPARATOR.str_replace('_', DIRECTORY_SEPARATOR, $class).'.php';
+                        if (file_exists($file)) {
+                            return $file;
+                        }
+                    }
+                }
+            }
+
+            foreach ($this->prefixFallback as $dir) {
+                $file = $dir.DIRECTORY_SEPARATOR.str_replace('_', DIRECTORY_SEPARATOR, $class).'.php';
+                if (file_exists($file)) {
+                    return $file;
+                }
+            }
+        }
+    }
+}