Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[2.3] Add missing WinCacheClassLoader #7076

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 23, 2013
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
138 changes: 138 additions & 0 deletions src/Symfony/Component/ClassLoader/WinCacheClassLoader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\ClassLoader;

/**
* WinCacheClassLoader implements a wrapping autoloader cached in WinCache for PHP 5.3.
*
* It expects an object implementing a findFile method to find the file. This
* allow using it as a wrapper around the other loaders of the component (the
* ClassLoader and the UniversalClassLoader for instance) but also around any
* other autoloader following this convention (the Composer one for instance)
*
* $loader = new ClassLoader();
*
* // register classes with namespaces
* $loader->add('Symfony\Component', __DIR__.'/component');
* $loader->add('Symfony', __DIR__.'/framework');
*
* $cachedLoader = new WinCacheClassLoader('my_prefix', $loader);
*
* // activate the cached autoloader
* $cachedLoader->register();
*
* // eventually deactivate the non-cached loader if it was registered previously
* // to be sure to use the cached one.
* $loader->unregister();
*
* @author Fabien Potencier <[email protected]>
* @author Kris Wallsmith <[email protected]>
* @author Artem Ryzhkov <[email protected]>
*
* @api
*/
class WinCacheClassLoader
{
private $prefix;

/**
* The class loader object being decorated.
*
* @var \Symfony\Component\ClassLoader\ClassLoader
* A class loader object that implements the findFile() method.
*/
protected $decorated;

/**
* Constructor.
*
* @param string $prefix The WinCache namespace prefix to use.
* @param object $decorated A class loader object that implements the findFile() method.
*
* @throws \RuntimeException
* @throws \InvalidArgumentException
*
* @api
*/
public function __construct($prefix, $decorated)
{
if (!extension_loaded('wincache')) {
throw new \RuntimeException('Unable to use WinCacheClassLoader as WinCache is not enabled.');
}

if (!method_exists($decorated, 'findFile')) {
throw new \InvalidArgumentException('The class finder must implement a "findFile" method.');
}

$this->prefix = $prefix;
$this->decorated = $decorated;
}

/**
* Registers this instance as an autoloader.
*
* @param Boolean $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 Boolean|null True, if loaded
*/
public function loadClass($class)
{
if ($file = $this->findFile($class)) {
require $file;

return true;
}
}

/**
* Finds a file by class name while caching lookups to WinCache.
*
* @param string $class A class name to resolve to file
*
* @return string|null
*/
public function findFile($class)
{
if (false === $file = wincache_ucache_get($this->prefix.$class)) {
wincache_ucache_set($this->prefix.$class, $file = $this->decorated->findFile($class), 0);
}

return $file;
}

/**
* Passes through all unknown calls onto the decorated object.
*/
public function __call($method, $args)
{
return call_user_func_array(array($this->decorated, $method), $args);
}

}