SHARE
TWEET

Untitled

a guest Mar 24th, 2019 59 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. namespace Assoa\Parser;
  3.  
  4. use PhpParser\Node;
  5. use PhpParser\Node\Stmt\Class_;
  6. use Assoa\Commands\BuilderCommand;
  7. use PhpParser\NodeVisitorAbstract;
  8. use Assoa\Parser\INodeVisitorFactory;
  9. use PhpParser\Node\Stmt\ClassMethod;
  10.  
  11. class ProxyNodeVisitorFactory implements INodeVisitorFactory
  12. {
  13.  
  14.     public static function get(...$args)
  15.     {
  16.         $className = $args[0];
  17.         $commands = $args[1];
  18.        
  19.         $visitor = new class($className, $commands) extends NodeVisitorAbstract
  20.         {
  21.             protected $parsingTargetClass = false;
  22.             protected $isFinalClass = false;
  23.  
  24.             public function __construct($className, $commands)
  25.             {
  26.                 $this->commands = $commands;
  27.                 $this->className = $className;
  28.             }
  29.  
  30.             public function enterNode(Node $node)
  31.             {
  32.                 if (($node instanceof ClassMethod) && $node->isPublic() && $this->parsingTargetClass === true) {on
  33.                     if ($node->isFinal()) {
  34.                         throw new InvalidArgumentException("A final class cannot be proxied");
  35.                     }
  36.                     if($node->name == "__construct"){
  37.                         $this->commands['constructor']->execute($node);
  38.                     } else {
  39.                         $this->commands['method']->execute($node);
  40.                     }
  41.                 } elseif (($node instanceof Class_) && ($node->name == $this->className)) {
  42.                     $this->parsingTargetClass = true;
  43.                 }
  44.             }
  45.  
  46.             public function leaveNode(Node $node)
  47.             {
  48.                 if ($node instanceof Class_) {
  49.                     // If the node is a class node
  50.                     if ($node->name == $this->className) {
  51.                         $this->commands['class']->execute();
  52.                         $this->parsingTargetClass = false;
  53.                     }
  54.  
  55.                 }
  56.             }
  57.  
  58.         };
  59.         return $visitor;
  60.     }
  61.  
  62. };
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top