Phpword: Composer installs not generating autoload.php properly?

Created on 4 Aug 2016  ·  16Comments  ·  Source: PHPOffice/PHPWord

Hey,

I tried installing PHPWord using composer install --prefer-source and the composer.json that you suggest developers should use in the documentation, but I'm getting this error in my error log when I try running the samples:

[04-Aug-2016 00:39:25 Europe/Zagreb] PHP Fatal error:  Uncaught Exception: Could not find file '/autoload.php'. It is generated by Composer. Use 'install --prefer-source' or 'update --prefer-source' Composer commands to move forward. in /Users/mbm/projekti/SomeProject/vendor/phpoffice/phpword/bootstrap.php:22
Stack trace:
#0 /Users/mbm/projekti/SomeProject/vendor/phpoffice/phpword/samples/Sample_Header.php(2): require_once()
#1 /Users/mbm/projekti/SomeProject/vendor/phpoffice/phpword/samples/index.php(2): include_once('/Users/mbm/proj...')
#2 {main}
  thrown in /Users/mbm/projekti/SomeProject/vendor/phpoffice/phpword/bootstrap.php on line 22

This is my composer.json:

{
    "name": "mbmjertan/SomeProject",
    "authors": [
        {
            "name": "Mario Borna Mjertan",
            "email": "[email protected]"
        }
    ],
     "require": {
       "phpoffice/phpword": "dev-develop"
    }
}

Composer:
Composer version 1.2-dev (06499749ff8875f939f413cf9a3725792d4ee4f9) 2016-07-11 07:16:26

PHP 7.0.0 on Mac OS X 10.11.6 (El Capitan) under Apache

Is there something I'm doing wrong or missing? This is my first time using Composer in a project.

For what it's worth, I tried looking at the source myself and it seems that PHPOffice is trying to include files from $vendorDirPath = realpath(__DIR__ . '/vendor');, a folder that does not exist.


Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.

Most helpful comment

Basically the bootstrap file is not getting the right path, at least for the samples page.
Here's what I changed phpoffice/phpword/bootstrap.php to:

$vendorDirPath = __DIR__ . "/../../autoload.php";

if (file_exists($vendorDirPath)) {
    require $vendorDirPath;
} else {
    throw new Exception(
        sprintf(
            'Could not find file \'%s\'. It is generated by Composer. Use \'install --prefer-source\' or \'update --prefer-source\' Composer commands to move forward.',
            $vendorDirPath
        )
    );
}

Once we get to the current directory, we need to travel back up two folders.
Don't know if it'll stuff up when I actually try using phpword but at lease the samples work.

All 16 comments

did u find the answer??

@avinashdchase unfortunately, no. I did a terrible hackjob to make it somewhat work, but that fell short and I ran out of things to try.

Basically the bootstrap file is not getting the right path, at least for the samples page.
Here's what I changed phpoffice/phpword/bootstrap.php to:

$vendorDirPath = __DIR__ . "/../../autoload.php";

if (file_exists($vendorDirPath)) {
    require $vendorDirPath;
} else {
    throw new Exception(
        sprintf(
            'Could not find file \'%s\'. It is generated by Composer. Use \'install --prefer-source\' or \'update --prefer-source\' Composer commands to move forward.',
            $vendorDirPath
        )
    );
}

Once we get to the current directory, we need to travel back up two folders.
Don't know if it'll stuff up when I actually try using phpword but at lease the samples work.

@CodeJason's suggestion worked for me.

@CodeJason's code worked for me as well. Is there a reason this hasn't been pushed?

Thanks @CodeJason, your suggestion helped me too

Had to use this

$vendorDirPath = __DIR__ . "/../../";

Reminds me again why I dislike composer. I had never been able to install at least one package with composer successfully from scratch. All needed rework.

Or you can simply change __DIR__ to $_SERVER["DOCUMENT_ROOT"]. So you can include it from any directory.

$vendorDirPath = realpath($_SERVER["DOCUMENT_ROOT"] . '/vendor');
if (file_exists($vendorDirPath . '/autoload.php')) {
    require $vendorDirPath . '/autoload.php';
} else {
    throw new Exception(
        sprintf(
            'Could not find file \'%s\'. It is generated by Composer. Use \'install --prefer-source\' or \'update --prefer-source\' Composer commands to move forward.',
            $vendorDirPath . '/autoload.php'
        )
    );
}

If you are starting the server from the samples folder (php -S localhost:800) try this:

$vendorDirPath = realpath($_SERVER["DOCUMENT_ROOT"] . '/../../../');
if (file_exists($vendorDirPath . '/autoload.php')) {
    require $vendorDirPath . '/autoload.php';
} else {
    throw new Exception(
        sprintf(
            'Could not find file \'%s\'. It is generated by Composer. Use \'install --prefer-source\' or \'update --prefer-source\' Composer commands to move forward.',
            $vendorDirPath . '/autoload.php'
        )
    );
}

Seems complex for just a self contained sample page.
Chris

you should run composer install in your phpword DIR

@1manfactory 很棒,弄了半天,还是你的这一句解决了我的问题!棒棒哒!

The docs need to better cover composer issues. Came back to this project to run some tests and could not get it to build. Finally ran:

composer update

And it got all the dependencies correctly and generated the autoload files so they work !
Such a wast of time, please add it to the main readme !

Hi i can not update phpword package . phpword package 0.16.0 and composer version is 1.8.1
when run on cmd composer require phpoffice/phpword
getting problem
the requested package phpoffice/phpword No version set (parsed as 1.0.0) is satisfiable by phpoffice/phpword No version set (parsed as 1.0.0) but this conflict with your requirements

Laptop setup ok it working but where install it windows server 2012 r2 then composer installed ok. package cant update
my composer.json code
"require": {
"php": "^5.3.3 || ^7.0",
"ext-xml": "*",
"zendframework/zend-escaper": "^2.2",
"phpoffice/common": "^0.2.9"
}
please help me anyone

@moly4565 I added some commands in the readme for production release that may help you.
See my pull request
https://github.com/PHPOffice/PHPWord/pull/1650

Chris

@vatavale your solution is perfect and work in any case

Was this page helpful?
0 / 5 - 0 ratings

Related issues

cyrillkalita picture cyrillkalita  ·  6Comments

totpero picture totpero  ·  6Comments

taophp picture taophp  ·  3Comments

Joel-James picture Joel-James  ·  3Comments

ahmednawazbutt picture ahmednawazbutt  ·  3Comments