Skip to content

Commit 667d1bb

Browse files
author
Alannah Kearney
committed
Added cache command for use with the Symphony Shell extension
1 parent f268539 commit 667d1bb

1 file changed

Lines changed: 79 additions & 0 deletions

File tree

bin/cache

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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

Comments
 (0)