项目作者: sergant210

项目描述 :
Middlewares emulator for MODX Revolution.
高级语言: PHP
项目地址: git://github.com/sergant210/Middlewares.git
创建时间: 2017-09-09T09:47:18Z
项目社区:https://github.com/sergant210/Middlewares

开源协议:

下载


Middlewares

It’s a middlewares emulator for MODx Revolution based on classes. But not only. In addition you can use class-based listeners instead of usual plugins.

Middlewares

Middlewares can be used to handle the request, and also allow you to perform certain actions after the response is prepared.
https://modzone.ru/assets/images/documentation/middlewares_thumb.jpg

Middlewares are based on classes. They can be global (fires on every request) or custom (fires only for specified resources). The global middlewares are specified in the “middlewares_global_middlewares” system setting. The custom ones must be set in a resource TV with name “middlewares”.
The middleware class has 3 methods:

  • onRequest - called on the OnMODXInit event.
  • beforeResponse - called on the OnWebPagePrerender event.
  • afterResponse - called on the OnWebPageComplete event.
Usage

Let’s create a global middleware. Create a file in the folder core/middlewares/. Let’s call it global.php.

  1. <?php
  2. // global.php
  3. class GlobalMiddleware extends Middlewares\Middleware
  4. {
  5. public function onRequest()
  6. {
  7. $this->modx->log(1, 'Save this message in the error log.');
  8. }
  9. public function beforeResponse()
  10. {
  11. // Change the output
  12. $this->modx->resource->_output = 'New content for output';
  13. }
  14. public function afterResponse() {}
  15. }
  16. // If the class name and file name do not match, then you need to return the class name.
  17. return 'GlobalMiddleware';

In the next step we need to specify the file name without extension (“global”) in the “middlewares_global_middlewares” system setting. That’s all.

The “contexts” property is intended to specify contexts in which the middleware will be work.

  1. // By default the web context is set.
  2. public $contexts = array('web');

Specify an empty array to ignore context checking.

Run a middleware manually
  1. $class = app('MiddlewareService')->loadMiddlewareClass('CsrfMiddleware');
  2. (new $class($this->modx))->onRequest();

Listeners

Listeners (event handlers) like middlewares are classes that are located in the folder specified in the “listeners_path” system settings. By default “core/listeners”.

How to use it

Create a file in the corresponding folder. Now you need to create a public method and give it the name of the corresponding MODX event.

  1. <?php
  2. // ListenerManager.php
  3. class ListenerManager extends Middlewares\Listener
  4. {
  5. public $contexts = ['web', 'mgr'];
  6. public function OnHandleRequest()
  7. {
  8. $this->modx->log(1, '[ListenerManager] OnHandleRequest event is fired!';
  9. $this->modx->regClientScript('<script>alert("OnHandleRequest");</script>');
  10. }
  11. public function OnBeforeManagerPageInit()
  12. {
  13. $this->modx->controller->addHtml('<script>alert("OnBeforeManagerPageInit");</script>');
  14. }
  15. public function OnBeforeDocFormSave($properties)
  16. {
  17. extract($properties);
  18. if (empty($resource->longtitle)) {
  19. $this->modx->event->output('['Long title is required!'); // to modal window
  20. $this->modx->log(1, '[ListenerManager] Failed to save page id '.$id.' due to missing longtitle'; // to the error log
  21. }
  22. }
  23. }
  24. // If the class name and file name do not match, then you need to return the class name.
  25. // In this case they are the same. So no need to return it.
  26. // return 'ListenerManager';

Put the file name in the “middlewares_listeners” system setting. Or you can do it in the global middleware:

  1. // global.php
  2. class GlobalMiddleware extends Middlewares\Middleware
  3. {
  4. public function onRequest()
  5. {
  6. $this->modx->setOption('middlewares_listeners', 'ListenerManager');
  7. }
  8. }
  9. return 'GlobalMiddleware';

if you want to run your listeners before any MODX plugins, specify the argument “before” of the corresponding method:

  1. public function OnHandleRequest($before=true)
  2. {
  3. $this->modx->log(1, '[ListenerManager] OnHandleRequest event is fired!');
  4. $this->modx->regClientScript('<script>alert("OnHandleRequest");</script>');
  5. }
  6. public function OnBeforeDocFormSave($properties, $before=true)
  7. {
  8. extract($properties);
  9. if (empty($resource->longtitle)) {
  10. $this->modx->event->output('Long title is required!'); // to modal window
  11. $this->modx->log(1, '[ListenerManager] Failed to save page id '.$id.' due to missing longtitle'!); // to the error log
  12. }
  13. }

So in most cases you can refuse to use usual MODX plugins.

Example files

You can find a middleware file in the core/middlewares directory and a listener file in the core/listeners directory. The middleware is ready for use right after installation.

System settings

  • middlewares_path - path to middlewares’ classes. By-default “{core_path}middlewares/“.
  • middlewares_lpath - path to listeners’ classes. By-default, “{core_path}listeners/“.
  • middlewares_global_middlewares - comma separated names of middlewares.
  • middlewares_listeners - comma separated names of listeners.