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 ApcClassLoader('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 (fabien@symfony.com)
Author: Kris Wallsmith (kris@symfony.com)
Example #1
0
 /**
  * Builds internal request handling objects.
  *
  * @return $this
  */
 public function build()
 {
     if ($this->cache) {
         $loader = new ClassLoader();
         if ($this->apc) {
             $apcLoader = new ApcClassLoader(sha1('ReactServer'), $loader);
             $loader->unregister();
             $apcLoader->register(true);
         }
     }
     require_once $this->root_dir . '/AppKernel.php';
     define('KERNEL_ROOT', $this->root_dir);
     $kernel = new ReactKernel($this->env, $this->env === 'dev' ? true : false);
     $this->loop = Factory::create();
     // TODO make config for this part
     if (class_exists('\\Doctrine\\DBAL\\Driver\\PingableConnection')) {
         $this->loop->addPeriodicTimer(15, function () use($kernel) {
             foreach ($kernel->getContainer()->get('doctrine')->getConnections() as $connection) {
                 if ($connection instanceof \Doctrine\DBAL\Driver\PingableConnection) {
                     $connection->ping();
                 }
             }
         });
     }
     $this->socket = new SocketServer($this->loop);
     $http = new HttpServer($this->socket, $this->loop);
     $http->on('request', $this->handleRequest($kernel));
     return $this;
 }
Example #2
0
 /**
  * Builds internal request handling objects.
  *
  * @return $this
  */
 public function build()
 {
     if ($this->cache) {
         $loader = new ClassLoader();
         if ($this->apc) {
             $apcLoader = new ApcClassLoader(sha1('ReactServer'), $loader);
             $loader->unregister();
             $apcLoader->register(true);
         }
     }
     require_once $this->root_dir . '/AppKernel.php';
     define('KERNEL_ROOT', $this->root_dir);
     $kernel = new ReactKernel($this->env, $this->env === 'dev' ? true : false);
     $this->loop = Factory::create();
     $this->socket = new SocketServer($this->loop);
     $http = new HttpServer($this->socket, $this->loop);
     $http->on('request', $this->handleRequest($kernel));
     return $this;
 }
Example #3
0
<?php

use Symfony\Component\ClassLoader\ApcClassLoader;
use Symfony\Component\HttpFoundation\Request;
$loader = (require_once __DIR__ . '/../app/bootstrap.php.cache');
// Use APC for autoloading to improve performance.
// Change 'sf2' to a unique prefix in order to prevent cache key conflicts
// with other applications also using APC.
$apcLoader = new ApcClassLoader('sf2', $loader);
$loader->unregister();
$apcLoader->register(true);
require_once __DIR__ . '/../app/AppKernel.php';
require_once __DIR__ . '/../app/AppCache.php';
$kernel = new AppKernel('prod', false);
$kernel->loadClassCache();
$kernel = new AppCache($kernel);
// When using the HttpCache, you need to call the method in your front controller instead of relying on the configuration parameter
Request::enableHttpMethodParameterOverride();
$request = Request::createFromGlobals();
$response = $kernel->handle($request);
$response->send();
$kernel->terminate($request, $response);
 /**
  * @dataProvider getLoadClassPrefixCollisionTests
  */
 public function testLoadClassPrefixCollision($prefixes, $className, $message)
 {
     $loader = new ClassLoader();
     $loader->addPrefixes($prefixes);
     $loader = new ApcClassLoader('test.prefix.collision.', $loader);
     $loader->loadClass($className);
     $this->assertTrue(class_exists($className), $message);
 }
Example #5
0
 public static function enableAPC($prefix, $loader)
 {
     $apc = new ApcClassLoader($prefix, $loader);
     $loader->unregister();
     $apc->register(true);
 }
Example #6
0
 /**
  * Locate site path and initialize settings singleton.
  *
  * @param \Symfony\Component\HttpFoundation\Request $request
  *   The current request.
  *
  * @throws \Symfony\Component\HttpKernel\Exception\BadRequestHttpException
  *   In case the host name in the request is not trusted.
  */
 protected function initializeSettings(Request $request)
 {
     $site_path = static::findSitePath($request);
     $this->setSitePath($site_path);
     $class_loader_class = get_class($this->classLoader);
     Settings::initialize($this->root, $site_path, $this->classLoader);
     // Initialize our list of trusted HTTP Host headers to protect against
     // header attacks.
     $host_patterns = Settings::get('trusted_host_patterns', array());
     if (PHP_SAPI !== 'cli' && !empty($host_patterns)) {
         if (static::setupTrustedHosts($request, $host_patterns) === FALSE) {
             throw new BadRequestHttpException('The provided host name is not valid for this server.');
         }
     }
     // If the class loader is still the same, possibly upgrade to the APC class
     // loader.
     if ($class_loader_class == get_class($this->classLoader) && Settings::get('class_loader_auto_detect', TRUE) && function_exists('apcu_fetch')) {
         $prefix = Settings::getApcuPrefix('class_loader', $this->root);
         $apc_loader = new ApcClassLoader($prefix, $this->classLoader);
         $this->classLoader->unregister();
         $apc_loader->register();
         $this->classLoader = $apc_loader;
     }
 }
Example #7
0
<?php

use Symfony\Component\ClassLoader\ApcClassLoader;
use Symfony\Component\HttpFoundation\Request;
$loader = (require_once __DIR__ . '/../app/bootstrap.php.cache');
$loader = new ApcClassLoader('sf2', $loader);
$loader->register(true);
require_once __DIR__ . '/../app/AppKernel.php';
//require_once __DIR__.'/../app/AppCache.php';
$kernel = new AppKernel('prod', false);
$kernel->loadClassCache();
//$kernel = new AppCache($kernel);
Request::enableHttpMethodParameterOverride();
$request = Request::createFromGlobals();
$response = $kernel->handle($request);
$response->send();
$kernel->terminate($request, $response);
Example #8
0
 /**
  * @dataProvider getLoaderTests
  */
 public function testFindFile($className, $location)
 {
     $loader = new ApcClassLoader(md5(__DIR__), new Nsautoload());
     $file = $loader->findFile($className);
     $this->assertSame(realpath($file), realpath($location));
 }
<?php

require_once __DIR__ . '/vendor/symfony/class-loader/ApcClassLoader.php';
require_once __DIR__ . '/vendor/symfony/class-loader/Psr4ClassLoader.php';
use MF\App;
use MF\Render\MessageRender;
use Symfony\Component\ClassLoader\ApcClassLoader;
use Symfony\Component\ClassLoader\Psr4ClassLoader;
$loader = new Psr4ClassLoader();
$loader->addPrefix('MF', __DIR__ . '/src');
$loader->register();
$cachedLoader = new ApcClassLoader('apc.prefix', $loader);
$cachedLoader->register();
$loader->unregister();
$app = new App(new MessageRender('Hello world by ApcClassLoader!'));
$app->renderResponse();
<?php

require_once __DIR__ . '/vendor/symfony/class-loader/ApcClassLoader.php';
require_once __DIR__ . '/vendor/symfony/class-loader/Psr4ClassLoader.php';
use MF\App;
use MF\Render\MessageRender;
use Symfony\Component\ClassLoader\ApcClassLoader;
use Symfony\Component\ClassLoader\Psr4ClassLoader;
$loader = new Psr4ClassLoader();
$loader->addPrefix('MF', __DIR__ . '/src');
$loader = new ApcClassLoader('apc.prefix', $loader);
$loader->register();
$app = new App(new MessageRender('Hello world by ApcClassLoader!'));
$app->renderResponse();