Skip to content

Commit

Permalink
refactored plugin
Browse files Browse the repository at this point in the history
Added Completer
Splitted Plugin to Plugin adn TypeResolver
  • Loading branch information
mkusher committed Aug 9, 2015
1 parent 92747db commit 7d0041d
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 35 deletions.
46 changes: 46 additions & 0 deletions Completer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace Mkusher\PadawanDi;

use Complete\Completer\CompleterInterface;
use Complete\Completer\ClassNameCompleter;
use Entity\Completion\Entry;
use Entity\Project;
use Entity\Completion\Context;
use Entity\FQCN;

class Completer implements CompleterInterface
{
public function __construct(ClassNameCompleter $completer)
{
$this->classNameCompleter = $completer;
}
public function getEntries(Project $project, Context $context)
{
list($type, $isThis, $types) = $context->getData();
if (is_array($types)) {
$fqcn = array_pop($types);
if ($fqcn instanceof FQCN
&& $fqcn->toString() === 'DI\\Container'
) {
return array_map(
[$this, 'wrapEntry'],
$this->classNameCompleter->getEntries($project, $context)
);
}
}
return [];
}

public function wrapEntry($entry)
{
return new Entry(
sprintf('"%s"', $entry->getName()),
$entry->getSignature(),
$entry->getDesc(),
$entry->getMenu()
);
}

private $classNameCompleter;
}
59 changes: 24 additions & 35 deletions Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,60 +4,49 @@

use Symfony\Component\EventDispatcher\EventDispatcher;
use Complete\Resolver\NodeTypeResolver;
use Complete\Resolver\TypeResolveEvent;
use Entity\FQCN;
use PhpParser\Node\Arg;
use PhpParser\Node\Scalar\String_;
use Complete\Completer\CompleterFactory;
use Parser\UseParser;

class Plugin
{
public function __construct(
EventDispatcher $dispatcher,
UseParser $useParser
TypeResolver $resolver,
Completer $completer
) {
$this->dispatcher = $dispatcher;
$this->useParser = $useParser;
$this->resolver = $resolver;
$this->completer = $completer;
}

public function load()
public function init()
{
if ($this->isLoaded) {
return;
}
$this->isLoaded = true;
$plugin = $this;
$this->dispatcher->addListener(
NodeTypeResolver::BLOCK_START,
function (TypeResolveEvent $e) use ($plugin) {
$plugin->parentType = $e->getType();
}
[$this->resolver, 'handleParentTypeEvent']
);
$this->dispatcher->addListener(
NodeTypeResolver::BLOCK_END,
function (TypeResolveEvent $e) use ($plugin) {
$parentType = $plugin->parentType;
if ($parentType instanceof FQCN
&& $parentType->toString() === 'DI\\Container'
) {
/** @var \Entity\Chain\MethodCall */
$chain = $e->getChain();
if ($chain->getType() === 'method' && count($chain->getArgs()) > 0) {
$firstArg = array_pop($chain->getArgs())->value;
if ($firstArg instanceof String_) {
$className = $firstArg->value;
$fqcn = $plugin->useParser->parseFQCN($className);
$e->setType($fqcn);
}
}
}
}
[$this->resolver, 'handleTypeResolveEvent']
);
$this->dispatcher->addListener(
CompleterFactory::CUSTOM_COMPLETER,
[$this, 'handleCompleteEvent']
);
}

private $parentType;
public function handleCompleteEvent($e)
{
$context = $e->context;
if ($context->isMethodCall()) {
$e->completer = $this->completer;
}
}

/** @var Completer */
private $completer;
/** @var TypeResolver */
private $resolver;
/** @var EventDispatcher */
private $dispatcher;
private $useParser;
private $isLoaded = false;
}
46 changes: 46 additions & 0 deletions TypeResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace Mkusher\PadawanDi;

use Parser\UseParser;
use Complete\Resolver\TypeResolveEvent;
use Entity\FQCN;
use PhpParser\Node\Arg;
use PhpParser\Node\Scalar\String_;

class TypeResolver
{
public function __construct(
UseParser $useParser
) {
$this->useParser = $useParser;
}

public function handleParentTypeEvent(TypeResolveEvent $e)
{
$this->parentType = $e->getType();
}

public function handleTypeResolveEvent(TypeResolveEvent $e)
{
$parentType = $this->parentType;
if ($parentType instanceof FQCN
&& $parentType->toString() === 'DI\\Container'
) {
/** @var \Entity\Chain\MethodCall */
$chain = $e->getChain();
if ($chain->getType() === 'method' && count($chain->getArgs()) > 0) {
$firstArg = array_pop($chain->getArgs())->value;
if ($firstArg instanceof String_) {
$className = $firstArg->value;
$fqcn = $this->useParser->parseFQCN($className);
$e->setType($fqcn);
}
}
}
}

/** @var UseParser */
private $useParser;
private $parentType;
}

0 comments on commit 7d0041d

Please sign in to comment.