本文整理汇总了PHP中FormBuilder::formatCode方法的典型用法代码示例。如果您正苦于以下问题:PHP FormBuilder::formatCode方法的具体用法?PHP FormBuilder::formatCode怎么用?PHP FormBuilder::formatCode使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类FormBuilder
的用法示例。
在下文中一共展示了FormBuilder::formatCode方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: actionSave
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);
}
}
示例2: updateFunctionBody
/**
* @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();
}
}
示例3: setOptions
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);
}
}