|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Symphony\Shell\Command\Api_Framework; |
| 4 | + |
| 5 | +use Symphony\Shell\Lib\Traits; |
| 6 | +use Symphony\Shell\Lib\Shell as Shell; |
| 7 | +use Symphony\ApiFramework\Lib\Models\PageCache; |
| 8 | + |
| 9 | +use SymphonyPDO; |
| 10 | +use Extension_API_Framework; |
| 11 | + |
| 12 | +class Cache extends \Symphony\Shell\Lib\AuthenticatedCommand |
| 13 | +{ |
| 14 | + use Traits\hasRequiresAuthenticationTrait; |
| 15 | + |
| 16 | + const ACTION_CLEAN = "clean"; |
| 17 | + const ACTION_PURGE = "purge"; |
| 18 | + |
| 19 | + public function usage() |
| 20 | + { |
| 21 | + echo "usage: cache [OPTION...] |
| 22 | + Maintentance script for API Framework cache entries |
| 23 | + |
| 24 | +options: |
| 25 | + |
| 26 | + --action | -a |
| 27 | + The action to run on the cache entries. See 'Actions' below. |
| 28 | + |
| 29 | +actions: |
| 30 | + |
| 31 | + clean |
| 32 | + Removes expired cache entries |
| 33 | + |
| 34 | + purge |
| 35 | + Deletes all cache entries |
| 36 | + |
| 37 | +examples: |
| 38 | + symphony -c api_framework/cache -a purge |
| 39 | + symphony -c api_framework/cache --action=clean |
| 40 | + |
| 41 | +"; |
| 42 | + } |
| 43 | + |
| 44 | + public function run() |
| 45 | + { |
| 46 | + |
| 47 | + if (Shell::instance()->args->find(['a', 'action']) === false) { |
| 48 | + throw new \Exception('No action specified. Must use -a or --action.'); |
| 49 | + |
| 50 | + } elseif(!in_array(Shell::instance()->args->find(['a', 'action'])->value(), [self::ACTION_CLEAN, self::ACTION_PURGE])) { |
| 51 | + throw new \Exception('Invalid action specified.'); |
| 52 | + } |
| 53 | + |
| 54 | + switch(Shell::instance()->args->find(['a', 'action'])->value()) { |
| 55 | + case self::ACTION_CLEAN: |
| 56 | + $cacheEntries = PageCache::fetchExpired(); |
| 57 | + break; |
| 58 | + |
| 59 | + case self::ACTION_PURGE: |
| 60 | + $cacheEntries = PageCache::all(); |
| 61 | + break; |
| 62 | + |
| 63 | + } |
| 64 | + |
| 65 | + Shell::message(sprintf('%d cache entries located.', $cacheEntries->count())); |
| 66 | + |
| 67 | + if ($cacheEntries->count() <= 0) { |
| 68 | + Shell::message('Nothing to do. Exiting.', false); |
| 69 | + return; |
| 70 | + } |
| 71 | + |
| 72 | + foreach($cacheEntries as $c) { |
| 73 | + $c->delete(); |
| 74 | + } |
| 75 | + |
| 76 | + Shell::message('Done.'); |
| 77 | + } |
| 78 | + |
| 79 | +} |
0 commit comments