-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathPolicyCommand.php
More file actions
150 lines (131 loc) · 4.13 KB
/
PolicyCommand.php
File metadata and controls
150 lines (131 loc) · 4.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php
declare(strict_types=1);
/**
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
* @link https://cakephp.org CakePHP(tm) Project
* @since 1.0.0
* @license https://opensource.org/licenses/mit-license.php MIT License
*/
namespace Authorization\Command;
use Authorization\IdentityInterface;
use Bake\Command\SimpleBakeCommand;
use Cake\Console\Arguments;
use Cake\Console\ConsoleIo;
use Cake\Console\ConsoleOptionParser;
use Cake\ORM\Query\SelectQuery;
use Cake\Utility\Inflector;
use RuntimeException;
/**
* Bake task for building policy classes
*/
class PolicyCommand extends SimpleBakeCommand
{
/**
* Path to Policy directory
*/
public string $pathFragment = 'Policy/';
protected string $type;
/**
* @inheritDoc
*/
public function name(): string
{
return 'policy';
}
/**
* @inheritDoc
*/
public function fileName(string $name): string
{
if ($this->type === 'table') {
$name .= 'Table';
}
return $name . 'Policy.php';
}
/**
* @inheritDoc
*/
public function template(): string
{
return 'Authorization.policy';
}
/**
* @inheritDoc
*/
public function templateData(Arguments $arguments): array
{
$data = parent::templateData($arguments);
$name = $arguments->getArgument('name');
if (!$name) {
throw new RuntimeException('You must specify name of policy to create.');
}
$name = $this->_getName($name);
$type = $this->type = (string)$arguments->getOption('type');
$suffix = '';
if ($type === 'table') {
$suffix = 'Table';
}
$imports = [];
$className = $data['namespace'] . '\\' . $name;
if ($type === 'table') {
$className = sprintf('%s\Model\Table\%s%s', $data['namespace'], $name, $suffix);
$imports[] = SelectQuery::class;
} elseif ($type === 'entity') {
$className = sprintf('%s\Model\Entity\%s', $data['namespace'], $name);
$imports[] = $className;
}
$imports[] = IdentityInterface::class;
$variable = Inflector::variable($name);
if ($variable === 'user') {
$variable = 'resource';
}
$vars = [
'name' => $name,
'type' => $type,
'suffix' => $suffix,
'variable_name' => $variable,
'classname' => $className,
'imports' => $imports,
];
return $vars + $data;
}
/**
* Gets the option parser instance and configures it.
*
* @param \Cake\Console\ConsoleOptionParser $parser The parser to update.
* @return \Cake\Console\ConsoleOptionParser
*/
public function buildOptionParser(ConsoleOptionParser $parser): ConsoleOptionParser
{
$parser = $this->_setCommonOptions($parser);
return $parser
->setDescription('Bake policy classes for various supported object types.')
->addArgument('name', [
'help' => 'The name of the policy class to create.',
])
->addOption('type', [
'help' => 'The object type to bake a policy for. If only one argument is used, type will be object.',
'default' => 'entity',
'choices' => ['table', 'entity', 'object'],
'required' => true,
]);
}
/**
* Do nothing (for now)
*
* @param string $className The class to bake a test for.
* @param \Cake\Console\Arguments $args The arguments object
* @param \Cake\Console\ConsoleIo $io The consoleio object
* @return void
*/
public function bakeTest(string $className, Arguments $args, ConsoleIo $io): void
{
}
}