https://github.com/winzou/state-machine
コメントしたけど反応ないので適当に書いてみました。
エスケープしないでHTMLに変数出力してるので、そのまま使わないでください。
app/sample.php
php
1 <! DOCTYPE html >
2 < html lang = " ja " >
3 < head >
4 < meta charset = " utf-8 " > </ head >
5 </ head >
6 < body >
7 <?php
8 require ( 'vendor/autoload.php' ) ;
9
10 class SampleState {
11 private $state = 'private' ;
12
13 public function getState ( )
14 {
15 return $this -> state ;
16 }
17
18 public function setState ( $state )
19 {
20 $this -> state = $state ;
21 }
22 }
23
24 function isAdministrator ( ) {
25 return ( $_REQUEST [ 'admin' ] ?? false ) ;
26 }
27
28 $config = array (
29 'graph' => 'myGraph' ,
30 'property_path' => 'state' ,
31 'states' => array (
32 'private' ,
33 'public' ,
34 'deleted'
35 ) ,
36 'transitions' => array (
37 'publish' => array (
38 'from' => array ( 'private' ) ,
39 'to' => 'public'
40 ) ,
41 'secret' => array (
42 'from' => array ( 'public' ) ,
43 'to' => 'private'
44 ) ,
45 'delete' => array (
46 'from' => array ( 'public' , 'private' ) ,
47 'to' => 'deleted'
48 ) ,
49 'revive' => array (
50 'from' => array ( 'deleted' ) ,
51 'to' => 'private'
52 )
53 ) ,
54 'callbacks' => array (
55 'guard' => array (
56 'guard-cancel' => array (
57 'from' => array ( 'deleted' ) ,
58 'do' => function ( ) {
59 return isAdministrator ( ) ;
60 }
61 )
62 )
63 )
64 ) ;
65
66 // リクエストの読み取り
67 $reqState = $_REQUEST [ 'state' ] ?? "private" ;
68 $reqEvent = $_REQUEST [ 'event' ] ?? "secret" ;
69
70 // 遷移
71 $state = new SampleState ( ) ;
72 $state -> setState ( $reqState ) ;
73 $stateMachine = new \ SM \ StateMachine \ StateMachine ( $state , $config ) ;
74 $stateMachine -> apply ( $reqEvent , true ) ;
75
76 // 表示
77 ?>
78 < form >
79 < div >
80 < span > state: </ span > < span > <?php
81 printf ( '%s<input type="hidden" name="state" value="%s"></input>' , $state -> getState ( ) , $state -> getState ( ) ) ;
82 ?> </ span >
83 </ div >
84 < div >
85 < span > event: </ span > < span > <?php
86 foreach ( $config [ 'transitions' ] as $e => $v ) {
87 $checked = ( $reqEvent === $e ) ? " checked" : "" ;
88 printf ( '<input type="radio" name="event" value="%s"%s>%s</input>' , $e , $checked , $e ) ;
89 }
90 ?> </ span >
91 </ div >
92 < div > <?php
93 printf ( '<input type="checkbox" name="admin"%s>administrator</input>' , ( $_REQUEST [ 'admin' ] ?? false ) ? " checked" : "" ) ;
94 ?> </ div >
95 < div >
96 < button type = " submit " > submit </ button >
97 </ div >
98 </ form >
99 </ body >
100 </ html >
docker用環境構築向け
docker-compose.yml
yaml
1 version : "3"
2
3 services :
4 php :
5 build : ./docker/php
6 volumes :
7 - ./app : /var/www/html/
8 - ./php.ini : /usr/local/etc/php/php.ini
9 ports :
10 - "8011:80"
11 composer :
12 image : composer
13 command : 'composer update'
14 volumes :
15 - ./app : /app
16 links :
17 - php
php.ini
ini
1 post_max_size = 50M
2 upload_max_filesize = 50M
3 memory_limit = 256M
4 [xdebug]
5 xdebug.remote_enable = 1
6 xdebug.remote_autostart = 1
7 xdebug.remote_host = [***YOUR HOST IP ADDRESS***]
8 xdebug.remote_port = 9000
9 xdebug.remote_log = /tmp/xdebug.log
10 zend_extension = /usr/local/lib/php/extensions/no-debug-non-zts-20190902/xdebug.so
※zend_extensionは新しくなると変わります。ビルド時のメッセージを良く見ましょう
docker/php/Dockerfile
Dockerfile
1 FROM php:7-apache
2
3 RUN pecl install xdebug \
4 && docker-php-ext-enable xdebug
5 CMD ["apache2-foreground"]
create_env.sh
sh
1 docker-compose run composer require winzou/state-machine
2 docker-compose down
3 docker-compose up -d
4 # http://localhost:8011/sample.php
.vscode/launch.json
json
1 {
2 "version" : "0.2.0" ,
3 "configurations" : [
4 {
5 "name" : "Listen for XDebug" ,
6 "type" : "php" ,
7 "request" : "launch" ,
8 "port" : 9000 ,
9 "pathMappings" : {
10 "/var/www/html" : "${workspaceRoot}/app"
11 }
12 }
13 ]
14 }