Skip to content

Commit 61db287

Browse files
authored
Scottchiefbaker/master (#1019)
* Add a PSR-4 loading script to allow Smarty to be used without Composer authored-by: Scott Baker <scott@perturb.org>
1 parent 3293a87 commit 61db287

3 files changed

Lines changed: 51 additions & 0 deletions

File tree

changelog/1017.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Added a PSR-4 loading script to allow Smarty to be used without Composer [#1017](https://github.com/smarty-php/smarty/pull/1017)

docs/getting-started.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,17 @@ Here's how you create an instance of Smarty in your PHP scripts:
2525
```php
2626
<?php
2727

28+
// Instantiated via composer
2829
require 'vendor/autoload.php';
2930
use Smarty\Smarty;
3031
$smarty = new Smarty();
32+
33+
// or ...
34+
35+
// Instantiated directly
36+
require("/path/to/smarty/libs/Smarty.class.php");
37+
use Smarty\Smarty;
38+
$smarty = new Smarty();
3139
```
3240

3341
Now that the library files are in place, it's time to set up the Smarty

libs/Smarty.class.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
/////////////////////////////////////////////////////////////////////
4+
// This is a stub PSR-4 loading script that gets all the pieces of //
5+
// Smarty 5.x loaded without requiring the use of composer. It's //
6+
// not really a 'class' file, but the name is used so we're //
7+
// backwards compatible with previous versions of Smarty. //
8+
// //
9+
// Example: //
10+
// require_once("/path/to/smarty/libs/Smarty.class.php"); //
11+
// //
12+
// $smarty = new Smarty\Smarty; //
13+
// $smarty->testInstall(); //
14+
/////////////////////////////////////////////////////////////////////
15+
16+
define('__SMARTY_DIR', __DIR__ . '/../src/');
17+
18+
// Global function declarations
19+
require_once(__SMARTY_DIR . "/functions.php");
20+
21+
spl_autoload_register(function ($class) {
22+
// Class prefix
23+
$prefix = 'Smarty\\';
24+
25+
// Does the class use the namespace prefix?
26+
$len = strlen($prefix);
27+
if (strncmp($prefix, $class, $len) !== 0) {
28+
// If not, move to the next registered autoloader
29+
return;
30+
}
31+
32+
// Hack off the prefix part
33+
$relative_class = substr($class, $len);
34+
35+
// Build a path to the include file
36+
$file = __SMARTY_DIR . str_replace('\\', '/', $relative_class) . '.php';
37+
38+
// If the file exists, require it
39+
if (file_exists($file)) {
40+
require_once($file);
41+
}
42+
});

0 commit comments

Comments
 (0)