Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a PSR-4 loading script to allow Smarty to be used without Composer #1017

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions docs/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,17 @@ Here's how you create an instance of Smarty in your PHP scripts:
```php
<?php

// Instantiated via composer
require 'vendor/autoload.php';
use Smarty\Smarty;
$smarty = new Smarty();

// or ...

// Instantiated directly
require("/path/to/smarty/libs/Smarty.class.php");
use Smarty\Smarty;
$smarty = new Smarty();
```

Now that the library files are in place, it's time to set up the Smarty
Expand Down
46 changes: 46 additions & 0 deletions libs/Smarty.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

/////////////////////////////////////////////////////////////////////
// This is a stub PSR-4 loading script that gets all the pieces of //
// Smarty 5.x loaded without requiring the use of composer. It's //
// not really a 'class' file, but the name is used so we're //
// backwards compatible with previous versions of Smarty. //
// //
// Example: //
// require_once("/path/to/smarty/libs/Smarty.class.php"); //
// //
// $smarty = new Smarty\Smarty; //
// $smarty->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)) {
//print "<div>Class <b>$class</b> maps to <code>$file</code></div>\n";
scottchiefbaker marked this conversation as resolved.
Show resolved Hide resolved

require_once($file);
}
});

// vim: tabstop=4 shiftwidth=4 noexpandtab autoindent softtabstop=4
scottchiefbaker marked this conversation as resolved.
Show resolved Hide resolved
Loading