• Swoole实战之手撸HttpServer框架 20 ORM整合2 注解1


    视频地址 https://www.bilibili.com/video/BV14E411t7T4?p=32&spm_id_from=pageDriver&vd_source=4a69745b599dffec877b0fcfe130b092

    1 原理

    使用注解来完成ORM整合

    将Core\init\MyDB的实例注入BeanFactory,

    Core\init\MyDB实例里有一个LvDB的实例,装载了ORM

    已经装载对象

    self::$container->get('Core\init\MyDB') 

    -----Core\init\MyDB Object
    (
        [lvDB] => Illuminate\Database\Capsule\Manager Object
            (
                [manager:protected] => Illuminate\Database\DatabaseManager Object
                    (
                        [app:protected] => Illuminate\Container\Container Object
                            (
                                [resolved:protected] => Array
                                    (
                                    )
    
                                [bindings:protected] => Array
                                    (
                                    )
    
                                [methodBindings:protected] => Array
                                    (
                                    )
    
                                [instances:protected] => Array
                                    (
                                        [config] => Illuminate\Support\Fluent Object
                                            (
                                                [attributes:protected] => Array
                                                    (
                                                        [database.fetch] => 5
                                                        [database.default] => default
                                                        [database.connections] => Array
                                                            (
                                                                [default] => Array
                                                                    (
                                                                        [driver] => mysql
                                                                        [host] => 10.10.10.234
                                                                        [port] => 12345
                                                                        [database] => seckill
                                                                        [username] => root
                                                                        [password] => 123456
                                                                        [charset] => utf8mb4
                                                                        [collation] => utf8mb4_general_ci
                                                                        [prefix] => 
                                                                    )
    
                                                                [test2] => Array
                                                                    (
                                                                        [driver] => mysql
                                                                        [host] => localhost
                                                                        [database] => test2
                                                                        [username] => root
                                                                        [password] => 500500
                                                                        [charset] => utf8
                                                                        [collation] => utf8_unicode_ci
                                                                        [prefix] => 
                                                                    )
    
                                                            )
    
                                                    )
    
                                            )
    
                                    )
    
                                [scopedInstances:protected] => Array
                                    (
                                    )
    
                                [aliases:protected] => Array
                                    (
                                    )
    
                                [abstractAliases:protected] => Array
                                    (
                                    )
    
                                [extenders:protected] => Array
                                    (
                                    )
    
                                [tags:protected] => Array
                                    (
                                    )
    
                                [buildStack:protected] => Array
                                    (
                                    )
    
                                [with:protected] => Array
                                    (
                                    )
    
                                [contextual] => Array
                                    (
                                    )
    
                                [reboundCallbacks:protected] => Array
                                    (
                                    )
    
                                [globalBeforeResolvingCallbacks:protected] => Array
                                    (
                                    )
    
                                [globalResolvingCallbacks:protected] => Array
                                    (
                                    )
    
                                [globalAfterResolvingCallbacks:protected] => Array
                                    (
                                    )
    
                                [beforeResolvingCallbacks:protected] => Array
                                    (
                                    )
    
                                [resolvingCallbacks:protected] => Array
                                    (
                                    )
    
                                [afterResolvingCallbacks:protected] => Array
                                    (
                                    )
    
                            )
    
                        [factory:protected] => Illuminate\Database\Connectors\ConnectionFactory Object
                            (
                                [container:protected] => Illuminate\Container\Container Object
                                    (
                                        [resolved:protected] => Array
                                            (
                                            )
    
                                        [bindings:protected] => Array
                                            (
                                            )
    
                                        [methodBindings:protected] => Array
                                            (
                                            )
    
                                        [instances:protected] => Array
                                            (
                                                [config] => Illuminate\Support\Fluent Object
                                                    (
                                                        [attributes:protected] => Array
                                                            (
                                                                [database.fetch] => 5
                                                                [database.default] => default
                                                                [database.connections] => Array
                                                                    (
                                                                        [default] => Array
                                                                            (
                                                                                [driver] => mysql
                                                                                [host] => 10.10.10.234
                                                                                [port] => 12345
                                                                                [database] => seckill
                                                                                [username] => root
                                                                                [password] => 123456
                                                                                [charset] => utf8mb4
                                                                                [collation] => utf8mb4_general_ci
                                                                                [prefix] => 
                                                                            )
    
                                                                        [test2] => Array
                                                                            (
                                                                                [driver] => mysql
                                                                                [host] => localhost
                                                                                [database] => test2
                                                                                [username] => root
                                                                                [password] => 500500
                                                                                [charset] => utf8
                                                                                [collation] => utf8_unicode_ci
                                                                                [prefix] => 
                                                                            )
    
                                                                    )
    
                                                            )
    
                                                    )
    
                                            )
    
                                        [scopedInstances:protected] => Array
                                            (
                                            )
    
                                        [aliases:protected] => Array
                                            (
                                            )
    
                                        [abstractAliases:protected] => Array
                                            (
                                            )
    
                                        [extenders:protected] => Array
                                            (
                                            )
    
                                        [tags:protected] => Array
                                            (
                                            )
    
                                        [buildStack:protected] => Array
                                            (
                                            )
    
                                        [with:protected] => Array
                                            (
                                            )
    
                                        [contextual] => Array
                                            (
                                            )
    
                                        [reboundCallbacks:protected] => Array
                                            (
                                            )
    
                                        [globalBeforeResolvingCallbacks:protected] => Array
                                            (
                                            )
    
                                        [globalResolvingCallbacks:protected] => Array
                                            (
                                            )
    
                                        [globalAfterResolvingCallbacks:protected] => Array
                                            (
                                            )
    
                                        [beforeResolvingCallbacks:protected] => Array
                                            (
                                            )
    
                                        [resolvingCallbacks:protected] => Array
                                            (
                                            )
    
                                        [afterResolvingCallbacks:protected] => Array
                                            (
                                            )
    
                                    )
    
                            )
    
                        [connections:protected] => Array
                            (
                            )
    
                        [extensions:protected] => Array
                            (
                            )
    
                        [reconnector:protected] => Closure Object
                            (
                                [this] => Illuminate\Database\DatabaseManager Object
     *RECURSION*
                                [parameter] => Array
                                    (
                                        [$connection] => <required>
                                    )
    
                            )
    
                        [doctrineTypes:protected] => Array
                            (
                            )
    
                    )
    
                [container:protected] => Illuminate\Container\Container Object
                    (
                        [resolved:protected] => Array
                            (
                            )
    
                        [bindings:protected] => Array
                            (
                            )
    
                        [methodBindings:protected] => Array
                            (
                            )
    
                        [instances:protected] => Array
                            (
                                [config] => Illuminate\Support\Fluent Object
                                    (
                                        [attributes:protected] => Array
                                            (
                                                [database.fetch] => 5
                                                [database.default] => default
                                                [database.connections] => Array
                                                    (
                                                        [default] => Array
                                                            (
                                                                [driver] => mysql
                                                                [host] => 10.10.10.234
                                                                [port] => 12345
                                                                [database] => seckill
                                                                [username] => root
                                                                [password] => 123456
                                                                [charset] => utf8mb4
                                                                [collation] => utf8mb4_general_ci
                                                                [prefix] => 
                                                            )
    
                                                        [test2] => Array
                                                            (
                                                                [driver] => mysql
                                                                [host] => localhost
                                                                [database] => test2
                                                                [username] => root
                                                                [password] => 500500
                                                                [charset] => utf8
                                                                [collation] => utf8_unicode_ci
                                                                [prefix] => 
                                                            )
    
                                                    )
    
                                            )
    
                                    )
    
                            )
    
                        [scopedInstances:protected] => Array
                            (
                            )
    
                        [aliases:protected] => Array
                            (
                            )
    
                        [abstractAliases:protected] => Array
                            (
                            )
    
                        [extenders:protected] => Array
                            (
                            )
    
                        [tags:protected] => Array
                            (
                            )
    
                        [buildStack:protected] => Array
                            (
                            )
    
                        [with:protected] => Array
                            (
                            )
    
                        [contextual] => Array
                            (
                            )
    
                        [reboundCallbacks:protected] => Array
                            (
                            )
    
                        [globalBeforeResolvingCallbacks:protected] => Array
                            (
                            )
    
                        [globalResolvingCallbacks:protected] => Array
                            (
                            )
    
                        [globalAfterResolvingCallbacks:protected] => Array
                            (
                            )
    
                        [beforeResolvingCallbacks:protected] => Array
                            (
                            )
    
                        [resolvingCallbacks:protected] => Array
                            (
                            )
    
                        [afterResolvingCallbacks:protected] => Array
                            (
                            )
    
                    )
    
            )
    
        [dbSource:Core\init\MyDB:private] => default
    )
    View Code

    2 代码

    2.1 \pro\core\annotationhandlers\DBHandler.php

    <?php
    
    namespace Core\annotationhandlers;
    
    use Core\annotations\DB;
    use Core\BeanFactory;
    use Core\init\MyDB;
    
    
    
    return [
      DB::class=>function(\ReflectionProperty $prop,$instance,$self) {
        $mydbBean = BeanFactory::getBean(MyDB::class);   //从新获取一个对象
        $mydbBean ->setDbSource($self->source);//新MyDB对象设置数据源
        
        $prop->setAccessible(true);   //
        $prop->setValue($instance,$mydbBean);
        
        return $instance;
      }
    ];

    2.1 \seckill\pro\core\annotations\DB.php

    <?php
    
    namespace Core\annotations;
    use Doctrine\Common\Annotations\Annotation\Target;
    
    /**
     * @Annotation
     * @Target({"PROPERTY"})
     */
    class DB
    {
        public $source =  'default';
    }

    2.3 \seckill\pro\app\config\db.php

    <?php
    
    return [
      "default"=>[
        'driver'    =>  'mysql',
        'host'      =>  '10.10.10.234', //我的dicker上的    da9aad4acad8   mysql:5.6
        'port'      =>  '12345',
        'database'  =>  'seckill',
        'username'  =>  'root',
        'password'  =>  '123456',
        'charset'   =>  'utf8mb4',
        'collation' =>  'utf8mb4_general_ci',
        'prefix'    =>  '',
        ] ,
      'test2' =>[
        'driver'    =>  'mysql',
        'host'      =>  'localhost',
        'database'  =>  'test2',
        'username'  =>  'root',
        'password'  =>  '500500',
        'charset'   =>  'utf8',
        'collation' =>  'utf8_unicode_ci',
        'prefix'    =>  '',
      ]
    ];

    2.4 \seckill\pro\app\controllers\UserController.php

    use Core\annotations\DB;
    use Core\init\MyDB;

    加载DB

    /**
         * @DB
         * @var MyDB
         */
        private $db;

    使用

        /**
         * @RequestMapping(value="/test")
         */
        public function test(Response $response )
        {
            return $this->db->table("users")->get();
        }

    3 完整代码

    https://github.com/guainttt/seckill/commit/ad3b3f64adc42ca225b951d43f81ab35f3485911#diff-6e54e7c44f70a08915676a86701ef8877d88d6e6799586f48610bbac299ab51c

  • 相关阅读:
    varnish反向代理
    Asp.Net MVC 3.0
    反向代理(Reverse Proxy)
    Go语言开发Windows应用
    Windows下安装NodeJS和CoffeeScript方法
    数据库设计....
    发布一个开源的c++网络事件库
    非小型电子商务系统设计经验分享 Coding changes the world
    SqlServer查询计划
    cocos2dx总结(一)HelloWord
  • 原文地址:https://www.cnblogs.com/polax/p/16701790.html
Copyright © 2020-2023  润新知