Browse Source

Initial commit.

master
dimti 1 year ago
commit
3a439538ca
  1. 1
      .gitignore
  2. 96
      Plugin.php
  3. 90
      README.md
  4. 26
      classes/observer/BaseObserver.php
  5. 6
      classes/observer/Observer.php
  6. 20
      composer.json
  7. 1
      updates/version.yaml

1
.gitignore

@ -0,0 +1 @@
/.idea

96
Plugin.php

@ -0,0 +1,96 @@
<?php namespace Wpstudio\Helpers;
use Backend;
use System\Classes\PluginBase;
/**
* helpers Plugin Information File
*/
class Plugin extends PluginBase
{
/**
* Returns information about this plugin.
*
* @return array
*/
public function pluginDetails()
{
return [
'name' => 'helpers',
'description' => 'No description provided yet...',
'author' => 'wpstudio',
'icon' => 'icon-leaf'
];
}
/**
* Register method, called when the plugin is first registered.
*
* @return void
*/
public function register()
{
}
/**
* Boot method, called right before the request route.
*
* @return array
*/
public function boot()
{
}
/**
* Registers any front-end components implemented in this plugin.
*
* @return array
*/
public function registerComponents()
{
return []; // Remove this line to activate
return [
'Wpstudio\Helpers\Components\MyComponent' => 'myComponent',
];
}
/**
* Registers any back-end permissions used by this plugin.
*
* @return array
*/
public function registerPermissions()
{
return []; // Remove this line to activate
return [
'wpstudio.helpers.some_permission' => [
'tab' => 'helpers',
'label' => 'Some permission'
],
];
}
/**
* Registers back-end navigation items for this plugin.
*
* @return array
*/
public function registerNavigation()
{
return []; // Remove this line to activate
return [
'helpers' => [
'label' => 'helpers',
'url' => Backend::url('wpstudio/helpers/mycontroller'),
'icon' => 'icon-leaf',
'permissions' => ['wpstudio.helpers.*'],
'order' => 500,
],
];
}
}

90
README.md

@ -0,0 +1,90 @@
# Observers
Observers maybe used for creating and clearing cache purposes
## Example observer use
Create you own class with observer:
```php
<?php namespace Dimti\Mirsporta\Classes\Observers;
use Dimti\Mirsporta\Models;
use Wpstudio\Helpers\Classes\Observer\BaseObserver;
class TagObserver extends BaseObserver
{
public static function getClass(): string
{
return Models\Tag::class;
}
}
```
Use in models:
```php
<?php namespace Dimti\Mirsporta\Models;
use Dimti\Mirsporta\Classes\Observers;
class Tag extends Model
{
//...
public function afterSave()
{
Observers\TagObserver::clearCache();
}
//...
}
```
And in your code for caching data based on this model/observer:
```php
use Dimti\Mirsporta\Classes\Observers;
//...
$cacheTags = Observers\TagObserver::getCacheTag();
$cacheKey = CacheManager::formatCacheKey([__CLASS__, __METHOD__, $siteId]);
\Cache::tags($cacheTags)->rememberForever($cacheKey, fn() =>
Models\Tag::all()
));
```
* CacheManager::formatCacheKey - that is simple helper for concatenate array elements to string
## Clearing cache from admin dashboard widget
### Template
```html
<input
type="button"
class='btn btn-primary'
value="Clear tag model cache"
data-request="<?= $this->getEventHandler('onClearTagCache') ?>"
data-request-success="$.oc.flashMsg({text: 'Cache of tag model is clearing', 'class': 'success', 'interval': 5}); return false;"
/>
```
### Controller widget action
```php
use Dimti\Mirsporta\Classes\Observers;
//...
public function onClearTagCache()
{
Observers\TagObserver::clearCache();
return ['status' => 1];
}
//...
```

26
classes/observer/BaseObserver.php

@ -0,0 +1,26 @@
<?php namespace Wpstudio\Helpers\Classes\Observer;
abstract class BaseObserver implements Observer
{
protected static string $class;
/**
* Очистка кэша
* @return void
*/
public static function clearCache(): void
{
$tags = [self::getCacheTag()];
\Cache::tags($tags)->flush();
}
/**
* Получение тэка кеша
* @return string
*/
public static function getCacheTag(): string
{
return static::getClass();
}
}

6
classes/observer/Observer.php

@ -0,0 +1,6 @@
<?php namespace Wpstudio\Helpers\Classes\Observer;
interface Observer
{
public static function getClass(): string;
}

20
composer.json

@ -0,0 +1,20 @@
{
"name": "wpstudio/helpers-plugin",
"type": "october-plugin",
"description": "Some extends models & controllers",
"require": {
"composer/installers": "~1.0"
},
"keywords": [
"models",
"api",
"observer"
],
"license": "MIT",
"authors": [
{
"name": "WP Studio Team",
"email": "info@wpstudio.ru"
}
]
}

1
updates/version.yaml

@ -0,0 +1 @@
1.0.1: First version of helpers
Loading…
Cancel
Save