2
votes

I have a project with all the composer.json, phpunit.xml, etc in the project's root directory.

All the .php sources are in src/

I have configured composer.json as follows:

{
  "name" : "myproj",
  "config" : {
    "vendor-dir" : "src/vendor"
  },
  "autoload" : {
    "psr-4" : {
      "myclasses\\" : "src/classes"
    }
  }
}

This works beautifully on my dev machine.

The issue is that I only want to deploy the contents of the src directory so that I do not get all the project metadata on the deployment server.

Unfortunately the autoloader looks for the classes in /var/www/test/vendor/composer/../../../src/classes/ instead of e.g. /var/www/test/vendor/composer/../../classes/

PHP Warning: include(): Failed opening '/var/www/test/vendor/composer/../../../src/classes/myclasses/core/messages/MessageList.php' for inclusion (include_path='.:/usr/share/php') in /var/www/test/vendor/composer/ClassLoader.php on line 444

Is there a clever configuration I can make? Or am I forced to move composer.json (which I see as meta-data not needed for production) into /src?

1
if its /var/www/test/src/classes you wouldn't want /var/www/test/classes - Lawrence Cherone
Not really. Everything in src is in /var/www/test I want the autoloader to skip the /../src part - JoSSte
"I want the autoloader to skip the /../src part" - So change it in your composer.json file? You can point your namespace "myclasses" to where ever you want. What "project metadata" is it you don't want on the deployment server? The composer.json file? How are you deploying the application? Can't you just setup your deployment script to handle what is being deployed? - Magnus Eriksson
it dont work like that ,you would need to change composer.json and remove the src/ then run composer du, but obviously it then won't work locally when you have src - Lawrence Cherone
@MagnusEriksson I run composer install and composer -o dumpautoloader on my jenkins server and then scp it to the target server, which does not have composer, etc. ofc I could always keep composer.json in the deployment dir and then delete it so it's not on the destination server - JoSSte

1 Answers

2
votes

The composer.json file it's never used at runtime. There is no use for it anyway, it's only used by the composer command. If you do not run composer in your production machine, you do not need to ever upload it.

Your project it's not failing because "you are not deploying composer.json" with the code, but because you are dumping the autoloader having certain structure, which you actually mention in your composer.json configuration, and then trying to run the server with a different directory structure.

When you run composer install and composer dumpautoloader your project looks like this:

composer.json
src/ <--- this is where autoloader looks for your files.
--- yourCode/
--- moreCode/
--- vendor/
------ autoloader.php

But then in your server you on your server you have

yourCode/
moreCode/
vendor/
--- autoload.php <--- this can't find the 'src' directory

The solution is not to upload the contents of your src directory, but the src directory itself.

Any other script that needs to use the autoloader and the rest of the code should just include /var/www/test/src/vendor/autoload.php and everything would work as it should.

If you do not want to have a src directory inside test, then you shouldn't have it during the autoloader generation. Do not make your development and staging environments different from your production environment.

Your only alternative would be to do in your build machine:

  • run composer install
  • move composer.json to src
  • change composer.json so the paths declared on the autoload key do not mention src
  • run composer dumpautoload
  • delete composer.json
  • upload src contents.

It's not guaranteed to work and it's a brittle solution, but the problem is that you are trying to use the tool in a way that runs counter to the design of the tool.