Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for caching the changes in Code Files. #534

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 43 additions & 4 deletions src/CodeFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,16 @@ public function __construct($path, $content, $config = [])
$this->content = $content;
$this->id = md5($this->path);
if (is_file($path)) {
$this->operation = file_get_contents($path) === $content ? self::OP_SKIP : self::OP_OVERWRITE;
$origContent = file_get_contents($path);
if ($data = $this->getCache()) {
if ($content != $data['content'] or $origContent != $data['original']) {
$this->operation = self::OP_OVERWRITE;
} else {
$this->operation = self::OP_SKIP;
}
} else {
$this->operation = $origContent === $content ? self::OP_SKIP : self::OP_OVERWRITE;
}
} else {
$this->operation = self::OP_CREATE;
}
Expand Down Expand Up @@ -99,6 +108,7 @@ public function save()
if (@file_put_contents($this->path, $this->content) === false) {
return "Unable to write the file '{$this->path}'.";
}
$this->setCache();

if ($module instanceof \yii\gii\Module) {
$mask = @umask(0);
Expand Down Expand Up @@ -146,10 +156,11 @@ public function preview()
$type = 'unknown';
}

$data = $this->getCache();
if ($type === 'php') {
return highlight_string($this->content, true);
return highlight_string($data ? $data['original'] : $this->content, true);
} elseif (!in_array($type, ['jpg', 'gif', 'png', 'exe'])) {
return nl2br(Html::encode($this->content));
return nl2br(Html::encode($data ? $data['original'] : $this->content));
}

return false;
Expand All @@ -166,7 +177,18 @@ public function diff()
if (in_array($type, ['jpg', 'gif', 'png', 'exe'])) {
return false;
} elseif ($this->operation === self::OP_OVERWRITE) {
return $this->renderDiff(file($this->path), $this->content);
$html = '';
if ($data = $this->getCache()) {
$html .= '<h3>Diff: Old and New Table</h3>';
$html .= ($this->renderDiff($data['content'], $this->content) ?: 'Identical.');
$html .= '<hr/>';
$html .= '<h3>Diff: Old and New File</h3>';
$html .= ($this->renderDiff($data['original'], file_get_contents($this->path)) ?: 'Identical.');
$html .= '<hr/>';
}
$html .= '<h3>Diff: File and Table</h3>';
$html .= ($this->renderDiff(file($this->path), $this->content) ?: 'Identical.');
return $html;
}

return '';
Expand Down Expand Up @@ -199,4 +221,21 @@ private function renderDiff($lines1, $lines2)

return $diff->render($renderer);
}

public function setCache()
{
$cache = Yii::$app->cache;
$key = $this->id;
$cache->set($key, [
'content' => $this->content,
'original' => strval(is_file($this->path) ? file_get_contents($this->path) : $this->content),
]);
}

public function getCache()
{
$cache = Yii::$app->cache;
$key = $this->id;
return $cache->get($key);
}
}
15 changes: 15 additions & 0 deletions src/controllers/DefaultController.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,21 @@ public function actionDiff($id, $file)
throw new NotFoundHttpException("Code file not found: $file");
}

public function actionCacheIt($id, $file)
{
$generator = $this->loadGenerator($id);
if ($generator->validate()) {
foreach ($generator->generate() as $f) {
if ($f->id === $file) {
$f->setCache();
$data = $f->getCache();
return ($data ? 'success' : 'fail');
}
}
}
throw new NotFoundHttpException("Code file not found: $file");
}

/**
* Runs an action defined in the generator.
* Given an action named "xyz", the method "actionXyz()" in the generator will be called.
Expand Down
38 changes: 37 additions & 1 deletion src/views/default/view/files.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
<thead>
<tr>
<th class="file">Code File</th>
<th class="cache">Cache It</th>
<th class="action">Action</th>
<?php
$fileChangeExists = false;
Expand Down Expand Up @@ -70,6 +71,11 @@
<?= Html::a('diff', ['diff', 'id' => $id, 'file' => $file->id], ['class' => 'diff-code badge badge-warning', 'data-title' => $file->getRelativePath()]) ?>
<?php endif; ?>
</td>
<td colspan="cache">
<?php if (!$file->getCache() or $file->operation !== CodeFile::OP_SKIP): ?>
<?= Html::a('cache it', ['cache-it', 'id' => $id, 'file' => $file->id], ['class' => 'cache-it badge badge-primary', 'data-hideit' => intval($file->operation != CodeFile::OP_CREATE)]) ?>
<?php endif; ?>
</td>
<td class="action">
<?php
if ($file->operation === CodeFile::OP_SKIP) {
Expand All @@ -94,7 +100,37 @@
<?php endforeach; ?>
</tbody>
</table>

<?php
$this->registerJs(<<<'NOWDOC'
$(function (){
$('.cache-it').click(function (){
let $that = $(this);
let $cbox = $that.closest('tr').find('input[type="checkbox"]');
let $diff = $that.closest('tr').find('.diff-code');
let $actn = $that.closest('tr').find('.action');
let hideit = $that.data('hideit');
let url = $(this).attr('href');
ajaxRequest = $.ajax({
type: 'POST',
cache: false,
url: url,
data: $('.default-view form').serializeArray(),
success: function (data) {
if (data === 'success') {
$that.remove();
$diff.remove();
if (hideit) {
$cbox.remove();
$actn.text('cached');
}
}
},
});
return false;
});
});
NOWDOC, $this::POS_END);
?>
<div class="modal fade" id="preview-modal" tabindex="-1" role="dialog">
<div class="modal-dialog modal-lg">
<div class="modal-content">
Expand Down