php-shopify icon indicating copy to clipboard operation
php-shopify copied to clipboard

Create autoload.php

Open a904guy opened this issue 4 years ago • 3 comments

Allow for non-composer users to download from github releases and work with the package on no shell accounts (sans composer), like FTP, ect.

a904guy avatar Sep 06 '21 21:09 a904guy

Replicating composer loading, this file will allow a person to embed the release into their project and include one file to have access to the package.

include('lib/php-shopify-1.1.19/autoload.php');

$shopify = PHPShopify\ShopifySDK::config(array(
    'ShopUrl' => ...,
    'ApiKey' => ...,
    'Password' => ...
));

I can vouch for its success, just implemented a script for iterating over products and updating metafields without shell access.

Worked flawlessly.

a904guy avatar Sep 07 '21 00:09 a904guy

Why should this library be responsible for that? If you want to use this library without composer you should handle that from your end.

humblewebdev avatar Sep 20 '21 19:09 humblewebdev

Why should this library be responsible for that?

I laid out a case for it in my original comment.

I've seen it mentioned in the issues tab, figured I would help solve a requested item.

If you don't want to support, that's okay, it's your project, just mark this as closed so I can remove the fork.

At least people will be able to find it if they look closely.

Instructions:

Add autoload.php in the root release directory, the file contents below:

<?php

$composer = json_decode(file_get_contents(__DIR__."/composer.json"), true);
$namespaces = $composer['autoload']['psr-4'];

// Foreach namespace specified in the composer, load the given classes
foreach ($namespaces as $namespace => $classpaths) {
    if (!is_array($classpaths)) {
        $classpaths = array($classpaths);
    }
    spl_autoload_register(function ($classname) use ($namespace, $classpaths, __DIR__) {
        // Check if the namespace matches the class we are looking for
        if (preg_match("#^".preg_quote($namespace)."#", $classname)) {
            // Remove the namespace from the file path since it's psr4
            $classname = str_replace($namespace, "", $classname);
            $filename = preg_replace("#\\\\#", "/", $classname).".php";
            foreach ($classpaths as $classpath) {
                $fullpath = __DIR__."/".$classpath."/$filename";
                if (file_exists($fullpath)) {
                    include_once $fullpath;
                }
            }
        }
    });
}

In your project just include the file from it's location, and use it as normal.

include('your_project/lib/php-shopify-1.1.19/autoload.php');

$shopify = PHPShopify\ShopifySDK::config(array(
    'ShopUrl' => ...,
    'ApiKey' => ...,
    'Password' => ...
));

a904guy avatar Sep 20 '21 20:09 a904guy