Skip to content

Instantly share code, notes, and snippets.

@kookxiang
Created August 18, 2014 06:45
Show Gist options
  • Save kookxiang/6f6e95c057b175297de1 to your computer and use it in GitHub Desktop.
Save kookxiang/6f6e95c057b175297de1 to your computer and use it in GitHub Desktop.
<?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