├── README.md ├── _add-ons └── webhooks │ ├── default.yaml │ └── hooks.webhooks.php └── _config └── add-ons └── webhooks └── webhooks.yaml /README.md: -------------------------------------------------------------------------------- 1 | # Webhooks for Statamic 2 | > Need a way to do something after deploying (like clearing your cache)? Do it automatically! 3 | 4 | ## What happens? 5 | This is what can happen when you hit the trigger URL. 6 | 7 | * Clear your Statamic cache. (on by default) 8 | * Clear your PHP OpCache (on by default if it's installed) 9 | * Clear your [rendered HTML cache](http://statamic.com/learn/advanced-features/html-caching) (off by default) 10 | 11 | All of these are customizable in `_config/add-ons/webhooks/webhooks.yaml`. 12 | 13 | ## That's all? 14 | If you have suggestions or requests for other things to be performed automatically, [let me know on Twitter (@jason_varga)](https://twitter.com/jason_varga) or open a GitHub issue. 15 | 16 | ## Installation and Usage 17 | 18 | * Copy `_add-ons/webhooks/` to your `_add-ons` directory. 19 | * Copy `_config/add-ons/webhooks` to your `_config/add-ons` directory. 20 | * Add an `api_key` to `_config/add-ons/webhooks/webhooks.yaml`. This can be anything you want. 21 | * Optionally update the other values in `webhooks.yaml` to turn on/off functionality. 22 | 23 | To trigger the actions, just hit the following URL after deployment. 24 | 25 | http://yoursite.com/TRIGGER/webhooks/go?api_key=[YOUR_API_KEY] 26 | 27 | Replace `[YOUR_API_KEY]` with the `api_key` you created in the config file. -------------------------------------------------------------------------------- /_add-ons/webhooks/default.yaml: -------------------------------------------------------------------------------- 1 | clear_cache: yes 2 | clear_php_opcache: yes 3 | clear_html_caching: no -------------------------------------------------------------------------------- /_add-ons/webhooks/hooks.webhooks.php: -------------------------------------------------------------------------------- 1 | fetchConfig('api_key', Helper::getRandomString(), null, false, false)) { 13 | $app = \Slim\Slim::getInstance(); 14 | $app->halt(403, 'Invalid API key.'); 15 | } 16 | 17 | if ($this->config['clear_cache']) { 18 | // Clear the contents of Statamic's cache directory 19 | $this->clearStatamicCache(); 20 | } 21 | 22 | if ($this->config['clear_php_opcache'] && function_exists('opcache_reset')) { 23 | // Clear OpCache PHP cache storage installed as part of PHP5.5.* 24 | $this->clearOpCache(); 25 | } 26 | 27 | if ($this->config['clear_html_caching']) { 28 | // Clear rendered html cache 29 | $this->clearHtmlCache(); 30 | } 31 | 32 | if ($this->config['clear_tag_cache']) { 33 | // Clear {{ cache }} template tag cache 34 | $this->clearTagCache(); 35 | } 36 | } 37 | 38 | 39 | //--------------------------------------------- 40 | 41 | 42 | private function clearStatamicCache() 43 | { 44 | $app_cache_folder = BASE_PATH . '/_cache/_app/'; 45 | Folder::delete($app_cache_folder, true); 46 | 47 | $tag_cache_folder = BASE_PATH . '/_cache/_add-ons/cache/'; 48 | Folder::delete($tag_cache_folder, true); 49 | 50 | $this->log->info('Statamic\'s cache has been cleared.'); 51 | } 52 | 53 | private function clearOpCache() 54 | { 55 | opcache_reset(); 56 | $this->log->info('OpCache has been cleared.'); 57 | } 58 | 59 | private function clearHtmlCache() 60 | { 61 | $cache_folder = BASE_PATH . '/_cache/_add-ons/html_caching/'; 62 | Folder::delete($cache_folder, true); 63 | $this->log->info('Rendered HTML cache has been cleared.'); 64 | } 65 | 66 | private function clearTagCache() 67 | { 68 | $cache_folder = BASE_PATH . '/_cache/_add-ons/cache/'; 69 | Folder::delete($cache_folder, true); 70 | $this->log->info('Template tag cache has been cleared.'); 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /_config/add-ons/webhooks/webhooks.yaml: -------------------------------------------------------------------------------- 1 | # Define an API key to prevent unwanted usage 2 | api_key: 3 | 4 | # Clear Statamic Cache 5 | # clear_cache: 6 | 7 | # Clear PHP OPcache (PHP 5.5.x) 8 | # clear_php_opcache: 9 | 10 | # Clear rendered html cache 11 | # http://statamic.com/learn/advanced-features/html-caching 12 | # clear_html_caching: --------------------------------------------------------------------------------