Created
August 18, 2014 06:45
-
-
Save kookxiang/6f6e95c057b175297de1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class PHPLock { | |
private $_PHP_LOCK = array(); | |
private $code; | |
private $lockId = 1; | |
public function __construct(&$code){ | |
$this->code =& $code; | |
$this->_PHP_LOCK = array(); | |
} | |
public function acquire(){ | |
$this->code = preg_replace_callback('/<\?php.+?\?>/is', array($this, '_lockContent'), $this->code); | |
} | |
public function release(){ | |
foreach($this->_PHP_LOCK as $LOCK_ID => $sourceCode){ | |
$this->code = str_replace($LOCK_ID, $sourceCode, $this->code); | |
} | |
$this->_PHP_LOCK = array(); | |
} | |
private function _lockContent($matches){ | |
$LOCK = '___PHP_CODE_LOCK_'.($this->lockId++).'___'; | |
$this->_PHP_LOCK[ $LOCK ] = $matches[0]; | |
return $LOCK; | |
} | |
public function syncContent($newCode){ | |
$this->code = $newCode; | |
return $this; | |
} | |
public function getContent(){ | |
return $this->code; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment