PHP code example of dcr / framework

1. Go to this page and download the library: Download dcr/framework library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

dcr / framework example snippets


 declare(strict_types=1);

namespace App\Controller;

use App\Middleware\AuthMiddleware;
use App\Middleware\TestMiddleware;
use App\Service\TestService;
use Dcr\Annotation\Mapping\Middlewares;
use Dcr\Annotation\Mapping\RequestMapping;
use DI\Attribute\Inject;

class MIddlewareController extends Controller
{
    #[Inject]
    public TestService $testService;

    #[RequestMapping(methods: 'GET , POST', path:'/test/middleware')]
    #[Middlewares(AuthMiddleware::class, TestMiddleware::class)]
    public function test1()
    {
//        $this->testService->testDi();
        return apiResponse([]);
    }
}

//->all()  ->get()  ->post() 等方法
ApplicationContext::getContainer()->get(Request::class)->all();
//di()->(Request::class)->all();

$redis = Redis::connection();
//->setex ->get ->del ->setnx 等方法 和predis一致


declare(strict_types = 1);
namespace App\Model;
use Illuminate\Database\Eloquent\Model;

/**
 * Class UserModel
 * @package App\Model
 * @see https://github.com/illuminate/database
 */
class UserModel extends Model
{
    protected $table = 'user';
}



   #[RequestMapping(methods: "GET , POST", path:"/test/test4")]
    public function test4($request, $response)
    {
        $validate = Validation::check($this->request->post ?? [], [
            // add rule
            ['title', 'min', 40],
            ['freeTime', 'number'],
        ]);

        if ($validate->isFail()) {
            var_dump($validate->getErrors());
            var_dump($validate->firstError());
        }

        // $postData = $v->all(); // 原始数据
        $safeData = $validate->getSafeData(); // 验证通过的安全数据

        return $safeData
    }

    #[Inject]
    public Config $config;

    #[RequestMapping(methods: "GET , POST", path:"/test/config")]
    public function config()
    {
        //di()->get(Config::class)->get('app.debug');
        return $this->config->get('app.debug');
    }



namespace App\Middleware;

use App\Exception\RuntimeException;
use App\Middleware\Contract\MiddlewareInterface;

class TestMiddleware implements MiddlewareInterface
{
    public function handle()
    {
        return static function ($request, $next) {
//            throw new RuntimeException('未登录');
            $response = $next->handle($request);
            return $response;
        };
    }
}



declare(strict_types = 1);

namespace App\Console\Command;

use Inhere\Console\IO\Input;
use Inhere\Console\IO\Output;
use Toolkit\PFlag\FlagsParser;

class Test extends \Inhere\Console\Command
{
    // php artisan test 
    protected static string $name = 'test';

    protected static string $desc = 'print system ENV information';

    protected function configFlags(FlagsParser $fs): void
    {
        // 绑定选项
        $fs->addOptByRule('update, up', 'bool;update linux command docs to latest');
        $fs->addOptByRule('init, i', 'bool;update linux command docs to latest');
        $fs->addOptByRule('search, s', 'string;input keywords for search');

        // - 这里没有设置必须 可以不传,获取到就是空string
        $fs->addArg('keywords', 'the keywords for search or show docs', 'string');
    }

    protected function execute(Input $input, Output $output): void
    {
        $keywords = $this->flags->getOpt('search', 23);
        var_dump($keywords);
        $output->info('hello world ...');
    }
}



declare(strict_types = 1);
namespace App\Event;
use Symfony\Contracts\EventDispatcher\Event;

/**
 * Class TestEvent
 */
class TestEvent extends Event
{
    public const NAME = 'order.placed';
    protected $params;
    public function __construct($params)
    {
        $this->params = $params;
    }
    public function getParams()
    {
        return $this->params;
    }
}




declare(strict_types = 1);
namespace App\Listener;

use App\Event\TestEvent;
use App\Utils\LogBase;
use Symfony\Contracts\EventDispatcher\Event;

class TestEventListener implements BaseListenerInterface
{
    public function process(object $event)
    {
        echo '打印参数'.PHP_EOL;
        var_dump($event->getParams());
    }
}



  #[RequestMapping(methods: 'GET , POST', path:'/test/event')]
    public function event(): string
    {
        $params = [
            'test' => 23,
        ];
        event(new TestEvent($params),TestEvent::NAME);
        // 初始化事件分发器

        return apiResponse([]);
    }

    #[RequestMapping(methods: "GET , POST", path:"/test/event")]
    public function event($request, $response): array
    {
        $params = [
            'test' => 23,
        ];
        $dispatcher = EventInstance::instance();
    $dispatcher->dispatch(new TestEvent($params), TestEvent::NAME);
    }


namespace App\Crontab;
use App\Crontab\Contract\CrontabInterface;
/**
 *
 * 需要在 /config/crontab.php 配置
 */
class TestCrontab implements CrontabInterface
{
    public $name = 'test';
    public $desc = 'just test demo';

    public function execute(): void
    {
        echo 'test crontab'.PHP_EOL;
//        di()->get(TestRepository::class)->test1();
//        di()->get(TestRepository::class)->fromRepos();
    }
}

php ./bin/startWs.php start   

php ./bin/crontab.php start  

php artisan test

./app/TestController.php