エントリーエンタープライズPHPプロセスフレームワークHyperf

24184 ワード

インストール
  • インストールswoole拡張:wgethttps://github.com/swoole/swoole-src/archive/v4.5.0.tar.gz
  • phpize
  • ./configure --with-php-config=/www/server/php/72/bin/php-config --enable-http2 --enable-openssl
  • make
  • make install
  • php --ri swoole
  • Componenterによるプロジェクトの作成:composer create-project hyperf/hyperrf-skeleton
  • 起動:php bin/hyperrf.php start
  • アクセス:curlhttp://127.0.0.1:9501/
  • ab圧力測定:ab-k-c 100-n 10000http://127.0.0.1:9501/

  • ルート
    config/routes.php
    <?php
    declare(strict_types=1);
    use Hyperf\HttpServer\Router\Router;
    Router::addRoute(['GET', 'POST', 'HEAD'], '/', 'App\Controller\IndexController@index');
    //[root@VM_0_13_centos hyperf-skeleton]# curl 127.0.0.1:9501/
    
    <?php
    declare(strict_types=1);
    use Hyperf\HttpServer\Router\Router;
    Router::addRoute(['POST', 'HEAD'], '/', 'App\Controller\IndexController@index');
    
    //curl 127.0.0.1:9501/ -w %{http_code}
    //Allow: POST, HEAD405
    //curl 127.0.0.1:9501/ -w %{http_code} -X POST
    //{"method":"POST","message":"Hello Hyperf."}200
    
    <?php
    declare(strict_types=1);
    use Hyperf\HttpServer\Router\Router;
    Router::post('/', 'App\Controller\IndexController@index');
    

    @Controller,@RequestMapping,@GetMapping,@PostMapping,@PutMapping,@PutMappingなどの注釈と組み合わせて使用する必要がある.
    //curl 127.0.0.1:9501/index/index -w %{http_code} -X POST
    <?php
    
    declare(strict_types=1);
    namespace App\Controller;
    
    use Hyperf\HttpServer\Annotation\AutoController;
    
    /**
     * @AutoController()
     */
    class IndexController extends AbstractController
    {
        public function index()
        {
            $user = $this->request->input('user', 'Hyperf');
            $method = $this->request->getMethod();
    
            return [
                'method' => $method,
                'message' => "Hello {$user}.",
            ];
        }
    }
    
    //curl 127.0.0.1:9501/user/index -w %{http_code} -X POST
    <?php
    
    declare(strict_types=1);
    namespace App\Controller;
    
    use Hyperf\HttpServer\Annotation\AutoController;
    
    /**
     * @AutoController(prefix="user")
     */
    class IndexController extends AbstractController
    {
        public function index()
        {
            $user = $this->request->input('user', 'Hyperf');
            $method = $this->request->getMethod();
    
            return [
                'method' => $method,
                'message' => "Hello {$user}.",
            ];
        }
    }
    
    //curl 127.0.0.1:9501/user/index/index -w %{http_code} -X POST
    <?php
    
    declare(strict_types=1);
    namespace App\Controller\User;
    use App\Controller\AbstractController;
    
    use Hyperf\HttpServer\Annotation\AutoController;
    
    /**
     * @AutoController()
     */
    class IndexController extends AbstractController
    {
        public function index()
        {
            $user = $this->request->input('user', 'Hyperf');
            $method = $this->request->getMethod();
    
            return [
                'method' => $method,
                'message' => "Hello {$user}.",
            ];
        }
    }
    
    <?php
    //curl 127.0.0.1:9501/index/index -w %{http_code} -X POST
    
    declare(strict_types=1);
    namespace App\Controller\User;
    
    use App\Controller\AbstractController;
    use Hyperf\HttpServer\Annotation\Controller;
    use Hyperf\HttpServer\Annotation\RequestMapping;
    
    /**
     * @Controller(prefix="index")
     */
    class IndexController extends AbstractController
    {
        /**
         * @RequestMapping(path="index",methods={"get","post"})
         */
        public function index()
        {
            $user = $this->request->input('user', 'Hyperf');
            $method = $this->request->getMethod();
    
            return [
                'method' => $method,
                'message' => "Hello {$user}.",
            ];
        }
    }
    
    <?php
    //curl 127.0.0.1:9501/overwrite -w %{http_code} -X POST
    declare(strict_types=1);
    namespace App\Controller\User;
    
    use App\Controller\AbstractController;
    use Hyperf\HttpServer\Annotation\Controller;
    use Hyperf\HttpServer\Annotation\RequestMapping;
    
    /**
     * @Controller(prefix="index")
     */
    class IndexController extends AbstractController
    {
        /**
         * @RequestMapping(path="/overwrite",methods={"get","post"})
         */
        public function index()
        {
            $user = $this->request->input('user', 'Hyperf');
            $method = $this->request->getMethod();
    
            return [
                'method' => $method,
                'message' => "Hello {$user}.",
            ];
        }
    }
    
    

    IocとDI
    Ioc,     ,           ,               。
    DI,     ,                 ,                 。
    DI  Ioc      。
    
        DI   
    1、PHP         ,                 
    2、         ,                    
    3、          Hyperf DI           
    4、       ,   ,      
    5、DI                     
    6、DI             ,        
    7、  $container->make()   make()           
    8、  new             ,        
    

    カスタム注記
    app/Annotation/Foo.php
    <?php
    namespace App\Annotation;
    use Hyperf\Di\Annotation\AbstractAnnotation;
    
    
    /**
     * @Annotation
     * @Target({"CLASS","METHOD"})
     */
    class Foo extends AbstractAnnotation
    {
        /**
         * @var string
         */
        public $bar;
    
    }
    
    app/Controller/User/IndexController.php
    <?php
    
    declare(strict_types=1);
    namespace App\Controller\User;
    
    use App\Annotation\Foo;
    use Hyperf\Di\Annotation\AnnotationCollector;
    use Hyperf\HttpServer\Annotation\AutoController;
    
    /**
     * @AutoController()
     * @Foo("123ka==")
     */
    class IndexController
    {
    
        public function index()
        {
          $res =AnnotationCollector::getClassByAnnotation(Foo::class);
            return json_encode($res);
        }
    }
    

    AOP
    AOP   ?
          ,                               。
    AOP     OOP   ,                
    OOP                         ,                。
    
     AOP                  ,                  ,       
                   。
    
           
    
    
    Hyperf  AOP
              , Aspect(  )
    Aspect             
            DI  
              AOP       ,        AOP   
           ( Before,After,AfterReturning,AfterThrowing),   
      (Around)       。
    
    Hyperf AOP   DI   
          Hyperf DI  , hyperf/di
        DI        AOP  ,  new  
                           。
    
    AOP     
        、  、     ,    ,    ,    ,    ,  ,
         ,   ,      
    
      AOP   
    @Cacheable
    @CachePut
    @CacheEvict
    @Task
    @RateLimit
    @CircuitBreaker
               
    
    app/Aspect/IndexAspect.php
    
    <?php
    namespace App\Aspect;
    use Hyperf\Di\Annotation\Aspect;
    use Hyperf\Di\Aop\AbstractAspect;
    use Hyperf\Di\Aop\ProceedingJoinPoint;
    use App\Controller\User\IndexController;
    
    /**
     * @Aspect()
     */
    class IndexAspect extends AbstractAspect
    {
        public $classes = [
            IndexController::class .'::'.'index',
        ];
        public function process(ProceedingJoinPoint $proceedingJoinPoint)
        {
            //var_dump(__CLASS__);
            $result = $proceedingJoinPoint->process();
            return 'before=='.$result.'==after';
        }
    }
    
    app/Controller/User/IndexController.php
    
    <?php
    
    declare(strict_types=1);
    namespace App\Controller\User;
    use Hyperf\HttpServer\Annotation\AutoController;
    
    /**
     * @AutoController()
     */
    class IndexController
    {
        public function index()
        {
            return 2;
        }
    }