Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"require": {
"symfony/console": "~2.7",
"roave/better-reflection": "dev-variable_collector",
"phpbench/container": "^1.0"
"phpbench/container": "^1.0",
"symfony/finder": "^3.2"
},
"require-dev": {
"phpunit/phpunit": "^5.2"
Expand Down
2 changes: 1 addition & 1 deletion lib/Complete/Provider/FetchProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function canProvideFor(Scope $scope): bool
return
$scope->getNode() instanceof Expr\ClassConstFetch ||
$scope->getNode() instanceof Expr\PropertyFetch ||
$scope->getNode() instanceof Expr\MethodCall;
$scope->getNode() instanceof Expr\MethodCall; // TODO: test?
}

public function provide(Scope $scope, Suggestions $suggestions)
Expand Down
81 changes: 81 additions & 0 deletions lib/Console/Command/IndexCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

namespace Phpactor\Console\Command;

use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Phpactor\Index\Indexer;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Console\Helper\ProgressBar;
use Phpactor\Index\Index;

class IndexCommand extends Command
{
/**
* @var Indexer
*/
private $indexer;

/**
* @var Finder
*/
private $finder;

public function __construct(Indexer $indexer, Finder $finder = null)
{
parent::__construct();
$this->indexer = $indexer;
$this->finder = $finder ?: new Finder();
}

public function configure()
{
$this->setName('index');
$this->addOption('path', null, InputOption::VALUE_REQUIRED|InputOption::VALUE_IS_ARRAY, 'Path to index');
$this->addOption('index', null, Inputoption::VALUE_REQUIRED, 'Path to index file');
$this->addOption('exclude', null, Inputoption::VALUE_REQUIRED|InputOption::VALUE_IS_ARRAY, 'Regex pattern to exclude');
}

public function execute(InputInterface $input, OutputInterface $output)
{
$paths = $input->getOption('path');
$indexPath = $input->getOption('index') ?: getcwd() . '/.phpactor.php';
$excludes = $input->getOption('exclude');

$index = new Index($paths);
if (file_exists($indexPath)) {
$existingIndex = Index::loadFromFile($indexPath);

if ($existingIndex->getPaths() == $paths) {
$this->finder->date('> ' . date('c', $existingIndex->getTimestamp()));
$index->setMap($existingIndex->getMap());;
}
}

if (empty($paths)) {
throw new \InvalidArgumentException(
'You must provide at least one path'
);
}

$this->finder->files();
$this->finder->in($paths);

foreach ($excludes as $exclude) {
$this->finder->notPath($exclude);
}

$progress = new ProgressBar($output, count($this->finder));
$progress->start();

$index = $this->indexer->__invoke($index, $this->finder, function ($file) use ($progress, $output) {
$progress->advance();
});

$progress->finish();

file_put_contents($indexPath, "<?php return <<<'EOT'" . PHP_EOL . serialize($index) . PHP_EOL . 'EOT' . PHP_EOL . ';');

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be handled by index class

}
}
15 changes: 15 additions & 0 deletions lib/Extension/CoreExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
use BetterReflection\SourceLocator\Type\StringSourceLocator;
use BetterReflection\SourceLocator\Type\AggregateSourceLocator;
use BetterReflection\SourceLocator\Type\PhpInternalSourceLocator;
use Phpactor\Console\Command\IndexCommand;
use Phpactor\Index\Indexer;

class CoreExtension implements ExtensionInterface
{
Expand All @@ -40,6 +42,7 @@ public function getDefaultConfig()
public function load(Container $container)
{
$this->registerComplete($container);
$this->registerIndex($container);
$this->registerConsole($container);
$this->registerStorage($container);
$this->registerMisc($container);
Expand All @@ -66,6 +69,13 @@ private function registerComplete(Container $container)
});
}

private function registerIndex(Container $container)
{
$container->register('indexer', function(Container $container) {
return new Indexer();
});
}

private function registerStorage(Container $container)
{
$container->register('dbal.connection_wrapper', function (Container $container) {
Expand Down Expand Up @@ -123,6 +133,7 @@ private function registerConsole(Container $container)
$application->addCommands([
$container->get('command.explain'),
$container->get('command.complete'),
$container->get('command.index'),
]);

return $application;
Expand All @@ -135,5 +146,9 @@ private function registerConsole(Container $container)
$container->register('command.explain', function (Container $container) {
return new ExplainCommand($container->get('reflector'));
});

$container->register('command.index', function (Container $container) {
return new IndexCommand($container->get('indexer'));
});
}
}
66 changes: 66 additions & 0 deletions lib/Index/Index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

namespace Phpactor\Index;

class Index
{
private $map;
private $paths;
private $timestamp;

public function __construct(array $paths, int $timestamp = null)
{
$this->paths = $paths;
$this->timestamp = $timestamp ?: time();
}

public static function loadFromFile(string $path)
{
if (!file_exists($path)) {
throw new \InvalidArgumentException(sprintf(
'Index does not exist at path "%s"', $path
));
}

$index = unserialize(require($path));

if (!$index instanceof Index) {
throw new \InvalidArgumentException(sprintf(
'Invalid index object'
));
}

if (!$index) {
throw new \RuntimeException(
'Could not deserialize index, delete it and try again please.'
);
}

return $index;
}

public function setMap(array $map)
{
$this->map = $map;
}

public function add($classFqn, $filePath)
{
$this->map[$classFqn] = $filePath;
}

public function getMap()
{
return $this->map;
}

public function getPaths()
{
return $this->paths;
}

public function getTimestamp()
{
return $this->timestamp;
}
}

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

trestths

41 changes: 41 additions & 0 deletions lib/Index/Indexer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Phpactor\Index;

use Symfony\Component\Finder\Finder;
use Phpactor\Util\ClassUtil;
use \Exception;
use \Closure;
use Phpactor\Index\Index;

class Indexer
{
/**
* @var Exception[]
*/
private $exceptions = [];

public function __invoke(Index $index, Finder $finder, Closure $progressCallback)
{
foreach ($finder as $file) {
try {
$classNames = ClassUtil::getClassNamesFromFile($file);

foreach ($classNames as $className) {
$index->add($className, $file->getPathname());
}
} catch (Exception $e) {
$this->exceptions[] = $e;
// continue
}
$progressCallback($file);
}

return $index;
}

public function getExceptions()
{
return $this->exceptions;
}
}
11 changes: 8 additions & 3 deletions lib/Util/ClassUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
class ClassUtil
{
public static function getClassNameFromFile(string $file): string
{
return $this->getClassNamesFromFile()->first();
}

public static function getClassNamesFromFile(string $file): \Generator
{
$reflector = new ClassReflector(new SingleFileSourceLocator($file));

Expand All @@ -17,8 +22,8 @@ public static function getClassNameFromFile(string $file): string
return;
}

$class = reset($classes);

return $class->getName();
foreach ($classes as $class) {
yield($class->getName());
}
}
}
Binary file removed phpactor.sqlite
Binary file not shown.