-1

I am working with a CMS that requires the user to upload extensions.

The users do not have access to composer. So I will need to include the dependencies in the distribution itself.

Would how I implement it work for autoloading all dependencies? Is this the way to go about this?

Here's the simplified directory structure of the distributed extension. (The user is supposed to upload the contents of the upload directory):

upload/Eg/
upload/Eg/autoload.php <-- autoloader to load the dependencies
upload/Eg/MyClass.php <-- requires autoload.php
upload/Eg/dependencies <-- where required composer packages are copied
upload/Eg/dependencies/guzzlehttp <-- for example

autoload.php

spl_autoload_register(function ($className)
{
    $className = ltrim($className, '\\');
    $fileName  = '';
    $namespace = '';
    if ($lastNsPos = strripos($className, '\\')) {
        $namespace = substr($className, 0, $lastNsPos);
        $className = substr($className, $lastNsPos + 1);
        $fileName  = str_replace('\\', DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR;
    }
    $fileName .= str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php';

    require $fileName;
});

MyClass.php

namespace Eg;

require './autoload.php';

use GuzzleHttp;

class MyClass {
}

Side note: The application I am writing actually "builds" the extension automatically. The copying of dependencies from composer's "vendor" folder is done automatically.

JC Lee
  • 2,337
  • 2
  • 18
  • 25
  • I don't understand what's the question. – yivi Jan 31 '17 at 09:49
  • @yivi The question if this implementation works and if it's a good way to go about it. – JC Lee Jan 31 '17 at 09:50
  • Have you tried it? "is it a good way to go" is a matter of opinion, not a great question for SO. And "does it work" is first answered by actually testing the code. If you do, and it doesn't, then you ask about an specific problem. :) – yivi Jan 31 '17 at 09:52

1 Answers1

-1

The users do not have access to composer. So I will need to include the dependencies in the distribution itself.

You are reinventing the wheel here. Composer is open source so I'd simply take a look and check if I can reuse its code and logic. You'd probably be able to easily get rid of 90% of the code, yet the autoloader and composer.json scanning should be all you need.

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
  • Downvote without constructive criticism is pretty useless. – Marcin Orlowski Jan 31 '17 at 10:05
  • Not me who downvoted but I had a look at composer. It's a lot to dig into. – JC Lee Feb 02 '17 at 09:18
  • Composer's sources are nicely structured, so I believe you can copy&paste most of the code w/o alteration. Sure, you can reinvent the wheel which may be an option if you do not have many use cases (as i.e. flexibility of composer may be simply too much for your needs). But if it is not, them this is the way I'd go and check Composer's sources first. but this will not take 1 minute like (most likely lazy downvoter wishes), but shoudl definitely pay off.. – Marcin Orlowski Feb 02 '17 at 12:34