diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..34464b5 --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +/composer.json +/composer.lock +/composer.phar +/vendor/ +/build/ +/utils/ diff --git a/.travis.yml b/.travis.yml index 8ea297a..845f9e8 100644 --- a/.travis.yml +++ b/.travis.yml @@ -11,6 +11,12 @@ matrix: os: - windows - linux +before_install: + - git clone https://github.com/phalcon/zephir + - phpize -v + - cd zephir + - ./install -c + - zephir init utils install: - travis_retry composer install --no-interaction - wget -c -nc --retry-connrefused --tries=0 https://github.com/php-coveralls/php-coveralls/releases/download/v2.0.0/php-coveralls.phar diff --git a/README.md b/README.md index bbcf990..ca72f29 100644 --- a/README.md +++ b/README.md @@ -4,3 +4,30 @@ [![Coveralls github branch](https://img.shields.io/coveralls/github/shake-php/autoloading/master.svg)](https://coveralls.io/github/shake-php/autoloading) [![Latest Stable Version](https://poser.pugx.org/shake-php/autoloading/v/stable)](https://packagist.org/packages/shake-php/autoloading) [![License](https://poser.pugx.org/shake-php/autoloading/license)](https://packagist.org/packages/shake-php/autoloading) + +The shake autoloader is to make autoloading of classes and files easy. + +## Installation + +via Shake: +> If you already use shake as your dependency manager it is already installed. + +via Composer: +```sh +composer require shake-php/autoloading +``` + +## Autoloaders + +> - Psr4 Autoloader +> - Classmap Autoloader +> - Files Autoloader + +## General Public License + +Permissions of this strong copyleft license are conditioned on making available complete source code of licensed works and modifications, which include larger works using a licensed work, under the same license. Copyright and license notices must be preserved. Contributors provide an express grant of patent rights. + +## Links + +- https://packagist.org/packages/shake-php/autoloading +- https://shake-php.github.io/shake.github.io/autoloading/index.html diff --git a/shake.json b/bin/classmap/.gitkeep similarity index 100% rename from shake.json rename to bin/classmap/.gitkeep diff --git a/bin/classmap/Test.php b/bin/classmap/Test.php new file mode 100644 index 0000000..24f8af6 --- /dev/null +++ b/bin/classmap/Test.php @@ -0,0 +1,18 @@ +. + * @link . + */ + +namespace Inisik\ClassmapTest; + +/** + * @class Test + */ +class Test +{ +} diff --git a/bin/classmap/Test2.php b/bin/classmap/Test2.php new file mode 100644 index 0000000..ebf5503 --- /dev/null +++ b/bin/classmap/Test2.php @@ -0,0 +1,18 @@ +. + * @link . + */ + +namespace Inisik\ClassmapTest; + +/** + * @class Test2 + */ +class Test2 +{ +} diff --git a/bin/classmap/extra/.gitkeep b/bin/classmap/extra/.gitkeep new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/bin/classmap/extra/.gitkeep @@ -0,0 +1 @@ + diff --git a/bin/classmap/extra/Test3.php b/bin/classmap/extra/Test3.php new file mode 100644 index 0000000..15ab73c --- /dev/null +++ b/bin/classmap/extra/Test3.php @@ -0,0 +1,18 @@ +. + * @link . + */ + +namespace Inisik\ClassmapTest; + +/** + * @class Test3 + */ +class Test3 +{ +} diff --git a/bin/classmap/extra/Test4.php b/bin/classmap/extra/Test4.php new file mode 100644 index 0000000..a647a4a --- /dev/null +++ b/bin/classmap/extra/Test4.php @@ -0,0 +1,18 @@ +. + * @link . + */ + +namespace Inisik\ClassmapTest; + +/** + * @class Test4 + */ +class Test4 +{ +} diff --git a/bin/function_reference.php b/bin/function_reference.php new file mode 100644 index 0000000..0279479 --- /dev/null +++ b/bin/function_reference.php @@ -0,0 +1,21 @@ +. + * @link . + */ + +function register_psr4_autoloader(array $options = array()): void { + (new Psr4Autoloading)->register($options); +} + +function register_files_autoloader(array $options = array()): viod { + (new FilesAutoloading)->register($options); +} + +function register_classmap_autoloader(array $options = array()): void { + (new ClassmapAutoloading)->register($options); +} diff --git a/bin/process.php b/bin/process.php new file mode 100644 index 0000000..465def6 --- /dev/null +++ b/bin/process.php @@ -0,0 +1,24 @@ +. + * @link . + */ + +namespace ShakeAutoloadingProcess239471002934583901294880124975124; + +function psr4(array $options = array()): void { + (new \Psr4Autoloading)->register($options); +} + +function files(array $options = array()): viod { + (new \FilesAutoloading)->register($options); +} + +function classmap(array $options = array()): void { + (new \ClassmapAutoloading)->register($options); +} + diff --git a/composer.json b/composer.json index dc4af5d..ee65105 100644 --- a/composer.json +++ b/composer.json @@ -24,6 +24,10 @@ "autoload": { "classmap": [ "./src" + ], + "files": [ + "./bin/process.php", + "./bin/function_reference.php" ] }, "require": { diff --git a/ext/classmap_autoloading.c b/ext/classmap_autoloading.c new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/ext/classmap_autoloading.c @@ -0,0 +1 @@ + diff --git a/ext/config.m4 b/ext/config.m4 new file mode 100644 index 0000000..ba09373 --- /dev/null +++ b/ext/config.m4 @@ -0,0 +1,4 @@ +if test "$PHP_SHAKE_AUTOLOADING" = "yes"; then + AC_DEFINE(HAVE_SHAKE_AUTOLOADING, 1, [Whether you have Shake Autoloading]) + PHP_NEW_EXTENSION(shake_autoloading, shake_autoloading.c, $ext_shared) +fi diff --git a/ext/config.w32 b/ext/config.w32 new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/ext/config.w32 @@ -0,0 +1 @@ + diff --git a/ext/extra/.gitkeep b/ext/extra/.gitkeep new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/ext/extra/.gitkeep @@ -0,0 +1 @@ + diff --git a/ext/files_autoloading.c b/ext/files_autoloading.c new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/ext/files_autoloading.c @@ -0,0 +1 @@ + diff --git a/ext/package.xml b/ext/package.xml new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/ext/package.xml @@ -0,0 +1 @@ + diff --git a/ext/php_shake_autoloading.h b/ext/php_shake_autoloading.h new file mode 100644 index 0000000..ce1862a --- /dev/null +++ b/ext/php_shake_autoloading.h @@ -0,0 +1,11 @@ +#ifndef PHP_SHAKE_AUTOLOADING_H +#define PHP_SHAKE_AUTOLOADING_H 1 + +extern zend_module_entry shake_autoloading_module_entry; +#define phpext_shake_autoloading_ptr &shake_autoloading_module_entry + +#ifdef ZTS +#include "TSRM.h" +#endif + +#endif diff --git a/ext/psr4_autoloading.c b/ext/psr4_autoloading.c new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/ext/psr4_autoloading.c @@ -0,0 +1 @@ + diff --git a/ext/shake_autoloading.c b/ext/shake_autoloading.c new file mode 100644 index 0000000..a666530 --- /dev/null +++ b/ext/shake_autoloading.c @@ -0,0 +1,59 @@ +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "php.h" +#include "php_ini.h" +#include "psr4_autoloading.c" +#include "files_autoloading.c" +#include "classmap_autoloading.c" +#include "php_shake_autoloading.h" + +#define PHP_SHAKE_AUTOLOADING_VERSION "1.1.2" +#define PHP_SHAKE_AUTOLOADING_EXTENSION_NAME "shake_autoloading" + +extern zend_module_entry shake_autoloading_module_entry; +#define phpext_shake_autoloading_ptr &shake_autoloading_module_entry + +PHP_FUNCTION(register_psr4_autoloader); +PHP_FUNCTION(register_files_autoloader); +PHP_FUNCTION(register_classmap_autoloader); + +static function_entry shake_autoloading_functions[] = { + PHP_FE(register_psr4_autoloader, NULL) + PHP_FE(register_files_autoloader, NULL) + PHP_FE(register_classmap_autoloader, NULL) + {NULL, NULL, NULL} +}; + +zend_module_entry slobel_module_entry = { + STANDARD_MODULE_HEADER, + PHP_SHAKE_AUTOLOADING_EXTENSION_NAME, + shake_autoloading_functions, + NULL, + NULL, + NULL, + NULL, + NULL, +#if ZEND_MODULE_API_NO >= 20010901 + PHP_SHAKE_AUTOLOADING_VERSION, +#endif + STANDARD_MODULE_PROPERTIES +}; + +#ifdef COMPILE_DL_SHAKE_AUTOLOADING +ZEND_GET_MODULE(shake_autoloading) +#endif + + +PHP_FUNCTION(register_psr4_autoloader) +{ +} + +PHP_FUNCTION(register_files_autoloader) +{ +} + +PHP_FUNCTION(register_classmap_autoloader) +{ +} diff --git a/ext/shake_autoloading.ini b/ext/shake_autoloading.ini new file mode 100644 index 0000000..ccb580a --- /dev/null +++ b/ext/shake_autoloading.ini @@ -0,0 +1,7 @@ +[PHP] + +extension=php_shake_autoloading + +disable_classmap_autoloading=0 +disable_files_autoloading=0 +disable_psr4_autoloading=0 diff --git a/src/AbstractAutoloader.php b/src/AbstractAutoloader.php index d2fd2d9..bd83b23 100644 --- a/src/AbstractAutoloader.php +++ b/src/AbstractAutoloader.php @@ -71,8 +71,10 @@ protected function try(string $file): bool { /** * @psalm-suppress UnresolvableInclude */ - if (file_exists($file)) + if (file_exists($file)) { include_once $file; - return true; + return true; + } + return false; } } diff --git a/src/AutoloadManager.php b/src/AutoloadManager.php new file mode 100644 index 0000000..d5d73a8 --- /dev/null +++ b/src/AutoloadManager.php @@ -0,0 +1,16 @@ +. + * @link . + */ + +/** + * @class AutoloadManager. + */ +class AutoloadManager implements AutoloadManagerInterface +{ +} diff --git a/src/AutoloadManagerInterface.php b/src/AutoloadManagerInterface.php new file mode 100644 index 0000000..66bea3f --- /dev/null +++ b/src/AutoloadManagerInterface.php @@ -0,0 +1,16 @@ +. + * @link . + */ + +/** + * @interface AutoloadManagerInterface. + */ +interface AutoloadManagerInterface +{ +} diff --git a/src/ClassmapAutoloading.php b/src/ClassmapAutoloading.php new file mode 100644 index 0000000..4b74ab7 --- /dev/null +++ b/src/ClassmapAutoloading.php @@ -0,0 +1,69 @@ +. + * @link . + */ + +/** + * @class ClassmapAutoloading. + * + * Classmap autoloader. + */ +class ClassmapAutoloading extends AbstractAutoloader +{ + + /** + * @var array $classmapLoadData The classmap load data. + */ + private $classmapLoadData = array(); + + /** + * Set the configuration options for the autoloader. + * + * @link . + * + * @param array $array An array of options. + * + * @return bool Returns TRUE on success or FALSE on failure. + */ + protected function setOptions(array $array = array()): bool { + $this->classmapLoadData = $array; + return true; + } + + /** + * Run the autoloader. + * + * @link . + * + * @param string $k The class name. + * + * @return void Return nothing. + */ + protected function load(string $k): void { + $replace = str_replace(['\\', '/'], DIRECTORY_SEPARATOR, $k); + $parts = explode(DIRECTORY_SEPARATOR, $replace); + $k = end($parts); + /** + * @var string $baseDir + */ + foreach ($this->classmapLoadData as $baseDir) { + $file = $baseDir . DIRECTORY_SEPARATOR . str_replace(['\\', '/'], DIRECTORY_SEPARATOR, $k) . '.php'; + if ($this->try($file)) + break; + } + } + + /** + * Get the autoloader information. + * + * @return array An array of information from the autoloader. + */ + public function getInfo(): array { + return array('optionsPassed' => $this->classmapLoadData); + } +} diff --git a/src/Psr4Autoloading.php b/src/Psr4Autoloading.php index 8b6d7a9..36f4c2e 100644 --- a/src/Psr4Autoloading.php +++ b/src/Psr4Autoloading.php @@ -62,8 +62,9 @@ protected function load(string $k): void { if (strncmp((string) $monolog, $k, $len) !== 0) continue; $relativeClass = substr($k, $len); - $file = $baseDir . DIRECTORY_SEPARATOR . str_replace('\\', '/', $relativeClass) . '.php'; - $this->try($file); + $file = $baseDir . DIRECTORY_SEPARATOR . str_replace(['\\', '/'], DIRECTORY_SEPARATOR, $relativeClass) . '.php'; + if ($this->try($file)) + break; } } diff --git a/tests/ClassmapTest.php b/tests/ClassmapTest.php new file mode 100644 index 0000000..83bf15b --- /dev/null +++ b/tests/ClassmapTest.php @@ -0,0 +1,49 @@ +. + * @link . + */ + +class ClassmapTest extends PHPUnit\Framework\TestCase +{ + + public function testRegistry(): void + { + + // Test using an empty option configuration. + // This will cover the set options function in the ClassmapAutoloading + // class. + (new ClassmapAutoloading)->register(/* No options are going to be passed. */); + + // Return test result. + $this->assertTrue(true); + + // Classmap to run. + $map = array( + __DIR__ . '/../bin/classmap/extra', + __DIR__ . '/../bin/classmap' + ); + + // Try to register it with the new options configuration. + (new ClassmapAutoloading)->register($map); + + // Try to use a class. + $result = new Inisik\ClassmapTest\Test; + $result = new Inisik\ClassmapTest\Test2; + $result = new Inisik\ClassmapTest\Test3; + $result = new Inisik\ClassmapTest\Test4; + + // Return test result. + $this->assertTrue(true); + + // Test getInfo. + (new ClassmapAutoloading)->getInfo(); + + // Return test result. + $this->assertTrue(true); + } +}