Version 1
[yaffs-website] / web / core / modules / dblog / src / Plugin / rest / resource / DBLogResource.php
1 <?php
2
3 namespace Drupal\dblog\Plugin\rest\resource;
4
5 use Drupal\rest\Plugin\ResourceBase;
6 use Drupal\rest\ResourceResponse;
7 use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
8 use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
9
10 /**
11  * Provides a resource for database watchdog log entries.
12  *
13  * @RestResource(
14  *   id = "dblog",
15  *   label = @Translation("Watchdog database log"),
16  *   uri_paths = {
17  *     "canonical" = "/dblog/{id}"
18  *   }
19  * )
20  */
21 class DBLogResource extends ResourceBase {
22
23   /**
24    * Responds to GET requests.
25    *
26    * Returns a watchdog log entry for the specified ID.
27    *
28    * @param int $id
29    *   The ID of the watchdog log entry.
30    *
31    * @return \Drupal\rest\ResourceResponse
32    *   The response containing the log entry.
33    *
34    * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
35    *   Thrown when the log entry was not found.
36    * @throws \Symfony\Component\HttpKernel\Exception\BadRequestHttpException
37    *   Thrown when no log entry was provided.
38    */
39   public function get($id = NULL) {
40     if ($id) {
41       $record = db_query("SELECT * FROM {watchdog} WHERE wid = :wid", [':wid' => $id])
42         ->fetchAssoc();
43       if (!empty($record)) {
44         return new ResourceResponse($record);
45       }
46
47       throw new NotFoundHttpException(t('Log entry with ID @id was not found', ['@id' => $id]));
48     }
49
50     throw new BadRequestHttpException(t('No log entry ID was provided'));
51   }
52
53 }