luoxiaojun / yaravel
Yaf framework skeleton for Laravel User
Installs: 0
Dependents: 0
Suggesters: 0
Security: 0
Stars: 2
Watchers: 2
Forks: 0
Open Issues: 0
Type:project
Requires
- php: >=7.1
- ext-json: *
- ext-yaf: *
- cviebrock/laravel-elasticsearch: ~3.4
- guzzlehttp/guzzle: ^6.3
- guzzlehttp/promises: ^1.3
- guzzlehttp/psr7: ^1.4
- illuminate/bus: 5.5.*
- illuminate/cache: 5.5.*
- illuminate/config: 5.5.*
- illuminate/contracts: 5.5.*
- illuminate/database: 5.5.*
- illuminate/events: 5.5.*
- illuminate/log: 5.5.*
- illuminate/pagination: 5.5.*
- illuminate/queue: 5.5.*
- illuminate/redis: 5.5.*
- illuminate/support: 5.5.*
- illuminate/validation: 5.5.*
- league/flysystem: ^1.0.8
- league/plates: ~3.0
- monolog/monolog: ~1.0
- nesbot/carbon: ^1.24.1
- openzipkin/zipkin: 1.3.2
- phpoffice/phpspreadsheet: ~1.12.0
- psr/container: ^1.0
- psr/http-message: ~1.0
- psr/log: ~1.0
- psy/psysh: ~0.10.4
- sentry/sentry-laravel: ^0.10.0
- swiftmailer/swiftmailer: ^6.0
- symfony/console: 3.*
- vlucas/phpdotenv: ~2.2
Requires (Dev)
- filp/whoops: ~2.1@stable
- friendsofphp/php-cs-fixer: ~2.7.0@stable
- fzaninotto/faker: ~1.4
- mockery/mockery: ~1.0
- overtrue/phplint: dev-master
- phpmd/phpmd: ~2.6@stable
- phpunit/phpunit: ~6.0
- sebastian/phpcpd: ~2.0@stable
- sebastian/phpdcd: ~1.0@stable
- squizlabs/php_codesniffer: 3.0.x-dev
- symfony/var-dumper: ~3.0
Suggests
- ext-pcntl: *
- illuminate/database: The best PHP ORM Engine from Laravel.
- illuminate/validation: The form validation component from Laravel.
- league/plates: Plates is a native PHP template system that's fast, easy to use and easy to extend.
This package is auto-updated.
Last update: 2024-11-11 13:27:22 UTC
README
The Yaf testable skeleton and composer supported.
Requirement
- PHP >= 7.0
- Yaf >= 3.0
Installation
- Update
yaf.ini
:
[yaf]
yaf.use_namespace=1
yaf.use_spl_autoload=1
...
- Create project.
$ composer create-project overtrue/yaf-skeleton myapp -vvv
-
Web server rewrite rules:
Apache
#.htaccess RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteRule .* index.php
Nginx
server { listen 80; server_name myapp.com; root /path/to/myapp; index index.php index.html index.htm; if (!-e $request_filename) { rewrite ^/(.*) /index.php/$1 last; } }
Lighttpd
$HTTP["host"] =~ "(www.)?myapp.com$" { url.rewrite = ( "^/(.+)/?$" => "/index.php/$1", ) }
Application structor
├── .scripts
│ ├── .gitkeep
│ ├── common.sh
│ ├── job_phpcs.sh
│ ├── job_phpmd.sh
│ ├── job_phpunit.sh
│ ├── sami.phar
│ └── sami.php
├── app
│ ├── commands # sora commands (namespace:\App\Commands)
│ ├── controllers # Yaf Controllers (namespace:\)
│ ├── exceptions # Exceptions (namespace:\App\Exceptions)
│ ├── facades # Service Facades (namespace:\)
│ ├── plugins # Yaf plugins (namespace:\)
│ ├── presenters # Object presenters (namespace:\App\Presenters)
│ ├── services # Services, the 3rd service bridges (namespace:\App\Services)
│ ├── traits # Traits (namespace:\App\Traits)
│ ├── views # Template files
│ ├── helpers.php # Herlpers
│ ├── Bootstrap.php # Yaf Bootstrap file
├── config
│ ├── application.ini # Yaf config file
├── public # web extrence
│ └── index.php
├── sora # The command line tool
├── tests # Unit tests
└── vendor #
├── phpunit.xml.dist # PHPUnit config file
├── .gitignore
├── .php_cs # PHP-CS-Fixer config file
├── composer.json
├── composer.lock
├── README.md
Controllers
$ ./sora make:controller Foo_Bar # or:foo_bar/FooBar/FooBarController # # /www/myapp/app/controllers/Foo/Bar.php Created! # /www/myapp/tests/controllers/Foo/BarTest.php Created!
All controllers are created in the app/controllers
directory,and test files are also created in the tests/controllers
directorty.
Of course, you can also create tests independently:
$ ./sora make:test Foo_Bar # Also supports multiple type controller names # /www/myapp/tests/controllers/Foo/BarTest.php Created!
The handle() method
The controller entry method handle()
:
<?php class ExampleController extends BaseController { public function handle() { return 'Hello world!'; // return json(['foo' => 'bar']); // return redirect('https://easywechat.com'); // return view('welcome', ['name' => 'MyApp']); // template engine require. } }
Views
There is no built-in template engine. If you need to use a PHP template, we recommend that you use Plates, Plates is a native PHP template system that's fast, easy to use and easy to extend.
$ composer require league/plates -vvv
You can use view(string $template, array $data)
helper in controller handle
method as a result.
for example:
public function handle() { $data = [ 'name' => 'overtrue', 'age' => 28, ]; return view('profile-page', $data); }
<!--app/views/profile-page.php:--> <h1><?= $name ?></h1> <p><?= $age ?></p>
More usage please read the Plates Docs.
Unit tests
The difficulty of writing unit tests is inversely proportional to the quality of your code. The higher the code quality, the lower the difficulty of unit testing, so design your code well.
The unit test for creating the controller can be done with the following command:
$ ./sora make:test Foo_BarController
Write test cases
To create a controller test object, use,you can use the mock_controller
function:
$controller = mock_controller(Foo_BarController::class); // Indicates the method to mock, and the protected method is also mockable $controller = mock_controller(Foo_BarController::class, ['getUsers', 'getApp']);
Assertion
We have such a controller:
... public function handle() { $params = Reuqest::only('uids', 'screen_name', 'trim_status', 'has_extend', 'simplify', 'is_encoded'); $users = $this->get('main.users.show_batch', $params); return $users; } ...
So the test should cover the above three behaviors:
public function testHandle() { $input = [ 'uids' => '2193182644', 'screen_name' => '安正超', 'trim_status' => 0, 'has_extend' => 1, 'simplify' => 0, 'is_encoded' => 0, 'foo' => 'bar', ]; Request::shouldReceive('only') ->with('uids', 'screen_name', 'trim_status', 'has_extend', 'simplify', 'is_encoded') ->andReturn($input); $controller = mock_controller(Users_Show_BatchController::class, ['get']); // mock the `get` method $controller->shouldReceive('get')->with('main.users.show_batch', array_except($_GET, ['foo'])) ->andReturn('foo') ->once(); $response = $controller->handle(); $this->assertSame('foo', $response); }
Facade Assertion
Request::shouldReceive('get')->with('mid')->andReturn('mock-mid')->once(); Log::shouldReceive('action')->with(48, 'oid', 'ext')->once(); ..
Built-in auxiliary assertion methods
$this->shouldAbort($message, $code);
They are used to correspond to exceptions thrown by abort($message, $code);
in the controller
Some helper methods in test case classes
Mock request method:
$this->method('post');
Mock request uri
:
$this->uri('/foo/bar?uid=12345');
Mock config:
$this->config('foo', 'bar');
Mock request IP:
$this->ip('127.0.0.1');
Mock $_SERVER vars:
$this->server('REQUEST_URI', '/foo/bar');
Docs
- Yaf Docs: http://www.php.net/manual/en/book.yaf.php
- Plates Docs: http://platesphp.com/v3/
- PHPUnit Docs: https://phpunit.de/manual/current/zh_cn/phpunit-book.html
- Mockery Docs: http://docs.mockery.io/en/latest/
PHP 扩展包开发
想知道如何从零开始构建 PHP 扩展包?
请关注我的实战课程,我会在此课程中分享一些扩展开发经验 —— 《PHP 扩展包实战教程 - 从入门到发布》
License
MIT
TODO
- domains
- zipkin
- clear lang resources
- support/helpers 提示不使用
- 测试redis queue