Skip to content

Instantly share code, notes, and snippets.

@pentagonal
Created June 18, 2017 07:00
Show Gist options
  • Save pentagonal/eaed8888d577708e2df334901838188b to your computer and use it in GitHub Desktop.
Save pentagonal/eaed8888d577708e2df334901838188b to your computer and use it in GitHub Desktop.
Using Composer To Register & Load Class Files
<?php
namespace Pentagonal\FollowComposerLoader;
use InvalidArgumentException;
use Composer\Autoload\ClassLoader;
/**
* Class ComposerLoaderPSR4
*
* Using @uses ClassLoader that make Load Class With Easy Way
*/
class ComposerLoaderPSR4
{
/**
* @var ClassLoader
*/
protected $classLoader;
/**
* ComposerLoaderPSR4 constructor.
* @param ClassLoader|null $classLoader
*/
public function __construct(ClassLoader $classLoader = null)
{
$this->classLoader = $classLoader ?: new ClassLoader();
}
/**
* Add PSR4
*
* @param array $nameSpacePath key as NameSpace & value as paths
* @return ComposerLoaderPSR4
* @throws InvalidArgumentException
*/
public function addArray(array $nameSpacePath) : ComposerLoaderPSR4
{
foreach ($nameSpacePath as $nameSpace => $paths) {
// if there was has a slash fix to backslash
if (strpos($nameSpace, '/') !== false) {
$nameSpace = preg_replace('(\\\|\/)', '\\', $nameSpace);
}
// Trim & add su-fix back slash that make Name Space Valid
$nameSpace = trim($nameSpace, '\\') . '\\';
if (!is_string($paths) && ! is_array($paths)) {
throw new InvalidArgumentException(
sprintf(
"Invalid paths for Name Space %s. Paths must be as a string or array.",
$nameSpace
),
E_USER_ERROR
);
} elseif (is_array($paths)) {
foreach ($paths as $keyPath => $path) {
if (!is_string($path)) {
throw new InvalidArgumentException(
sprintf(
"Invalid path value for Name Space %s in key %s. Path must be as a string.",
$nameSpace,
$keyPath
),
E_USER_ERROR
);
}
}
}
$this->classLoader->addPsr4(
$nameSpace,
(array) $paths
);
}
return $this;
}
/**
* Add Path
*
* @param string $prefix
* @param string|array $paths
* @return ComposerLoaderPSR4
*/
public function add(string $prefix, $paths) : ComposerLoaderPSR4
{
return $this->addArray([$prefix => $paths]);
}
/**
* Register Auto loader
*
* @param bool $prepend
*/
public function register($prepend = false)
{
$this->classLoader->register($prepend);
}
/**
* UnRegister The Loader
*/
public function unRegister()
{
$this->classLoader->unregister();
}
/**
* @param array $path
* @param ClassLoader|null $classLoader
* @return ComposerLoaderPSR4
*/
public static function create(array $path, ClassLoader $classLoader = null) : ComposerLoaderPSR4
{
$object = new static($classLoader ?: new ClassLoader());
$object->addArray($path);
return $object;
}
}
@pentagonal
Copy link
Author

USAGE

<?php
namespace {

    use  Pentagonal\FollowComposerLoader\ComposerLoaderPSR4;
    /**
     * Create Object & Listing uses PSR4
     */
    ComposerLoaderPSR4::create([
        'The\\NameSpace\\' => [
             '/Path/To/Directory',
             '/Path/To/SecondDirectory',
        ],
        'The\\Second\\NameSpace\\' => [
            '/Path/To/DirectoryToRegister',
            '/Path/To/SecondDirectoryToRegister',
        ],
    ])
    // Register As AutoLoad Include Path
    ->register();
}

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