phpclassic / php-shopify

PHP SDK for Shopify API
Apache License 2.0
568 stars 211 forks source link

Create autoload.php #239

Open a904guy opened 3 years ago

a904guy commented 3 years ago

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 commented 3 years ago

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.

humblewebdev commented 2 years ago

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

a904guy commented 2 years ago

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' => ...
));