Skip to content

Commit 7d54d27

Browse files
authored
Cache clear (phpactor#307)
* Use test util * Clear cache * Updated plugin * Fixed benchmarks * Updated composer
1 parent b6734f6 commit 7d54d27

26 files changed

+277
-51
lines changed

composer.lock

Lines changed: 32 additions & 30 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/Application/CacheClear.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace Phpactor\Application;
4+
5+
use Symfony\Component\Filesystem\Filesystem;
6+
use Webmozart\PathUtil\Path;
7+
8+
class CacheClear
9+
{
10+
/**
11+
* @var string
12+
*/
13+
private $cachePath;
14+
15+
/**
16+
* @var Filesystem
17+
*/
18+
private $filesystem;
19+
20+
public function __construct(string $cachePath)
21+
{
22+
$this->cachePath = Path::canonicalize($cachePath);
23+
$this->filesystem = new Filesystem();
24+
}
25+
26+
public function clearCache()
27+
{
28+
$this->filesystem->remove($this->cachePath);
29+
}
30+
31+
public function cachePath()
32+
{
33+
return $this->cachePath;
34+
}
35+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace Phpactor\Console\Command;
4+
5+
use Symfony\Component\Console\Command\Command;
6+
use Symfony\Component\Console\Input\InputInterface;
7+
use Symfony\Component\Console\Output\OutputInterface;
8+
use Phpactor\Application\CacheClear;
9+
10+
class CacheClearCommand extends Command
11+
{
12+
/**
13+
* @var CacheClear
14+
*/
15+
private $cache;
16+
17+
public function __construct(CacheClear $cache)
18+
{
19+
parent::__construct();
20+
$this->cache = $cache;
21+
}
22+
23+
protected function configure()
24+
{
25+
$this->setName('cache:clear');
26+
$this->setDescription('Clear the cache');
27+
}
28+
29+
protected function execute(InputInterface $input, OutputInterface $output)
30+
{
31+
$this->cache->clearCache();
32+
$output->writeln(sprintf('<info>Cache cleared: </>%s', $this->cache->cachePath()));
33+
}
34+
}

lib/Container/CoreExtension.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@
4545
use Monolog\Handler\StreamHandler;
4646
use Monolog\Handler\FingersCrossedHandler;
4747
use Phpactor\ClassFileConverter\PathFinder;
48+
use Phpactor\Application\CacheClear;
49+
use Phpactor\Console\Command\CacheClearCommand;
4850

4951
class CoreExtension implements ExtensionInterface
5052
{
@@ -187,6 +189,12 @@ private function registerConsole(Container $container)
187189
);
188190
}, [ 'ui.console.command' => []]);
189191

192+
$container->register('command.cache_clear', function (Container $container) {
193+
return new CacheClearCommand(
194+
$container->get('application.cache_clear')
195+
);
196+
}, [ 'ui.console.command' => []]);
197+
190198
// ---------------
191199
// Dumpers
192200
// ---------------
@@ -401,6 +409,12 @@ private function registerApplicationServices(Container $container)
401409
);
402410
});
403411

412+
$container->register('application.cache_clear', function (Container $container) {
413+
return new CacheClear(
414+
$container->getParameter(self::CACHE_DIR)
415+
);
416+
});
417+
404418
$container->register('application.helper.class_file_normalizer', function (Container $container) {
405419
return new ClassFileNormalizer($container->get('class_to_file.converter'));
406420
});

lib/Container/RpcExtension.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
use Phpactor\Rpc\RequestHandler\LoggingHandler;
2828
use Phpactor\Rpc\Handler\NavigateHandler;
2929
use Phpactor\Rpc\Handler\OverrideMethodHandler;
30+
use Phpactor\Rpc\Handler\CacheClearHandler;
3031

3132
class RpcExtension implements ExtensionInterface
3233
{
@@ -177,6 +178,12 @@ private function registerHandlers(Container $container)
177178
$container->get('code_transform.refactor.override_method')
178179
);
179180
}, [ 'rpc.handler' => [] ]);
181+
182+
$container->register('rpc.handler.cache_clear', function (Container $container) {
183+
return new CacheClearHandler(
184+
$container->get('application.cache_clear')
185+
);
186+
}, [ 'rpc.handler' => [] ]);
180187
}
181188

182189
/**
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace Phpactor\Rpc\Handler;
4+
5+
use Phpactor\Rpc\Handler;
6+
use Phpactor\Application\CacheClear;
7+
use Phpactor\Rpc\Response\EchoResponse;
8+
9+
class CacheClearHandler implements Handler
10+
{
11+
const CACHE_CLEAR = 'cache_clear';
12+
13+
/**
14+
* @var CacheClear
15+
*/
16+
private $cacheClear;
17+
18+
public function __construct(CacheClear $cacheClear)
19+
{
20+
$this->cacheClear = $cacheClear;
21+
}
22+
23+
public function name(): string
24+
{
25+
return self::CACHE_CLEAR;
26+
}
27+
28+
public function defaultParameters(): array
29+
{
30+
return [];
31+
}
32+
33+
public function handle(array $arguments)
34+
{
35+
$this->cacheClear->clearCache();
36+
37+
return EchoResponse::fromMessage(sprintf('Cache cleared: %s', $this->cacheClear->cachePath()));
38+
}
39+
}

plugin/phpactor.vim

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,10 @@ function! phpactor#Navigate()
222222
call phpactor#rpc("navigate", { "source_path": currentPath })
223223
endfunction
224224

225+
function! phpactor#CacheClear()
226+
call phpactor#rpc("cache_clear", {})
227+
endfunction
228+
225229
"""""""""""""""""""""""
226230
" Utility functions
227231
"""""""""""""""""""""""

tests/Benchmark/ClassSearchBench.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class ClassSearchBench extends BaseBenchCase
1515
{
1616
public function setUp()
1717
{
18-
$this->initWorkspace();
18+
$this->workspace()->reset();
1919
$this->loadProject('Symfony');
2020
}
2121

0 commit comments

Comments
 (0)