| 1 | <?php |
||
| 15 | class TraitDefinition extends ParentDefinition |
||
| 16 | { |
||
| 17 | /** |
||
| 18 | * @var string |
||
| 19 | */ |
||
| 20 | protected $filepath; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @var Stmt\Trait_ |
||
| 24 | */ |
||
| 25 | protected $statement; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var ClassMethod[] |
||
| 29 | */ |
||
| 30 | protected $methods = []; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @param string $name |
||
| 34 | * @param Stmt\Trait_ $statement |
||
| 35 | */ |
||
| 36 | public function __construct($name, Stmt\Trait_ $statement) |
||
| 37 | { |
||
| 38 | $this->name = $name; |
||
| 39 | $this->statement = $statement; |
||
| 40 | } |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Compile the definition |
||
| 44 | * |
||
| 45 | * @param Context $context |
||
| 46 | * @return boolean |
||
| 47 | */ |
||
| 48 | public function compile(Context $context) |
||
| 49 | { |
||
| 50 | $context->setFilepath($this->filepath); |
||
| 51 | |||
| 52 | $context->getEventManager()->fire( |
||
| 53 | Event\StatementBeforeCompile::EVENT_NAME, |
||
| 54 | new Event\StatementBeforeCompile( |
||
| 55 | $this->statement, |
||
| 56 | $context |
||
| 57 | ) |
||
| 58 | ); |
||
| 59 | |||
| 60 | return true; |
||
| 61 | } |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @return string |
||
| 65 | */ |
||
| 66 | public function getFilepath() |
||
| 67 | { |
||
| 68 | return $this->filepath; |
||
| 69 | } |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @param string $filepath |
||
| 73 | */ |
||
| 74 | public function setFilepath($filepath) |
||
| 75 | { |
||
| 76 | $this->filepath = $filepath; |
||
| 77 | } |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @return bool |
||
| 81 | */ |
||
| 82 | public function precompile() |
||
| 83 | { |
||
| 84 | foreach ($this->statement->stmts as $stmt) { |
||
| 85 | if ($stmt instanceof Stmt\ClassMethod) { |
||
| 86 | $this->addMethod(new ClassMethod($stmt->name, $stmt, $stmt->flags)); |
||
| 87 | } |
||
| 88 | } |
||
| 89 | |||
| 90 | return true; |
||
| 91 | } |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @param ClassMethod $method |
||
| 95 | */ |
||
| 96 | public function addMethod(ClassMethod $method) |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @return ClassMethod[] |
||
| 103 | */ |
||
| 104 | public function getMethods() |
||
| 108 | } |
||
| 109 |