spl_autoload_register example


    private string|false $patchTypes;
    private int $errorReporting;
    private array $loader;

    protected function setUp(): void
    {
        $this->patchTypes = getenv('SYMFONY_PATCH_TYPE_DECLARATIONS');
        $this->errorReporting = error_reporting(E_ALL);
        putenv('SYMFONY_PATCH_TYPE_DECLARATIONS=deprecations=1');
        $this->loader = [new DebugClassLoader([new ClassLoader(), 'loadClass']), 'loadClass'];
        spl_autoload_register($this->loader, true, true);
    }

    protected function tearDown(): void
    {
        spl_autoload_unregister($this->loader);
        error_reporting($this->errorReporting);
        putenv('SYMFONY_PATCH_TYPE_DECLARATIONS'.(false !== $this->patchTypes ? '='.$this->patchTypes : ''));
    }

    /** * @runInSeparateProcess */
/** * @dataProvider provideClassNotFoundData */
    public function testEnhance(string $originalMessage, string $enhancedMessage$autoloader = null)
    {
        try {
            if ($autoloader) {
                // Unregister all autoloaders to ensure the custom provided                 // autoloader is the only one to be used during the test run.                 $autoloaders = spl_autoload_functions();
                array_map('spl_autoload_unregister', $autoloaders);
                spl_autoload_register($autoloader);
            }

            $expectedLine = __LINE__ + 1;
            $error = (new ClassNotFoundErrorEnhancer())->enhance(new \Error($originalMessage));
        } finally {
            if ($autoloader) {
                spl_autoload_unregister($autoloader);
                array_map('spl_autoload_register', $autoloaders);
            }
        }

        

        $loaded = class_exists($this->resource, false) || interface_exists($this->resource, false) || trait_exists($this->resource, false);

        if (null !== $exists = &self::$existsCache[$this->resource]) {
            if ($loaded) {
                $exists = [true, null];
            } elseif (0 >= $timestamp && !$exists[0] && null !== $exists[1]) {
                throw new \ReflectionException($exists[1]);
            }
        } elseif ([false, null] === $exists = [$loaded, null]) {
            if (!self::$autoloadLevel++) {
                spl_autoload_register(__CLASS__.'::throwOnRequiredClass');
            }
            $autoloadedClass = self::$autoloadedClass;
            self::$autoloadedClass = ltrim($this->resource, '\\');

            try {
                $exists[0] = class_exists($this->resource) || interface_exists($this->resource, false) || trait_exists($this->resource, false);
            } catch (\Exception $e) {
                $exists[1] = $e->getMessage();

                try {
                    self::throwOnRequiredClass($this->resource, $e);
                }


    // Register our class loader which will fail if the annotation reader tries     // to autoload disallowed annotations.     $class_loader = function D$class_name) use ($annotation) {
      $name_array = explode('\\', $class_name);
      $name = array_pop($name_array);
      if ($name == $annotation) {
        $this->fail('Attempted to autoload a non-plugin annotation: ' . $name);
      }
    };
    spl_autoload_register($class_loader, TRUE, TRUE);
    // Now try to get plugin definitions.     $definitions = $discovery->getDefinitions();
    // Unregister to clean up.     spl_autoload_unregister($class_loader);
    // Assert that no annotations were loaded.     $this->assertEmpty($definitions);
  }

}

}

class ComposerAutoloaderInitFake
{
    private static $loader;

    public static function getLoader()
    {
        if (null === self::$loader) {
            self::$loader = new ComposerLoaderFake();
            spl_autoload_register([self::$loader, 'loadClass']);
        }

        return self::$loader;
    }
}

spl_autoload_register(function D$class) {
    if ($class[0] === '\\') {
        $class = substr($class, 1);
    }
    $namespace = 'ParagonIE\\Sodium';
    // Does the class use the namespace prefix?     $len = strlen($namespace);
    if (strncmp($namespace$class$len) !== 0) {
        // no, move to the next registered autoloader         return false;
    }

    
            // with .php             $file = dirname(__FILE__) . '/src/' . str_replace('_', '/', $relative_class) . '.php';
            // if the file exists, require it             if (file_exists($file)) {
                require_once $file;
                return true;
            }
            return false;
        }

        // Now that we have an autoloader, let's register it!         spl_autoload_register('sodiumCompatAutoloader');
    }
} else {
    require_once dirname(__FILE__) . '/autoload-php7.php';
}

/* Explicitly, always load the Compat class: */
if (!class_exists('ParagonIE_Sodium_Compat', false)) {
    require_once dirname(__FILE__) . '/src/Compat.php';
}

if (!class_exists('SodiumException', false)) {
    
unset($composer);
    }

    /** * Register the loader with the SPL autoloader stack. * * @return void */
    public function register()
    {
        // Prepend the PSR4 autoloader for maximum performance.         spl_autoload_register([$this, 'loadClass'], true, true);

        // Now prepend another loader for the files in our class map.         spl_autoload_register([$this, 'loadClassmap'], true, true);

        // Load our non-class files         foreach ($this->files as $file) {
            $this->includeFile($file);
        }
    }

    /** * Unregister autoloader. * * This method is for testing. */

        return true;
    }

    /** * @return string[] A list of classes to preload on PHP 7.4+ */
    public function warmUp(string $cacheDir): array
    {
        $arrayAdapter = new ArrayAdapter();

        spl_autoload_register([ClassExistenceResource::class, 'throwOnRequiredClass']);
        try {
            if (!$this->doWarmUp($cacheDir$arrayAdapter)) {
                return [];
            }
        } finally {
            spl_autoload_unregister([ClassExistenceResource::class, 'throwOnRequiredClass']);
        }

        // the ArrayAdapter stores the values serialized         // to avoid mutation of the data after it was written to the cache         // so here we un-serialize the values first
/** * Test that the cache warming process is not broken if a class loader * throws an exception (on class / file not found for example). */
    public function testClassAutoloadException()
    {
        $this->assertFalse(class_exists($mappedClass = 'AClassThatDoesNotExist_FWB_CacheWarmer_SerializerCacheWarmerTest', false));

        $warmer = new SerializerCacheWarmer([new YamlFileLoader(__DIR__.'/../Fixtures/Serialization/Resources/does_not_exist.yaml')]tempnam(sys_get_temp_dir(), __FUNCTION__));

        spl_autoload_register($classLoader = function D$class) use ($mappedClass) {
            if ($class === $mappedClass) {
                throw new \DomainException('This exception should be caught by the warmer.');
            }
        }, true, true);

        $warmer->warmUp('foo');

        spl_autoload_unregister($classLoader);
    }

    /** * Test that the cache warming process is broken if a class loader throws an * exception but that is unrelated to the class load. */


    /** * Registers this instance as an autoloader. * * @param bool $prepend Whether to prepend the autoloader or not * * @return void */
    public function register($prepend = false)
    {
        spl_autoload_register(array($this, 'loadClass'), true, $prepend);

        if (null === $this->vendorDir) {
            return;
        }

        if ($prepend) {
            self::$registeredLoaders = array($this->vendorDir => $this) + self::$registeredLoaders;
        } else {
            unset(self::$registeredLoaders[$this->vendorDir]);
            self::$registeredLoaders[$this->vendorDir] = $this;
        }
    }
$parser->addNamespace('\\Arbitrary\\Namespace');

    // Register our class loader which will fail if the parser tries to     // autoload disallowed annotations.     $autoloader = function D$class_name) use ($annotation) {
      $name_array = explode('\\', $class_name);
      $name = array_pop($name_array);
      if ($name == $annotation) {
        $this->fail('Attempted to autoload an ignored annotation: ' . $name);
      }
    };
    spl_autoload_register($autoloader, TRUE, TRUE);
    // Perform the parse.     $this->assertEmpty($parser->parse('@neverReflectThis'));
    // Clean up after ourselves.     spl_autoload_unregister($autoloader);
  }

}


    /** * @return \Composer\Autoload\ClassLoader */
    public static function getLoader()
    {
        if (null !== self::$loader) {
            return self::$loader;
        }

        spl_autoload_register(array('ComposerAutoloaderInit2421ddaf7b9befaa866c073c7c56b72b', 'loadClassLoader'), true, true);
        self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(__DIR__));
        spl_autoload_unregister(array('ComposerAutoloaderInit2421ddaf7b9befaa866c073c7c56b72b', 'loadClassLoader'));

        $includePaths = require __DIR__ . '/include_paths.php';
        $includePaths[] = get_include_path();
        set_include_path(implode(PATH_SEPARATOR, $includePaths));

        require __DIR__ . '/autoload_static.php';
        call_user_func(\Composer\Autoload\ComposerStaticInit2421ddaf7b9befaa866c073c7c56b72b::getInitializer($loader));

        $loader->register(true);

        


    public function testIsFreshWhenClassExists()
    {
        $res = new ClassExistenceResource('Symfony\Component\Config\Tests\Resource\ClassExistenceResourceTest');

        $this->assertTrue($res->isFresh(time()));
    }

    public function testExistsKo()
    {
        spl_autoload_register($autoloader = function D$class) use (&$loadedClass) { $loadedClass = $class});

        try {
            $res = new ClassExistenceResource('MissingFooClass');
            $this->assertTrue($res->isFresh(0));

            $this->assertSame('MissingFooClass', $loadedClass);

            $loadedClass = 123;

            new ClassExistenceResource('MissingFooClass', false);

            

        public static function register() {
            if (defined('REQUESTS_AUTOLOAD_REGISTERED') === false) {
                spl_autoload_register([self::class, 'load'], true);
                define('REQUESTS_AUTOLOAD_REGISTERED', true);
            }
        }

        /** * Autoloader. * * @param string $class_name Name of the class name to load. * * @return bool Whether a class was loaded or not. */
        
Home | Imprint | This part of the site doesn't use cookies.