Magento – Composer Vendor Directory : Accessing magento classes from Vendor directory

autoloadercomposermagento-1.9

I have installed a magento module called Mage-Resque using composer. Composer has installed all the components into a directory called Vendor. When I run the module, one of the component needs to access the class Mns_Resque_Model_Job_Logmessage. But it always returns error Class not found.

As per my understanding the codes in Vendor directory cannot access the Namespace of Magento. How can I solve this issue should I something additional config in composor.json file.

EDIT

Here is the error message that I get

Could not find job class Mns_Resque_Model_Job_Logmessage
backtrace : vendor/chrisboulton/php-resque/lib/Resque/Job.php(172): Resque_Job_Factory->create('Mns_Resque_Mode...', Array, 'default')\",\"#1 //vendor/chrisboulton/php-resque/lib/Resque/Job.php(189): Resque_Job->getInstance()\",\"#2 //vendor/chrisboulton/php-resque/lib/Resque/Worker.php(240): Resque_Job->perform()\",\"#3 //vendor/chrisboulton/php-resque/lib/Resque/Worker.php(202): Resque_Worker->perform(Object(Resque_Job))\",\"#4 //vendor/chrisboulton/php-resque/bin/resque(127): Resque_Worker->work(5, false)

EDIT

When I searched I found that the check ie the check for to whether class exists happens in following path vendor/chrisboulton/php-resque/lib/Resque/Job/Factory.php. But the model class is in app/code/community/Mns/Resque/Model/Job/Logmessage.php.

Composer.json

{

    "require": {
        "ajbonner/mage-resque": "*",
        "magento-hackathon/magento-composer-installer": "*"
    },
    "require-dev": {
        "aoepeople/aoe_profiler": "*",
        "magetest/magento-phpunit-extension": "*"
    },
    "repositories": [
    {
        "type": "vcs",
        "url": "https://github.com/magento-hackathon/magento-composer-installer"
    },
    {
        "type": "vcs",
        "url": "https://github.com/ajbonner/magento-composer-autoload"
    },
    {
        "type": "vcs",
        "url": "https://github.com/ajbonner/mage-resque"
    },
    {
        "type": "vcs",
        "url": "https://github.com/MageTest/Mage-Test"
    },
    {
        "type": "vcs",
        "url": "https://github.com/fbrnc/Aoe_Profiler"
    }
    ],
    "extra":{
        "magento-root-dir": "./",
        "magento-deploystrategy":"copy" 
    },
    "config": {
        "bin-dir": "shell"
    }
    "minimum-stability": "dev"
}

To test I have created a controller action from which I'm queuing a job. The job seems to be getting queued in Redis server but then it results in above error.

Here is the code in controller action I'm using (Please note that this is code I copied from GitHub page for Mage Resque)

$resque = Mage::getSingleton('mnsresque/factory')->create();
$resque->addJob(
    'Mns_Resque_Model_Job_Logmessage',
    array('message'=>'foo'));

As per the error log the error occurs in the following file

vendor/chrisboulton/php-resque/lib/Resque/Job/Factory.php:16

Which is outside of the Magento app/ folder structure. And following is the code that causes this error.

if (!class_exists($className)) {
            throw new Resque_Exception(
                'Could not find job class ' . $className . '.'
            );
        }

The class above code trying to access is defined in app/code/local/community/Mns/Resque/Model/Job/. I have also tried copying the entire folder Mns to /chrisboulton/php-resque/lib/Resque/Job/ thinking that the class_exists checks in the current folder. But that too is not working.

How I can resolve this issue. Have i missed something?

Best Answer

This has to work, the issue is not in the code.

Possible reasons:

  • app/code/community/Msn/Resque/Model/Job/Logmessage.php is not readable. Please double check permissions and owner.
  • Opcode cache (like APC) is in place and not up to date. Flush it.
  • You use the Magento compiler. Recompile, or better, disable it entirely.

Explanation why you are looking in the wrong direction:

  1. The code is called from a Magento controller, and Mage::getSingleton('mnsresque/factory') did not result in an error, from that I conclude:

    • the Magento autoloader works
    • the module files are in the right location
  2. You get an error in vendor/chrisboulton/php-resque/lib/Resque/Job/Factory.php, that means:

    • the 3rd party library in vendor can be loaded as well
Related Topic