Skip to content

Commit

Permalink
Making HHVM friendly
Browse files Browse the repository at this point in the history
1. Added File::writeAndInclude which solves facebook/hhvm#4797
2. Improved Cache internal includes to be HHVM friendly
3. Added HHVM to the tests
  • Loading branch information
crodas committed Jan 5, 2016
1 parent a4217d9 commit 6da9a84
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 3 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ php:
- 5.4
- 5.5
- 7.0
- hhvm
8 changes: 6 additions & 2 deletions lib/crodas/FileUtil/Cache.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
/*
+---------------------------------------------------------------------------------+
| Copyright (c) 2014 César Rodas |
| Copyright (c) 2016 César Rodas |
+---------------------------------------------------------------------------------+
| Redistribution and use in source and binary forms, with or without |
| modification, are permitted provided that the following conditions are met: |
Expand Down Expand Up @@ -54,6 +54,7 @@ class Cache
protected $is_listening = false;

protected static $dir = '';
protected static $includes = array();

public static function setDirectory($dir)
{
Expand Down Expand Up @@ -89,7 +90,10 @@ public function __destruct()
public function run($method, Array $args)
{
if ($this->content === false) {
$this->content = (array)@ include $this->file ;
if (empty(self::$includes[$this->file])) {
self::$includes[$this->file] = (array)@ include $this->file ;
}
$this->content = & self::$includes[$this->file];
}

$name = serialize($args);
Expand Down
39 changes: 38 additions & 1 deletion lib/crodas/FileUtil/File.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
/*
+---------------------------------------------------------------------------------+
| Copyright (c) 2014 César Rodas |
| Copyright (c) 2016 César Rodas |
+---------------------------------------------------------------------------------+
| Redistribution and use in source and binary forms, with or without |
| modification, are permitted provided that the following conditions are met: |
Expand Down Expand Up @@ -73,6 +73,43 @@ public static function dumpArray($path, Array $data, $perm = 0644)
self::write($path, "<?php return " . dump_array($data) . ';', $perm);
}

/**
* Similar to ::write but after writing the file content
* it will include the newest code and return its output.
*
* This function solves an HHVM issue that exists requiring
* the same file more than once in the same program runtime.
*
* It only works with pure PHP (not with mixed HTML and PHP), and it
* must begin with `<?php`.
*
*
* @param string $path File path
* @param string $code Source code
*
* @return mixed
*/
public static function writeAndInclude($path, $code)
{
self::write($path, $code);
if (defined('HHVM_VERSION')) {
// https://github.com/facebook/hhvm/issues/4797
chdir(dirname($path));
return eval(substr($code, 5));
}
return require $path;
}

/**
* Writes a file atomically, using a temporary and then moving
* the file.
*
* @param string $path File path to write
* @param string $content File content
* @param int $perm File permissions (default 0644)
*
* @return bool
*/
public static function write($path, $content, $perm = 0644)
{
$dir = dirname($path);
Expand Down
11 changes: 11 additions & 0 deletions tests/FileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,17 @@ public function testWrite()
unlink('foobar.php');
}

public function testWriteInclude()
{
$this->assertFalse(is_file('foobarxx.php'));
$this->AssertEquals(2, File::writeAndInclude("foobarxxx.php", "<?php return 2;"));
$this->AssertEquals(3, File::writeAndInclude("foobarxxx.php", "<?php return 3;"));
$this->AssertEquals(4, File::writeAndInclude("foobarxxx.php", "<?php return 4;"));
unlink('foobarxxx.php');
}



public function testFilePath()
{
$path = File::generateFilepath('activemongo');
Expand Down

0 comments on commit 6da9a84

Please sign in to comment.