Maslosoft Addendum API
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
<?php
/**
* This software package is licensed under AGPL, Commercial license.
*
* @package maslosoft/addendum
* @licence AGPL, Commercial
* @copyright Copyright (c) Piotr Masełkowski <pmaselkowski@gmail.com> (Meta container, further improvements, bugfixes)
* @copyright Copyright (c) Maslosoft (Meta container, further improvements, bugfixes)
* @copyright Copyright (c) Jan Suchal (Original version, builder, parser)
* @link https://maslosoft.com/addendum/ - maslosoft addendum
* @link https://code.google.com/p/addendum/ - original addendum project
*/
namespace Maslosoft\Addendum\Utilities;
use Maslosoft\Addendum\Addendum;
use Maslosoft\Addendum\Builder\DocComment;
use Maslosoft\Addendum\Collections\MatcherConfig;
use Maslosoft\Addendum\Matcher\AnnotationsMatcher;
use Maslosoft\Addendum\Reflection\ReflectionFile;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
use RecursiveRegexIterator;
use RegexIterator;
/**
* This is utility class, should not be used in production environment
* @author Piotr
*/
class AnnotationUtility
{
public $searchPaths = [
'annotations'
];
public $settingsPath = 'config/Preferences/org/netbeans/modules/php/project/';
public $outputPath = null;
/**
* This utility method find files containing $annotations
* annotates them and performs callback
*
* TODO: Use FileWalker after extensive tests
* NOTE: It is recommended to use FileWalker class
*
* @see FileWalker
* @param string[] $annotations
* @param callback $callback param is file path
*/
public static function fileWalker($annotations, $callback, $searchPaths = [])
{
$patterns = [];
foreach ($annotations as $annotation)
{
$annotation = preg_replace('~^@~', '', $annotation);
$patterns[] = sprintf('~@%s~', $annotation);
}
foreach ($searchPaths as $path)
{
$directoryIterator = new RecursiveDirectoryIterator($path);
$iterator = new RecursiveIteratorIterator($directoryIterator);
$regexIterator = new RegexIterator($iterator, '/^.+\.php$/i', RecursiveRegexIterator::GET_MATCH);
foreach ($regexIterator as $matches)
{
$file = $matches[0];
$parse = false;
if (is_readable($file))
{
$contents = file_get_contents($file);
}
else
{
// TODO Log this
continue;
}
foreach ($patterns as $pattern)
{
if ($parse)
{
continue;
}
if (preg_match($pattern, $contents))
{
$parse = true;
}
}
if (!$parse)
{
continue;
}
call_user_func($callback, $file, $contents);
}
}
}
/**
* Annotate file without including php file and without using reflection.
* This method returns raw annotation values.
* <i>This is intented for various builders, which should not include files.</i>
* This <b>ALWAYS</b> parses file.
* @param string $file
* @param string $className <b>NOT RECOMMENDED!</b> Optional class name if multiple classes are declared in one file
* @return mixed[][]
*/
public static function rawAnnotate($file, $className = null)
{
$docExtractor = new DocComment();
$docs = $docExtractor->forFile($file, $className);
$matcher = new AnnotationsMatcher();
$class = [];
$matcher->setPlugins(new MatcherConfig([
'addendum' => new Addendum(),
'reflection' => new ReflectionFile($file)
]));
$matcher->matches($docs['class'], $class);
$methods = [];
foreach ((array) $docs['methods'] as $name => $doc)
{
$methods[$name] = [];
$matcher->matches($doc, $methods[$name]);
}
$fields = [];
foreach ((array) $docs['fields'] as $name => $doc)
{
$fields[$name] = [];
$matcher->matches($doc, $fields[$name]);
}
$result = [
'namespace' => $docs['namespace'],
'className' => $docs['className'],
'class' => $class,
'methods' => $methods,
'fields' => $fields
];
return $result;
}
}
API documentation generated by ApiGen