1

Using Symfony 2.0, I am trying to make it work together with XCache.

XCache is properly installed.

As for the offitial Symfony documentation, we have this XcacheClassLoader.php that should make it. As for the same documentation, we get this piece of advice:

 /**
 * XcacheClassLoader implements a wrapping autoloader cached in Xcache for PHP 5.3.
 *
 * It expects an object implementing a findFile method to find the file. This
 * allows 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 XcacheClassLoader('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>
 * @author Kim Hemsø Rasmussen <kimhemsoe@gmail.com>
 *
 * @api
 */

If I am getting it right, the autoload.php should contain that code. This is what I am doing in autoload.php:

Normal loader is declared:

$loader = new UniversalClassLoader();

All stuff is done to that $loader, like registerNamespaces, registerPrefixes and all external dependencies are loaded to the $loader:

$loader
->registerNamespaces(
    array(
        'Symfony' => array(__DIR__ . '/../vendor/symfony/src',
                __DIR__ . '/../vendor/bundles'),
        'Sensio' => __DIR__ . '/../vendor/bundles',
        'JMS' => __DIR__ . '/../vendor/bundles',
        'Doctrine\\Common' => __DIR__. '/../vendor/doctrine-common/lib',
        'Doctrine\\DBAL' => __DIR__
etc...

Once all the "normal" stuff is made to the $loader, I declare the $cachedLoader, just as said in the documentation:

 // register classes with namespaces
 $loader->add('Symfony\Component', __DIR__.'/component');
 $loader->add('Symfony',           __DIR__.'/framework');

 $cachedLoader = new XcacheClassLoader('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();

Am I getting it right? Well obviously not, because all what I get is a completely blank page in my browser and not even logs are written in case they could give me some clue.

Thank you in advance.

ElPiter
  • 4,046
  • 9
  • 51
  • 80

2 Answers2

1

I didnt use XCache but i can suggest you an alternative way to increase class loading performance and as far as i can remember it is the best way of doing so.

Check Use Composer's Class Map Functionality section of the official documentation.

Refs:

By default, the Symfony2 standard edition uses Composer's autoloader in the 
autoload.php file. This autoloader is easy to use, as it will automatically find 
any new classes that you've placed in the registered directories.

Unfortunately, this comes at a cost, as the loader iterates over all configured 
namespaces to find a particular file, making file_exists calls until it finally 
finds the file it's looking for.

The simplest solution is to tell Composer to build a "class map" (i.e. a big 
array of the locations of all the classes). This can be done from the command 
line, and might become part of your deploy process:

    php composer.phar dump-autoload --optimize

Internally, this builds the big class map array in 
vendor/composer/autoload_namespaces.php.
Vadim Ashikhman
  • 9,851
  • 1
  • 35
  • 39
1

If you aren't already at 2.2, I recommend you upgrade to 2.2 if at all possible. 2.0 is now at the end of its maintenance cycle.

But, since we have experience in both - for symfony 2.0 there isn't an XcacheClassLoader included, you have to write it yourself and then put it in app/autoload.php (in the same place the the APC loader would go). For symfony 2.2 that's no longer the case.

Next, I recommend you make sure that your xcache is working right - you may want to create a php file and do a test like this:

<?php

$something="test";
xcache_set('key', $something, 0);
if (xcache_get('key') !== $something)
    echo "something's wrong...";
else
    echo "xcache appears to be working";

Last, what we what we did is pretty much what's in the latest version of Symfony/web/app.php (modified for XcacheClassLoader) - something that would (in its simple form) look like this:

<?php

use Symfony\Component\ClassLoader\XcacheClassLoader;
use Symfony\Component\HttpFoundation\Request;

$loader = require_once __DIR__.'/../app/bootstrap.php.cache';

$loader = new XcacheClassLoader('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);

// rest of app.php

This symfony2 documentation link below should explain things (including the other poster's comment on having composer dump the autoload files (note the 'current' in the url):

http://symfony.com/doc/current/book/performance.html

mmucklo
  • 390
  • 2
  • 6