How can I provide a script for PHP CLI via composer (as standalone, and as dependency) -


i attempting write php script run command line. want use composer manage dependencies, make available installed dependency other projects. want maintain ability use on own (with dependencies).

currently, main.php "entry point" (what execute command line). built , tested it, did in "stand alone" mindset. such, main.php gets loads autoload so:

<?php require_once __dir__.'/../vendor/autoload.php'; 

continuing in stand alone mindset, set so:

  1. git clone package place
  2. cd package
  3. composer install

this produces following directory setup"

package     |-composer.json     |     |-src     |   |-classone.php     |   |     |   |-classtwo.php     |   |     |   |-main.php     |     |-vendor         |-autoload.php         |         |-composer         |         |-3rdpaty_1         |         |-3rdpaty_2 

this works well--i can run php src/main.php able find classes needs because loads __dir__.'../vendor/autoload.php'.

where run in trouble when want install package dependency project (in order have script available run there). add package composer.json , gets installed. attempting run php vendor/compnay/package/src/main.php fails, however, because necessary autoload.php in different place:

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 

i understand wrong, not sure how fix it. how 1 provide such thing using composer? i've searched around lot don't see asking or answering same question. notice bin property of composer.json , started looking idea, i'm still not finding lot of info on how set script find needs in different contexts.

i considered trying use include in if, , running second include other path on fail, doesn't seem proper way go it.

a common practice both locations autoload file. see instance snippet used behat:

function includeifexists($file) {     if (file_exists($file)) {         return include $file;     } } if ((!$loader = includeifexists(__dir__.'/../vendor/autoload.php')) && (!$loader = includeifexists(__dir__.'/../../../autoload.php'))) {     fwrite(stderr,         'you must set project dependencies, run following commands:'.php_eol.         'curl -s http://getcomposer.org/installer | php'.php_eol.         'php composer.phar install'.php_eol     );     exit(1); } 

Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -