Maslosoft Zamm 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
<?php
/**
* This software package is licensed under `AGPL, Commercial` license[s].
*
* @package maslosoft/zamm
* @license AGPL, Commercial
*
* @copyright Copyright (c) Peter Maselkowski <pmaselkowski@gmail.com>
* @link https://maslosoft.com/zamm/
*/
namespace Maslosoft\Zamm;
use Maslosoft\Zamm\Helpers\Tabs;
use Maslosoft\Zamm\Helpers\Wrapper;
use Maslosoft\Zamm\Interfaces\SourceAccessorInterface;
use ReflectionClass;
use ReflectionProperty;
/**
* Source extractor for code fragments.
* This class extracts source code for specified code elements.
*
* @author Piotr Maselkowski <pmaselkowski at gmail.com>
*/
class Source implements SourceAccessorInterface
{
use Traits\SourceMagic;
/**
* Reflection information
* @var ReflectionClass
*/
private $info = null;
/**
* Source as array
* @var string[]
*/
private $source = [];
public function __construct($className = null)
{
$this->info = new ReflectionClass($className);
$this->source = file($this->info->getFileName());
}
/**
* Should return source of working class
*/
public function __toString()
{
return $this->getTypeSource();
}
public static function __callStatic($name, $arguments)
{
}
/**
* Get source code of method
* @param string $name
* @return Wrapper
*/
public function method($name)
{
assert($this->info->hasMethod($name), sprintf('Method `%s` does not exists on class `%s`', $name, $this->info->name));
$method = $this->info->getMethod($name);
$code = $this->getMethodSource($method->getStartLine(), $method->getEndLine());
$result = [];
$comment = $method->getDocComment();
Tabs::trim($comment);
$result[] = $comment . PHP_EOL;
$result[] = $code . PHP_EOL;
return new Wrapper(implode('', $result));
}
/**
* Get source code of property
* @param string $name
* @return Wrapper
*/
public function property($name)
{
assert($this->info->hasProperty($name), sprintf('Property `%s` does not exists on class `%s`', $name, $this->info->name));
$property = $this->info->getProperty($name);
$code = $this->getPropertySource($property);
$result = [];
$comment = $property->getDocComment();
Tabs::trim($comment);
$result[] = $comment . PHP_EOL;
$result[] = $code . PHP_EOL;
return new Wrapper(implode('', $result));
}
private function getMethodSource($start, $end)
{
// Assume blank line after method name
// So start one line above
if (preg_match('~^\s*\{~', $this->source[$start]))
{
$start--;
}
$source = array_values($this->source);
$result = array_splice($source, $start, $end - $start);
Tabs::trim($result);
return implode('', $result);
}
public function getPropertySource(ReflectionProperty $property)
{
$name = [];
switch (true)
{
case $property->isPublic():
$name[] = 'public';
break;
case $property->isProtected():
$name[] = 'public';
break;
case $property->isPrivate():
$name[] = 'public';
break;
}
if ($property->isStatic())
{
$name[] = 'static';
}
$name[] = sprintf('$%s', $property->name);
return implode(' ', $name) . ';';
}
private function getTypeSource()
{
return '';
}
}
API documentation generated by ApiGen