Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Add an autoloader to use the snappy standalone. #24

Merged
merged 1 commit into from

2 participants

@GromNaN

Related to #19

Embed an autoloader to simplify use without framework dependency.

@mbontemps mbontemps merged commit e77ca12 into KnpLabs:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Dec 3, 2011
  1. @GromNaN
This page is out of date. Refresh to see the latest.
View
2  README.markdown
@@ -11,6 +11,8 @@ You will have to download wkhtmltopdf 0.10.0 >= rc2 in order to use Snappy.
```php
<?php
+require_once '/path/to/snappy/src/autoload.php';
+
use Knp\Snappy\Pdf;
$snappy = new Pdf('/usr/local/bin/wkhtmltopdf');
View
2  phpunit.xml.dist
@@ -9,7 +9,7 @@
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
- bootstrap="test/bootstrap.php"
+ bootstrap="src/autoload.php"
>
<testsuites>
<testsuite name="Snappy Test Suite">
View
21 src/autoload.php
@@ -0,0 +1,21 @@
+<?php
+
+/**
+ * Simple autoloader that follow the PHP Standards Recommendation #0 (PSR-0)
+ * @see https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-0.md for more informations.
+ *
+ * Code inspired from the SplClassLoader RFC
+ * @see https://wiki.php.net/rfc/splclassloader#example_implementation
+ */
+spl_autoload_register(function($className) {
+ $package = 'Knp\\Snappy';
+ $className = ltrim($className, '\\');
+ if (0 === strpos($className, $package)) {
+ $fileName = __DIR__ . DIRECTORY_SEPARATOR . str_replace('\\', DIRECTORY_SEPARATOR, $className) . '.php';
+ if (is_file($fileName)) {
+ require $fileName;
+ return true;
+ }
+ }
+ return false;
+});
View
19 test/bootstrap.php
@@ -1,19 +0,0 @@
-<?php
-
-spl_autoload_register(
- function($class) {
- static $classes = null;
- if ($classes === null) {
- $classes = array(
- 'knp\\snappy\\generatorinterface' => '/GeneratorInterface.php',
- 'knp\\snappy\\abstractgenerator' => '/AbstractGenerator.php',
- 'knp\\snappy\\image' => '/Image.php',
- 'knp\\snappy\\pdf' => '/Pdf.php'
- );
- }
- $cn = strtolower($class);
- if (isset($classes[$cn])) {
- require __DIR__ . '/../src/Knp/Snappy' . $classes[$cn];
- }
- }
-);
Something went wrong with that request. Please try again.