Skip to content

Commit

Permalink
adds a post-processing step to publish the PHP package
Browse files Browse the repository at this point in the history
avoids the need for using output buffering, and removes all HTML comments from the source code.
  • Loading branch information
aaronpk committed Jan 24, 2016
1 parent 5b310b0 commit e9f4b76
Show file tree
Hide file tree
Showing 7 changed files with 170 additions and 12 deletions.
6 changes: 1 addition & 5 deletions README.md
Expand Up @@ -17,9 +17,7 @@ Clientside in HTML:
Serverside in PHP:

```
ob_start(); // stops the few HTML comments in CASSIS from being outputted
include 'cassis.js';
ob_end_clean();
include 'cassis.php';
```

In PHP using Composer:
Expand All @@ -31,9 +29,7 @@ In PHP using Composer:
```

```
ob_start();
require_once 'vendor/autoload.php';
ob_end_clean();
```

Tests
Expand Down
4 changes: 0 additions & 4 deletions cassis-loader.php

This file was deleted.

122 changes: 122 additions & 0 deletions cassis.php

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion composer.json
@@ -1,6 +1,6 @@
{
"name": "tantek/cassis",
"autoload": {
"files": ["cassis-loader.php"]
"files": ["cassis.php"]
}
}
19 changes: 19 additions & 0 deletions php-tests/PostProcessTest.php
@@ -0,0 +1,19 @@
<?php
// A few tests to make sure the post-processing actually stopped the source from outputting content
class PostProcessTest extends PHPUnit_Framework_TestCase {

public function testEllipsize() {
ob_start();
$short = ellipsize_to_word('some text', 40, '...', 10);
$result = ob_get_clean();
$this->assertEquals('', $result);
}

public function testBase60() {
ob_start();
num_to_sxg(503194);
$result = ob_get_clean();
$this->assertEquals('', $result);
}

}
2 changes: 0 additions & 2 deletions php-tests/bootstrap.php
@@ -1,4 +1,2 @@
<?php
ob_start();
require_once __DIR__ . '/../vendor/autoload.php';
ob_end_clean();
27 changes: 27 additions & 0 deletions post-process.php
@@ -0,0 +1,27 @@
<?php
/*
* After making changes to cassis.js, run this file to transform the JS into
* native PHP code. This process involves removing all PHP and HTML comments,
* so that no comment hacks or output buffering is required in order to use
* Cassis in PHP.
*/

// Load the source code, stripping any comments and whitespace from the PHP source
$source = php_strip_whitespace('cassis.js');

// Remove the top comment that talks about using ob_start
$source = preg_replace('/\/\* <!--.+\n\/\* <!-- /Usm', '', $source);

// Remove HTML comments
$source = preg_replace('/<!--(.|\s)*?-->/', '', $source);

// Remove close/open PHP tags
$source = preg_replace('/\?>\s+<\?php/', '', $source);

// Remove trailing HTML comment tag (and PHP tag since we can do that safely)
$source = preg_replace('/\?> -->$/', '', $source);

// Put each function on its own line
$source = preg_replace('/function /', "\n$0", $source);

file_put_contents('cassis.php', $source);

0 comments on commit e9f4b76

Please sign in to comment.