Initial commit.
This commit is contained in:
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
/.idea
|
96
Plugin.php
Normal file
96
Plugin.php
Normal file
@ -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
Normal file
90
README.md
Normal file
@ -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
Normal file
26
classes/observer/BaseObserver.php
Normal file
@ -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
Normal file
6
classes/observer/Observer.php
Normal file
@ -0,0 +1,6 @@
|
||||
<?php namespace Wpstudio\Helpers\Classes\Observer;
|
||||
|
||||
interface Observer
|
||||
{
|
||||
public static function getClass(): string;
|
||||
}
|
20
composer.json
Normal file
20
composer.json
Normal file
@ -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
Normal file
1
updates/version.yaml
Normal file
@ -0,0 +1 @@
|
||||
1.0.1: First version of helpers
|
Reference in New Issue
Block a user