Skip to content

Instantly share code, notes, and snippets.

@philsturgeon
Created May 7, 2013 13:56
Show Gist options
  • Save philsturgeon/5532765 to your computer and use it in GitHub Desktop.
Save philsturgeon/5532765 to your computer and use it in GitHub Desktop.
PSR-0 != Composer

A native implementation of PSR-0 using only the autoloader from the PSR-0 example in the spec would expect a folder structure like this:

League\Oauth2\Client\Foo = myapp/somefolder/League/Oauth2/Client/Foo.php
League\Oauth2\Server\Bar = myapp/somefolder/League/Oauth2/Server/Bar.php

If I was making these as packages, I could make them into two packages, which would be installed in different locations, because thats how Composer rolls:

League\Oauth2\Client\Foo = myapp/vendor/league/oauth2/src/League/Oauth2/Client/Foo.php
League\Oauth2\Server\Bar = myapp/vendor/league/oauth2-server/src/League/Oauth2/Server/Bar.php
@simensen
Copy link

simensen commented May 7, 2013

Which indicated that while Composer does allow multiple paths within a namespace

No it doesn't. It maps different locations with different namespace prefixes.

Composer allows multiple paths within a namespace. We are using this for Stack to ensure that we can use classes named like Stack\Builder, Stack\Session, and Stack\OAuth for individual middlewares. This is accomplished by setting Stack as the PSR-0 root for each Composer package. Works like a charm.

To see this in action, here is the vendor/composer/autoload_classmap.php generated when you composer install Stack\OAuth:

<?php

// autoload_namespaces.php generated by Composer

$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);

return array(
    'Symfony\\Component\\Routing\\' => $vendorDir . '/symfony/routing',
    'Symfony\\Component\\HttpKernel\\' => $vendorDir . '/symfony/http-kernel',
    'Symfony\\Component\\HttpFoundation\\' => $vendorDir . '/symfony/http-foundation',
    'Symfony\\Component\\EventDispatcher\\' => $vendorDir . '/symfony/event-dispatcher',
    'Stack' => array(
        $baseDir . '/src',
        $vendorDir . '/stack/builder/src',
        $vendorDir . '/stack/session/src',
        $vendorDir . '/stack/callable-http-kernel/src'
    ),
    'Silex' => $vendorDir . '/silex/silex/src',
    'Psr\\Log\\' => $vendorDir . '/psr/log',
    'Pimple' => $vendorDir . '/pimple/pimple/lib',
    'OAuth' => $vendorDir . '/lusitanian/oauth/src',
);

@philsturgeon
Copy link
Author

Cool!

Well in this case, this would be considered a Composer specific feature, which has been added as a super-set to the "sample SplClassLoader implementation" linked in the spec, and not - as Anthony has been insisting - a fault with PSR-0.

And again, thats not what I was even talking about in the first place :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment