Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

use PHPExcel with composer and Symfony2.2

I found this on SO: How to use PHPExcel correctly with Symfony 2

This works, but I want to use it with composer. The first part I already solved: to load PHPExcel for a special tag (the last stable release)

I don't find out how to fetch a tag with this syntax:

"repositories": [
    {
        "type": "vcs",
        "url": "https://github.com/umpirsky/SyliusAssortmentBundle"
    }
]

So I use the Package notation:
I found out, the reference should be the tag name on github. And the version cannot be the same value (PHPExcel_1.7.8). Seems that alphabetical characters are not allowed, so it's only the version as a number (1.7.8)

"repositories": [{
    "type": "package",
    "package": {
        "name": "PHPOffice/PHPExcel",
        "version": "1.7.8",
        "source": {
            "url": "https://github.com/PHPOffice/PHPExcel.git",
            "type": "git",
            "reference": "PHPExcel_1.7.8"
        }
    }
}]

The next step I didn't solve. I tried out every combination for the autoloading: psr-0, classmap, different paths, relative to project/vendor/phpexcel, update composer everytime, but nothing worked.

It only works, if I put this line

$loader->add('PHPExcel', __DIR__.'/../vendor/PHPOffice/PHPExcel/Classes');

into the app/autoload.php. I found out, that the first string (PHPExcel) can also be an empty string: ''.
Is there a differnece if I use PHPExcel or ''?

So my primary question is, how can I avoid to write this line into the autoload.php, put the equivalent commands into my project's composer.json?

like image 372
timaschew Avatar asked Feb 16 '23 17:02

timaschew


1 Answers

Regarding your primary question, the problem is that once the package is installed, if you update the definition and add autoload stuff, then running composer update will not change anything. Composer still has the old package that was already installed in its "cache", so it uses that to generate the autoload and that fails.

To resolve this you should remove the vendor/PHPOffice/PHPExcel directly and run composer update, which will reinstall it with the latest information from your composer.json, including autoload, etc. You should specify autoloading as such:

"repositories": [{
    "type": "package",
    "package": {
        "name": "PHPOffice/PHPExcel",
        "version": "1.8.0",
        "source": {
            "url": "https://github.com/PHPOffice/PHPExcel.git",
            "type": "git",
            "reference": "1.8.0"
        },
        "autoload": {
            "psr-0": {
                "PHPExcel": "Classes/"
            }
        }
    }
}],
"require": {
    "PHPOffice/PHPExcel": "1.8.*",
    ...

Regarding the secondary question and '' vs 'PHPExcel': '' just says that any namespace can be found in this directory. That means the autoloader will always scan this directory to find classes, which is convenient but slower than mapping namespaces to directories explicitly. So both work, but the more specific form is preferred, especially in packages you publish publicly.

like image 169
Seldaek Avatar answered Feb 27 '23 10:02

Seldaek