xmlsquad / gsheet-to-xml

Given the url of a Google Sheet, this Symfony Console command fetches the Google Sheet and outputs it in the form of Xml.
Apache License 2.0
2 stars 1 forks source link

Allow standalone command bootstrap file to find the vendor dir in parent directories #19

Closed forikal-uk closed 6 years ago

forikal-uk commented 6 years ago

Currently the standalone bootstrap file expects the autoloader to be in one relative location

#!/usr/bin/env php
<?php
require __DIR__ . '/../vendor/autoload.php';

I notice that other commands can find them irregardless of whether the bin directory is in the root of the project /bin or the vendor/bin directory.

There are two styles in xmlsquad.

Pattern A xml-authoring-tools searches up the parent directories in a dynamic fashion:


#!/usr/bin/env php
<?php
set_time_limit(0);
$vendor = __DIR__;
while (!file_exists($vendor.'/vendor')) {
    $vendor = dirname($vendor);
}
require $vendor.'/vendor/autoload.php';

Pattern B Whereas the ping-drive is hardcoded with some places to look:

#!/usr/bin/env php
<?php
foreach ([__DIR__.'/../../../autoload.php', __DIR__.'/../../vendor/autoload.php', __DIR__.'/../vendor/autoload.php'] as $file) {
    if (file_exists($file)) {
        require_once $file;
        break;
    }
}
...

I am not sure which one is best pattern to copy.

forikal-uk commented 6 years ago

I am thinking that, if a user has not run composer installor composer update to get a vendor directory created, then pattern A will carry on going forever until the user types CTRL+C which is not particularly good userbility.

So, I prefer pattern B.

Let's use pattern B in the gsheet-to-xml bootstrap.

and also test the never-ending-loop assumption in those projects that use pattern A and add bug if needed.

forikal-uk commented 6 years ago

I have added some code and tested it.

See: 4ba5935

To test it, I commented out one of the paths to force it to fail and ensured it presented a friendly message:

$ bin/gsheet-to-xml.php 
autoload.php not found.
Searched in:
[/Users/x/Documents/Projects/XmlAuthoringSuite/xml-authoring-project/vendor/xmlsquad/gsheet-to-xml/bin/../vendor/autoload.php]
Maybe you forgot to run 'composer install'?

and when it finds the autoload.php it just works:

$ bin/gsheet-to-xml.php 

  Not enough arguments (missing: "drive-url").  

inventory:gsheet-to-xml [-r|--recursive] [--client-secret-file CLIENT-SECRET-FILE] [--access-token-file ACCESS-TOKEN-FILE] [--force-authenticate] [--] <drive-url>