diff --git a/changelog/1017.md b/changelog/1017.md new file mode 100644 index 000000000..cb9dd5552 --- /dev/null +++ b/changelog/1017.md @@ -0,0 +1 @@ +- Added a PSR-4 loading script to allow Smarty to be used without Composer [#1017](https://github.com/smarty-php/smarty/pull/1017) diff --git a/docs/getting-started.md b/docs/getting-started.md index 5b49fffdb..3628fd203 100644 --- a/docs/getting-started.md +++ b/docs/getting-started.md @@ -25,9 +25,17 @@ Here's how you create an instance of Smarty in your PHP scripts: ```php testInstall(); // +///////////////////////////////////////////////////////////////////// + +define('__SMARTY_DIR', __DIR__ . '/../src/'); + +// Global function declarations +require_once(__SMARTY_DIR . "/functions.php"); + +spl_autoload_register(function ($class) { + // Class prefix + $prefix = 'Smarty\\'; + + // Does the class use the namespace prefix? + $len = strlen($prefix); + if (strncmp($prefix, $class, $len) !== 0) { + // If not, move to the next registered autoloader + return; + } + + // Hack off the prefix part + $relative_class = substr($class, $len); + + // Build a path to the include file + $file = __SMARTY_DIR . str_replace('\\', '/', $relative_class) . '.php'; + + // If the file exists, require it + if (file_exists($file)) { + require_once($file); + } +});