Skip to content

Commit c172c55

Browse files
committed
feat(developer): Replace annotations with attributes
Signed-off-by: Joas Schilling <[email protected]>
1 parent 5e9e082 commit c172c55

File tree

6 files changed

+103
-163
lines changed

6 files changed

+103
-163
lines changed

developer_manual/app_development/tutorial.rst

Lines changed: 49 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -97,14 +97,16 @@ On the server side we need to register a callback that is executed once the requ
9797
9898
This route calls the controller **OCA\\notestutorial\\PageController->index()** method which is defined in **notestutorial/lib/Controller/PageController.php**. The controller returns a :doc:`template <../basics/front-end/templates>`, in this case **notestutorial/templates/main.php**:
9999

100-
.. note:: **@NoAdminRequired** and **@NoCSRFRequired** in the comments above the method turn off security checks, see `Authentication on Controllers <../basics/controllers.html#authentication>`__
100+
.. note:: The ``#[NoAdminRequired]`` and ``#[NoCSRFRequired]`` attributes on the methods turn off security checks, see `Authentication on Controllers <../basics/controllers.html#authentication>`__
101101

102102
.. code-block:: php
103103
104104
<?php
105105
namespace OCA\NotesTutorial\Controller;
106106
107107
use OCP\IRequest;
108+
use OCP\AppFramework\Http\Attribute\NoAdminRequired;
109+
use OCP\AppFramework\Http\Attribute\NoCSRFRequired;
108110
use OCP\AppFramework\Http\TemplateResponse;
109111
use OCP\AppFramework\Controller;
110112
@@ -114,10 +116,8 @@ This route calls the controller **OCA\\notestutorial\\PageController->index()**
114116
parent::__construct($appName, $request);
115117
}
116118
117-
/**
118-
* @NoAdminRequired
119-
* @NoCSRFRequired
120-
*/
119+
#[NoAdminRequired]
120+
#[NoCSRFRequired]
121121
public function index() {
122122
return new TemplateResponse('notestutorial', 'main');
123123
}
@@ -133,63 +133,41 @@ Since the route which returns the initial HTML has been taken care of, the contr
133133
134134
use OCP\IRequest;
135135
use OCP\AppFramework\Controller;
136+
use OCP\AppFramework\Http\Attribute\NoAdminRequired;
136137
137138
class NoteController extends Controller {
138139
139140
public function __construct(string $appName, IRequest $request){
140141
parent::__construct($appName, $request);
141142
}
142143
143-
/**
144-
* @NoAdminRequired
145-
*/
144+
#[NoAdminRequired]
146145
public function index() {
147146
// empty for now
148147
}
149148
150-
/**
151-
* @NoAdminRequired
152-
*
153-
* @param int $id
154-
*/
149+
#[NoAdminRequired]
155150
public function show(int $id) {
156151
// empty for now
157152
}
158153
159-
/**
160-
* @NoAdminRequired
161-
*
162-
* @param string $title
163-
* @param string $content
164-
*/
154+
#[NoAdminRequired]
165155
public function create(string $title, string $content) {
166156
// empty for now
167157
}
168158
169-
/**
170-
* @NoAdminRequired
171-
*
172-
* @param int $id
173-
* @param string $title
174-
* @param string $content
175-
*/
159+
#[NoAdminRequired]
176160
public function update(int $id, string $title, string $content) {
177161
// empty for now
178162
}
179163
180-
/**
181-
* @NoAdminRequired
182-
*
183-
* @param int $id
184-
*/
164+
#[NoAdminRequired]
185165
public function destroy(int $id) {
186166
// empty for now
187167
}
188168
189169
}
190170
191-
.. note:: The parameters are extracted from the request body and the URL using the controller method's variable names. Since PHP does not support type hints for primitive types such as ints and booleans, we need to add them as annotations in the comments. In order to type cast a parameter to an int, add **@param int $parameterName**
192-
193171
Now the controller methods need to be connected to the corresponding URLs in the **notestutorial/appinfo/routes.php** file:
194172

195173
.. code-block:: php
@@ -225,8 +203,8 @@ Database
225203

226204
Now that the routes are set up and connected the notes should be saved in the
227205
database. To do that first create a :doc:`database migration <../basics/storage/migrations>`
228-
by creating a file **notestutorial/lib/Migration/VersionXXYYZZDateYYYYMMDDHHSSAA.php**,
229-
so for example **notestutorial/lib/Migration/Version000000Date20181013124731.php**""
206+
by creating a file ``notestutorial/lib/Migration/VersionXYYYDateYYYYMMDDHHSSAA.php``,
207+
so for example version 1.4.3 goes with ``notestutorial/lib/Migration/Version1004Date20181013124731.php``
230208

231209
.. code-block:: php
232210
@@ -239,7 +217,7 @@ so for example **notestutorial/lib/Migration/Version000000Date20181013124731.php
239217
use OCP\Migration\SimpleMigrationStep;
240218
use OCP\Migration\IOutput;
241219
242-
class Version1400Date20181013124731 extends SimpleMigrationStep {
220+
class Version1004Date20181013124731 extends SimpleMigrationStep {
243221
244222
/**
245223
* @param IOutput $output
@@ -281,7 +259,7 @@ To create the tables in the database, run the :ref:`migration <migration_consol
281259

282260
php ./occ migrations:execute <appId> <versionNumber>
283261

284-
Example: sudo -u www-data php ./occ migrations:execute photos 000000Date20201002183800
262+
Example: sudo -u www-data php ./occ migrations:execute notestutorial 1004Date20201002183800
285263

286264
.. note:: To trigger the table creation/alteration when user updating the app, update the :doc:`version tag <info>` in **notestutorial/appinfo/info.xml** . migration will be executed when user reload page after app upgrade
287265

@@ -400,6 +378,7 @@ You can pass in the mapper by adding it as a type hinted parameter. Nextcloud wi
400378
401379
use OCP\IRequest;
402380
use OCP\AppFramework\Http;
381+
use OCP\AppFramework\Http\Attribute\NoAdminRequired;
403382
use OCP\AppFramework\Http\DataResponse;
404383
use OCP\AppFramework\Controller;
405384
@@ -417,18 +396,12 @@ You can pass in the mapper by adding it as a type hinted parameter. Nextcloud wi
417396
$this->userId = $userId;
418397
}
419398
420-
/**
421-
* @NoAdminRequired
422-
*/
399+
#[NoAdminRequired]
423400
public function index(): DataResponse {
424401
return new DataResponse($this->mapper->findAll($this->userId));
425402
}
426403
427-
/**
428-
* @NoAdminRequired
429-
*
430-
* @param int $id
431-
*/
404+
#[NoAdminRequired]
432405
public function show(int $id): DataResponse {
433406
try {
434407
return new DataResponse($this->mapper->find($id, $this->userId));
@@ -437,12 +410,7 @@ You can pass in the mapper by adding it as a type hinted parameter. Nextcloud wi
437410
}
438411
}
439412
440-
/**
441-
* @NoAdminRequired
442-
*
443-
* @param string $title
444-
* @param string $content
445-
*/
413+
#[NoAdminRequired]
446414
public function create(string $title, string $content): DataResponse {
447415
$note = new Note();
448416
$note->setTitle($title);
@@ -451,13 +419,7 @@ You can pass in the mapper by adding it as a type hinted parameter. Nextcloud wi
451419
return new DataResponse($this->mapper->insert($note));
452420
}
453421
454-
/**
455-
* @NoAdminRequired
456-
*
457-
* @param int $id
458-
* @param string $title
459-
* @param string $content
460-
*/
422+
#[NoAdminRequired]
461423
public function update(int $id, string $title, string $content): DataResponse {
462424
try {
463425
$note = $this->mapper->find($id, $this->userId);
@@ -469,11 +431,7 @@ You can pass in the mapper by adding it as a type hinted parameter. Nextcloud wi
469431
return new DataResponse($this->mapper->update($note));
470432
}
471433
472-
/**
473-
* @NoAdminRequired
474-
*
475-
* @param int $id
476-
*/
434+
#[NoAdminRequired]
477435
public function destroy(int $id): DataResponse {
478436
try {
479437
$note = $this->mapper->find($id, $this->userId);
@@ -645,6 +603,7 @@ Now we can wire up the trait and the service inside the **NoteController**:
645603
namespace OCA\NotesTutorial\Controller;
646604
647605
use OCP\IRequest;
606+
use OCP\AppFramework\Http\Attribute\NoAdminRequired;
648607
use OCP\AppFramework\Http\DataResponse;
649608
use OCP\AppFramework\Controller;
650609
@@ -664,52 +623,31 @@ Now we can wire up the trait and the service inside the **NoteController**:
664623
$this->userId = $userId;
665624
}
666625
667-
/**
668-
* @NoAdminRequired
669-
*/
626+
#[NoAdminRequired]
670627
public function index(): DataResponse {
671628
return new DataResponse($this->service->findAll($this->userId));
672629
}
673630
674-
/**
675-
* @NoAdminRequired
676-
*
677-
* @param int $id
678-
*/
631+
#[NoAdminRequired]
679632
public function show(int $id): DataResponse {
680633
return $this->handleNotFound(function () use ($id) {
681634
return $this->service->find($id, $this->userId);
682635
});
683636
}
684637
685-
/**
686-
* @NoAdminRequired
687-
*
688-
* @param string $title
689-
* @param string $content
690-
*/
638+
#[NoAdminRequired]
691639
public function create(string $title, string $content) {
692640
return $this->service->create($title, $content, $this->userId);
693641
}
694642
695-
/**
696-
* @NoAdminRequired
697-
*
698-
* @param int $id
699-
* @param string $title
700-
* @param string $content
701-
*/
643+
#[NoAdminRequired]
702644
public function update(int $id, string $title, string $content): DataResponse {
703645
return $this->handleNotFound(function () use ($id, $title, $content): Note {
704646
return $this->service->update($id, $title, $content, $this->userId);
705647
});
706648
}
707649
708-
/**
709-
* @NoAdminRequired
710-
*
711-
* @param int $id
712-
*/
650+
#[NoAdminRequired]
713651
public function destroy(int $id): DataResponse {
714652
return $this->handleNotFound(function () use ($id): Note {
715653
return $this->service->delete($id, $this->userId);
@@ -958,6 +896,9 @@ With that in mind create a new controller in **notestutorial/lib/Controller/Note
958896
namespace OCA\NotesTutorial\Controller;
959897
960898
use OCP\IRequest;
899+
use OCP\AppFramework\Http\Attribute\CORS;
900+
use OCP\AppFramework\Http\Attribute\NoAdminRequired;
901+
use OCP\AppFramework\Http\Attribute\NoCSRFRequired;
961902
use OCP\AppFramework\Http\DataResponse;
962903
use OCP\AppFramework\ApiController;
963904
@@ -977,63 +918,42 @@ With that in mind create a new controller in **notestutorial/lib/Controller/Note
977918
$this->userId = $userId;
978919
}
979920
980-
/**
981-
* @CORS
982-
* @NoCSRFRequired
983-
* @NoAdminRequired
984-
*/
921+
#[CORS]
922+
#[NoAdminRequired]
923+
#[NoCSRFRequired]
985924
public function index() {
986925
return new DataResponse($this->service->findAll($this->userId));
987926
}
988927
989-
/**
990-
* @CORS
991-
* @NoCSRFRequired
992-
* @NoAdminRequired
993-
*
994-
* @param int $id
995-
*/
996-
public function show($id) {
928+
#[CORS]
929+
#[NoAdminRequired]
930+
#[NoCSRFRequired]
931+
public function show(int $id) {
997932
return $this->handleNotFound(function () use ($id) {
998933
return $this->service->find($id, $this->userId);
999934
});
1000935
}
1001936
1002-
/**
1003-
* @CORS
1004-
* @NoCSRFRequired
1005-
* @NoAdminRequired
1006-
*
1007-
* @param string $title
1008-
* @param string $content
1009-
*/
1010-
public function create($title, $content) {
937+
#[CORS]
938+
#[NoAdminRequired]
939+
#[NoCSRFRequired]
940+
public function create(string $title, string $content) {
1011941
return $this->service->create($title, $content, $this->userId);
1012942
}
1013943
1014-
/**
1015-
* @CORS
1016-
* @NoCSRFRequired
1017-
* @NoAdminRequired
1018-
*
1019-
* @param int $id
1020-
* @param string $title
1021-
* @param string $content
1022-
*/
1023-
public function update($id, $title, $content) {
944+
#[CORS]
945+
#[NoAdminRequired]
946+
#[NoCSRFRequired]
947+
public function update(int $id, string $title, string $content) {
1024948
return $this->handleNotFound(function () use ($id, $title, $content) {
1025949
return $this->service->update($id, $title, $content, $this->userId);
1026950
});
1027951
}
1028952
1029-
/**
1030-
* @CORS
1031-
* @NoCSRFRequired
1032-
* @NoAdminRequired
1033-
*
1034-
* @param int $id
1035-
*/
1036-
public function destroy($id) {
953+
#[CORS]
954+
#[NoAdminRequired]
955+
#[NoCSRFRequired]
956+
public function destroy(int $id) {
1037957
return $this->handleNotFound(function () use ($id) {
1038958
return $this->service->delete($id, $this->userId);
1039959
});

0 commit comments

Comments
 (0)