Просмотр исходного кода

A simple module to try on caching external requests

ahwelp 2 лет назад
Родитель
Сommit
78bd7cc06d

+ 107 - 0
app/core/cache/CacheController.php

@@ -0,0 +1,107 @@
+<?php
+
+namespace App\Core\Cache;
+
+use \App\Core\Template\Output as Output;
+use \App\Core\Cache\Classes\Cache as Cache;
+use RR\Response;
+
+class CacheController extends \DefaultController
+{
+
+    protected $_class   = Cache::class;
+    protected $_baseUrl = '/cache';
+
+    /**
+     * Index
+     *   Show the main Cache list
+     */
+    function index()
+    {
+        Output::render('index', array('list' => Cache::findAll()));
+    }
+
+    /**
+     * Create
+     *
+     * Render the main Cache formulary
+     */
+    function create()
+    {
+        Output::render('form', ['id' => 0]);
+    }
+
+    /**
+     * Store
+     *
+     * Store the param on the database
+     * @param Cache $cache
+     */
+    function store(Cache $cache)
+    {
+    }
+
+    /**
+     * Search
+     *
+     * Store the param on the database
+     * @param Cache $cache
+     */
+    function search()
+    {
+    }
+
+    /**
+     * Show
+     *
+     * Render one register
+     *
+     * @param Cache $cache
+     */
+    function show(Cache $cache)
+    {
+        Response::json(stripslashes($cache->value))->send();
+    }
+
+    /**
+     * Edit
+     *
+     * Render the formular for a database Cache
+     *
+     * @param Cache $cache
+     */
+    function edit(Cache $cache)
+    {
+        Output::render('form', $cache);
+    }
+
+    /**
+     * Update
+     * Store the changes of the param on the database
+     *
+     * @param Cache $cache
+     */
+    function update(Cache $cache)
+    {
+    }
+
+    /**
+     * Destroy
+     * If the object has soft delete.
+     *
+     * @param Cache $cache
+     */
+    function destroy(Cache $cache)
+    {
+    }
+
+    /**
+     * Purge
+     * Remove object even with soft delete.
+     *
+     * @param Cache $cache
+     */
+    function purge(Cache $cache)
+    {
+    }
+}

+ 23 - 0
app/core/cache/Routes.php

@@ -0,0 +1,23 @@
+<?php
+
+use App\Core\Template\Output as Output;
+use Routes\RouteCollection as RouteCollection;
+use ORM\DBInstance as DB;
+use App\Core\Cache\Classes\Cache as Cache;
+//Menu itens
+RouteCollection::get('*', function () {
+    Output::addOnSubmenu('dev', '/cache', "Cache", "", ['class' => 'nav-link']);
+}, -10)->doIgnore();
+
+RouteCollection::group("/cache", function () {
+    RouteCollection::get("/",            "\App\Core\Cache\CacheController@index");
+    RouteCollection::get("/table",       "\App\Core\Cache\CacheController@table");
+    RouteCollection::post("/table",       "\App\Core\Cache\CacheController@searchTable");
+    RouteCollection::get("/form",        "\App\Core\Cache\CacheController@create");
+    RouteCollection::post("/",            "\App\Core\Cache\CacheController@store");
+    RouteCollection::post("/search",      "\App\Core\Cache\CacheController@search");
+    RouteCollection::get("/[i:id]",      "\App\Core\Cache\CacheController@show");
+    RouteCollection::get("/[i:id]/edit", "\App\Core\Cache\CacheController@edit");
+    RouteCollection::put("/[i:id]/edit", "\App\Core\Cache\CacheController@update");
+    RouteCollection::delete("/[i:id]",      "\App\Core\Cache\CacheController@destroy");
+});

+ 37 - 0
app/core/cache/classes/Cache.php

@@ -0,0 +1,37 @@
+<?php
+
+namespace App\Core\Cache\Classes;
+
+use ORM\Entity as Entity;
+
+class Cache extends Entity {
+
+    //const _idPolice = Array("type" => "", "min" => 0, "max" => 100000, "step" => 2);
+
+    const _tableName = "core_caches";
+    const _timestamps = true;
+
+    const _properties = Array(
+        'id',
+        'name',
+        'description'
+    );
+
+    const _listable = Array(
+        'id',
+        'name'
+    );
+
+    const _searchable = Array(
+        'name'
+    );
+
+    const _orderable = Array(
+
+    );
+
+
+    //const _softdelete = true;
+
+    //const _connectionName = "";
+}

+ 30 - 0
app/core/cache/db/Migrate.php

@@ -0,0 +1,30 @@
+<?php
+
+use Schema\Wrapper as Wrapper;
+use App\Core\Sanity\MigratorController as Migrator;
+
+//https://book.cakephp.org/phinx/0/en/migrations.html#valid-column-types
+function core_cache_upgrade($pluginversion) {
+    if ($pluginversion < "0.0.1") {
+        $table = Wrapper::get_table("core_caches");
+        $table->addColumn("key", "string", Array("null" => false));
+        $table->addColumn("type", "string", Array("null" => false));
+        $table->addColumn("format", "string", Array("null" => false));
+        $table->addColumn("value", "text", Array("null" => true));
+
+        $table->addTimestamps();
+        $table->create();
+
+        Migrator::getInstance()->update_plugin_version("core_cache", "1.0.0");
+        return;
+    }
+
+}
+
+function core_cache_rollback($pluginversion) {
+    if($pluginversion > "0.0.1"){
+        $table = Wrapper::get_table("core_caches");
+        $table->drop();
+        return;
+    }
+}

+ 2 - 0
app/core/cache/lang/en.php

@@ -0,0 +1,2 @@
+<?php
+    $lang["cache"]["module_name"] = "Cache";

+ 2 - 0
app/core/cache/lang/pt_br.php

@@ -0,0 +1,2 @@
+<?php
+    $lang["cache"]["module_name"] = "Cache";

+ 0 - 0
app/core/cache/todo.md


+ 4 - 0
app/core/cache/version.php

@@ -0,0 +1,4 @@
+<?php
+
+$plugin->name = "core_cache";
+$plugin->version = "1.0.0";

+ 62 - 0
app/core/cache/views/form.mustache

@@ -0,0 +1,62 @@
+{{#id}}
+<form method="POST" action="/cache/{{id}}/edit">
+   <input type="hidden" name="id" value="{{id}}" />
+   <input type="hidden" name="_method" value="put" />
+{{/id}}
+{{^id}}
+<form method="POST" action="/cache">
+   <input type="hidden" name="id" value="" />
+{{/id}}
+    <div class='row'>
+        <div class="col-md-8 col-xs-12 g-3">
+            <div class="card">
+                <div class="card-header">Header</div>
+                <div class="card-body">
+                    <div class="row g-3">
+                        <div class="col-xs-12 col-3">
+                            <label class="form-label">Código</label>
+                            <input type="text" class="form-control" name='code' value="{{code}}">
+                        </div>
+                        <div class="col-xs-12 col-9">
+                            <label class="form-label">Nome</label>
+                            <input type="text" class="form-control" name='name' value="{{name}}">
+                        </div>
+                    </div>
+
+                    <div class="row">
+                        <div class="col-xs-12 g-3">
+                            <label class="form-label">Descrição</label>
+                            <textarea class="form-control" name="descricao">{{description}}</textarea>
+                        </div>
+                    </div>
+                </div>
+            </div>
+        </div>
+    </div>
+    <div class='row'>
+        <div class="col-md-8 col-xs-12 g-3">
+            <div class="card">
+                <div class="card-header">Header</div>
+                <div class="card-body">
+                    <div class="row g-3">
+                        <div class="col-xs-12 col-3">
+                            <label class="form-label">Código</label>
+                            <input type="text" class="form-control" name='code' value="{{code}}">
+                        </div>
+                        <div class="col-xs-12 col-9">
+                            <label class="form-label">Nome</label>
+                            <input type="text" class="form-control" name='name' value="{{name}}">
+                        </div>
+                    </div>
+
+                    <div class="row">
+                        <div class="col-xs-12 g-3">
+                            <label class="form-label">Descrição</label>
+                            <textarea class="form-control" name="descricao">{{description}}</textarea>
+                        </div>
+                    </div>
+                </div>
+            </div>
+        </div>
+    </div>
+</form>

+ 39 - 0
app/core/cache/views/index.mustache

@@ -0,0 +1,39 @@
+<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/11.7.0/styles/default.min.css">
+<script src="//cdnjs.cloudflare.com/ajax/libs/highlight.js/11.7.0/highlight.min.js"></script>
+
+<style>
+    .highlight {
+        padding: 20px;
+        background-color: #f8f9fa;
+    }
+</style>
+
+<table class="table table-responsive">
+    {{#list}}
+    <tr id="{{id}}">
+        <td>{{id}}</td>
+        <td>{{key}}</td>
+        <td>{{type}}</td>
+        <td>{{format}}</td>
+        <td>{{created_at}}</td>
+    </tr>
+    {{/list}}
+</table>
+
+<pre>
+    <code id="canvas" class="language-json"></code>
+</pre>
+
+<script>
+    // https://highlightjs.org/download/
+    // https://www.tutorialspoint.com/how-to-format-json-string-in-javascript
+    $('tr').on('click', function (e) {
+        let id = $(this).attr('id');
+        fetch("/cache/" + id)
+            .then(response => response.json())
+            .then(function (data) {
+                $('#canvas').html(JSON.stringify(data, null, 4));
+                hljs.highlightAll();
+            });
+    });
+</script>