public function actionSave($class) { $postdata = file_get_contents("php://input"); $post = CJSON::decode($postdata); if (isset($post['list'])) { MenuTree::cleanMenuItems($post['list']); $options = MenuTree::getOptions($class); $code = "<?php \n\n" . MenuTree::OPTIONS_COMMENT_START . "\n\$options = " . FormBuilder::formatCode($options, '') . ";\n" . MenuTree::OPTIONS_COMMENT_END . "\n\nreturn " . FormBuilder::formatCode($post['list'], '') . ";"; file_put_contents(Yii::getPathOfAlias($class) . ".php", $code); } }
/** * @param string $functionName * @param array $fields Parameter * @param array $class Parameter * @param array $replaceString * @return field Fungsi ini digunakan untuk update model. */ public function updateFunctionBody($functionName, $fields, $class = "", $replaceString = null) { if ($class == "") { $using_another_class = false; $class = get_class($this->model); } else { $using_another_class = true; } ## get class data $isNewFunc = false; $sourceFile = ''; extract($this->getLineOfClass($class, $functionName)); if (is_array($fields)) { $fields = FormBuilder::formatCode($fields); ## replace multiline string to preserve indentation if (is_array($replaceString)) { $fields = str_replace(array_keys($replaceString), $replaceString, $fields); } ## generate function $func = <<<EOF public function {$functionName}() { return {$fields}; } EOF; } else { $func = $fields; } ## put function to class array_splice($file, $line, $length, explode("\n", $func)); ## adjust other methods line and length $newlength = count(explode("\n", $func)); foreach ($this->methods as $k => $m) { if (@$m['line'] >= $line && $k != $functionName) { if (!$isNewFunc) { $this->methods[$k]['line'] -= $length; } $this->methods[$k]['line'] += $newlength; } } $this->methods[$functionName]['length'] = $newlength; $this->methods[$functionName]['line'] = $line; $this->file = $file; $fp = @fopen($sourceFile, 'r+'); if (!$fp) { return false; } ## write new function to sourceFile if (flock($fp, LOCK_EX)) { // acquire an exclusive lock ftruncate($fp, 0); // truncate file $buffer = implode("\n", $file); //TODO: fix gigantic bug, do not allow more than 200 consecutive spaces $buffer = preg_replace('/\\s{200,}/', ' ', $buffer); $buffer = str_replace('\\\\\'', '\'', $buffer); $file = explode("\n", $buffer); fwrite($fp, $buffer); fflush($fp); // flush output before releasing the lock flock($fp, LOCK_UN); // release the lock Yii::app()->session['FormBuilder_' . $this->originalClass] = ['sourceFile' => $this->sourceFile, 'file' => $file, 'methods' => $this->methods, 'timestamp' => $this->timestamp]; return true; } else { echo "ERROR: Couldn't lock source file '{$sourceFile}'!"; die; } if (!$using_another_class) { ## update model instance $this->model = new $class(); } }
public static function setOptions($alias, $options) { $path = Asset::resolveAlias($alias . ".php"); if (isset($path)) { $file = file($path); $foundPHP = false; $startPHP = 0; $startLine = false; $lineLength = false; foreach ($file as $k => $f) { $tf = trim($f); if (!$foundPHP) { if (substr($tf, 0, 5) == "<?php") { $foundPHP = true; $startPHP = $k + 1; } } else { if ($tf != "") { if (!$startLine) { if ($tf == MenuTree::OPTIONS_COMMENT_START) { $startLine = $k + 1; } } else { if ($startLine && !$lineLength) { if ($tf == MenuTree::OPTIONS_COMMENT_END) { $lineLength = $k - $startLine; } } } } } } $optionsCode = explode("\n", '$options = ' . FormBuilder::formatCode($options, "")); $optionsCode[count($optionsCode) - 1] = $optionsCode[count($optionsCode) - 1] . ";"; if (!$startLine) { array_unshift($optionsCode, MenuTree::OPTIONS_COMMENT_START); array_push($optionsCode, MenuTree::OPTIONS_COMMENT_END); $lineLength = 0; $startLine = $startPHP; } foreach ($optionsCode as $k => $o) { $optionsCode[$k] = $optionsCode[$k] . "\n"; } array_splice($file, $startLine, $lineLength, $optionsCode); return implode("", $file); } }