use PHPExcel with composer and Symfony2.2
Asked Answered
D

1

4

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?

Deforce answered 24/4, 2013 at 16:2 Comment(0)
S
8

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.

Scarcity answered 24/4, 2013 at 16:23 Comment(4)
what you mean with will reinstall it with the latest information from your composer.json, including autoload, etc.? How can I include / enable autoload for PHPExcel, which has no own composer.json and no namespaces?Deforce
As I said once you do that you must first remove vendor/PHPOffice then run update, to force it to reinstall the package.Scarcity
You may have over engineered the problem/solution. All I use is: "CodePlex/PHPExcel": "1.7.8", under require. No need for PHPOffice or a package entry or anything under autoload.Follower
Naming of "PHPExcel" package was updated, you have to use "phpoffice/phpexcel" in composer.jsonZerline

© 2022 - 2024 — McMap. All rights reserved.