Skip to content

Commit

Permalink
+ Raw doc comment use statements
Browse files Browse the repository at this point in the history
+~ Matcher plugins
  • Loading branch information
pmaselkowski committed Mar 17, 2015
1 parent 5bd87e3 commit 2beeba1
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 22 deletions.
5 changes: 5 additions & 0 deletions src/Builder/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Exception;
use Maslosoft\Addendum\Addendum;
use Maslosoft\Addendum\Collections\AnnotationsCollection;
use Maslosoft\Addendum\Collections\MatcherConfig;
use Maslosoft\Addendum\Interfaces\IAnnotation;
use Maslosoft\Addendum\Matcher\AnnotationsMatcher;
use Maslosoft\Addendum\Reflection\ReflectionAnnotatedClass;
Expand Down Expand Up @@ -160,6 +161,10 @@ private function _parse($reflection)
{
$parser = new AnnotationsMatcher;
$data = [];
$parser->setPlugins(new MatcherConfig([
'addendum' => $this->addendum,
'reflection' => $reflection
]));
$parser->matches($this->getDocComment($reflection), $data);
self::$_cache[$key] = $data;
}
Expand Down
75 changes: 56 additions & 19 deletions src/Builder/DocComment.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

class DocComment
{

private static $use = [];
private static $namespaces = [];
private static $classNames = [];
private static $classes = [];
Expand Down Expand Up @@ -63,27 +63,36 @@ public function get($reflection)
*/
public function forFile($name, $className = null)
{
if (null === $className)
$fqn = $this->process($name);
if($className)
{
$className = basename($name, '.' . pathinfo($name, PATHINFO_EXTENSION));
$fqn = $className;
}

$this->process($name);
$result = [
'namespace' => isset(self::$namespaces[$className]) ? self::$namespaces[$className] : [],
'className' => isset(self::$classNames[$className]) ? self::$classNames[$className] : [],
'class' => isset(self::$classes[$className]) ? self::$classes[$className] : '',
'methods' => isset(self::$methods[$className]) ? self::$methods[$className] : [],
'fields' => isset(self::$fields[$className]) ? self::$fields[$className] : []
'namespace' => isset(self::$namespaces[$fqn]) ? self::$namespaces[$fqn] : [],
'use' => isset(self::$use[$fqn]) ? self::$use[$fqn] : [],
'className' => isset(self::$classNames[$fqn]) ? self::$classNames[$fqn] : [],
'class' => isset(self::$classes[$fqn]) ? self::$classes[$fqn] : '',
'methods' => isset(self::$methods[$fqn]) ? self::$methods[$fqn] : [],
'fields' => isset(self::$fields[$fqn]) ? self::$fields[$fqn] : []
];

return $result;
}

public function forClass($reflection)
{
$this->process($reflection->getFileName());
$name = $reflection->getName();
return isset(self::$classes[$name]) ? self::$classes[$name] : false;
$fqn = $reflection->getName();
$result = [
'namespace' => isset(self::$namespaces[$fqn]) ? self::$namespaces[$fqn] : [],
'use' => isset(self::$use[$fqn]) ? self::$use[$fqn] : [],
'className' => isset(self::$classNames[$fqn]) ? self::$classNames[$fqn] : [],
'class' => isset(self::$classes[$fqn]) ? self::$classes[$fqn] : '',
'methods' => isset(self::$methods[$fqn]) ? self::$methods[$fqn] : [],
'fields' => isset(self::$fields[$fqn]) ? self::$fields[$fqn] : []
];
return $result;
}

public function forMethod($reflection)
Expand All @@ -106,16 +115,19 @@ private function process($file)
{
if (!isset(self::$parsedFiles[$file]))
{
$this->parse($file);
self::$parsedFiles[$file] = true;
$fqn = $this->parse($file);
self::$parsedFiles[$file] = $fqn;
}
return self::$parsedFiles[$file];
}

protected function parse($file)
{
$use = [];
$namespace = '\\';
$tokens = $this->getTokens($file);
$class = false;
$fqn = false;
$comment = null;
$max = count($tokens);
$i = 0;
Expand All @@ -125,6 +137,8 @@ protected function parse($file)
if (is_array($token))
{
list($code, $value) = $token;
$tokenName = token_name($code);

switch ($code)
{
case T_DOC_COMMENT:
Expand All @@ -146,7 +160,27 @@ protected function parse($file)
}
$namespace = preg_replace('~^\\\\+~', '', $namespace);
break;

case T_USE:
// After class declaration, this should ignore `use` trait
if($class)
{
break;
}
$comment = false;
$useNs = '';
for ($j = $i + 1; $j < count($tokens); $j++)
{
if ($tokens[$j][0] === T_STRING)
{
$useNs .= '\\' . $tokens[$j][1];
}
else if ($tokens[$j] === '{' || $tokens[$j] === ';')
{
break;
}
}
$use[] = preg_replace('~^\\\\+~', '', $useNs);
break;
case T_TRAIT:
case T_CLASS:
case T_INTERFACE:
Expand All @@ -161,15 +195,17 @@ protected function parse($file)
self::$classes[$class] = $comment;
$comment = false;
}
self::$classNames[$class] = $class;
self::$namespaces[$class] = $namespace;
$fqn = sprintf('%s\%s', $namespace, $class);
self::$namespaces[$fqn] = $namespace;
self::$classNames[$fqn] = $class;
self::$use[$fqn] = $use;
break;

case T_VARIABLE:
if ($comment !== false && $class)
{
$field = substr($token[1], 1);
self::$fields[$class][$field] = $comment;
self::$fields[$fqn][$field] = $comment;
$comment = false;
}
break;
Expand All @@ -178,7 +214,7 @@ protected function parse($file)
if ($comment !== false && $class)
{
$function = $this->getString($tokens, $i, $max);
self::$methods[$class][$function] = $comment;
self::$methods[$fqn][$function] = $comment;
$comment = false;
}

Expand All @@ -205,6 +241,7 @@ protected function parse($file)
}
$i++;
}
return $fqn;
}

private function getString($tokens, &$i, $max)
Expand Down
24 changes: 24 additions & 0 deletions src/Collections/MatcherConfig.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

namespace Maslosoft\Addendum\Collections;

use Maslosoft\Gazebo\PluginContainer;

/**
* MatcherConfig
*
* @author Piotr Maselkowski <pmaselkowski at gmail.com>
*/
class MatcherConfig extends PluginContainer
{

public $addendum = null;
public $reflection = null;

}
21 changes: 21 additions & 0 deletions src/Interfaces/Tokenizer/ITokenizer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

namespace Maslosoft\Addendum\Interfaces\Tokenizer;

use Maslosoft\Addendum\Builder\DocComment;

/**
* TODO This is for refactoring of DocComment
* @see DocComment
* @author Piotr Maselkowski <pmaselkowski at gmail.com>
*/
interface ITokenizer
{

}
18 changes: 15 additions & 3 deletions src/Matcher/Traits/PluginsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

namespace Maslosoft\Addendum\Matcher\Traits;

use Maslosoft\Addendum\Collections\MatcherConfig;
use Maslosoft\Addendum\Interfaces\Matcher\IMatcher;

/**
* PluginsTrait
*
Expand All @@ -17,17 +20,26 @@ trait PluginsTrait
{

/**
* Plugins
* @var mixed[]
* Matcher confugration
* @var MatcherConfig
*/
private $_plugins = [];

/**
*
* @return MatcherConfig
*/
public function getPlugins()
{
return $this->_plugins;
}

public function setPlugins($plugins)
/**
*
* @param MatcherConfig $plugins
* @return IMatcher
*/
public function setPlugins(MatcherConfig $plugins)
{
$this->_plugins = $plugins;
return $this;
Expand Down

0 comments on commit 2beeba1

Please sign in to comment.