title: How can I provide a script for PHP CLI via composer (as standalone, and as dependency)
body: <p>I am attempting to write a PHP script that I would like to run from the command line. I want to use composer to manage its dependencies, as well as to make it available to be installed as a dependency to other projects. I also want to maintain the ability to use it on its own (with its dependencies).</p>
  
  <p>Currently, <code>main.php</code> is my "entry point" (what I would execute from the command line). As I built and tested it, I did so in the "stand alone" mindset. A such, <code>main.php</code> requires autoload like so:</p>
  
  <pre><code>&lt;?php
  require_once __DIR__.'/../vendor/autoload.php';
  </code></pre>
  
  <p>Continuing in stand alone mindset, I set it up like so:</p>
  
  <ol>
  <li><code>git clone</code> package into place</li>
  <li><code>cd package</code></li>
  <li><code>composer install</code></li>
  </ol>
  
  <p>This produces the following directory setup"</p>
  
  <pre><code>package
      |-composer.json
      |
      |-src
      |   |-ClassOne.php
      |   |
      |   |-ClassTwo.php
      |   |
      |   |-main.php
      |
      |-vendor
          |-autoload.php
          |
          |-composer
          |
          |-3rdpaty_1
          |
          |-3rdpaty_2
  </code></pre>
  
  <p>This works well--I can run <code>php src/main.php</code> which is able to find the classes it needs because it loads <code>__DIR__.'../vendor/autoload.php'</code>.</p>
  
  <p>Where I run in to trouble is when I want to install the package as a dependency to another project (in order to have the script available to run there). I add my package to the <code>composer.json</code> and it gets installed. Attempting to run <code>php vendor/compnay/package/src/main.php</code> fails, however, because the necessary <code>autoload.php</code> is in a different place:</p>
  
  <pre><code>dependent_project
      |-composer.json
      |
      |-some_code
      |
      |-vendor
          |-autoload.php
          |
          |-composer
          |
          |-company
          |    |-package
          |        |-composer.json
          |        |
          |        |-src
          |            |-ClassOne.php
          |            |
          |            |-ClassTwo.php
          |            |
          |            |-main.php
          |
          |-other_vendor_1
          |
          |-other_vendor_2
  </code></pre>
  
  <p>I understand what is wrong, but I am not sure how to fix it. How does one provide such a thing using composer? I've searched around a lot but I don't see anyone asking or answering the same question. I notice the <code>bin</code> property of <code>composer.json</code> and started looking into that idea, but I'm still not finding a lot of info on how to properly set up my script to find what it needs in the different contexts.</p>
  
  <p>I considered trying to use <code>include</code> in an <code>if</code>, and running a second <code>include</code> to the other path on fail, but that doesn't seem like the proper way to go about it.</p>