Skip to content

Instantly share code, notes, and snippets.

@TheDauthi
Created March 7, 2018 18:52
Show Gist options
  • Save TheDauthi/41a79de43f139e84851eca9d878ea729 to your computer and use it in GitHub Desktop.
Save TheDauthi/41a79de43f139e84851eca9d878ea729 to your computer and use it in GitHub Desktop.
Using a composer.json inside the vendor directory

This is how you create a composer.json that places itself inside the (managed) vendor directory Very simple, but I didn't find an example while searching, and I want to remind myself that I can do this.

Place this file inside the vendor directory. The following would be an example project structure

+- /project
   +- /lib
      +- /TestNamespace
         - TestClass.php
      +- /vendor
         - composer.json
         - composer.lock
         +- /bugsnag
         +- /composer
         +- /psr
// This is not a valid composer file, because it is not valid JSON due to the comments.
{
  // This is the important part
  // vendor-dir is the package install path, typically 'vendor'
  // Setting it to "." changes the composer package install path relative
  // to composer.json, but does NOT change the install structure.
  "config": {
      "vendor-dir": "."
  },
  "require": {
    "php": ">=5.4",
    "bugsnag/bugsnag": "^3.0",
    "psr/log": "^1.0",
  },
  // The other component. The PSR-4 namespace is simply loaded relative to the vendor-dir
  "autoload": {
    "psr-4": {
      "TestNamespace\\": "../lib/TestNamespace"
    }
  }
}
{
"config": {
"vendor-dir": "."
},
"require": {
"php": ">=5.4",
"bugsnag/bugsnag": "^3.0",
"psr/log": "^1.0",
},
"autoload": {
"psr-4": {
"TestNamespace\\": "../lib/TestNamespace"
}
}
}
```
<?php
// This file would go into
// Composer autoloader
require_once("vendor/autoload.php");
use TestNamespace\TestClass;
// etc, etc
$testClass = new \TestNamespace\TestClass();
<?php
namespace TestNamespace;
class TestClass
{
public function __construct()
{
echo "Fuck you, this code works";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment