Skip to content

Create autoload.php #239

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Create autoload.php #239

wants to merge 1 commit into from

Conversation

a904guy
Copy link

@a904guy a904guy commented Sep 6, 2021

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

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
Copy link
Author

a904guy commented Sep 7, 2021

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
Copy link

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
Copy link
Author

a904guy commented Sep 20, 2021

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

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants