| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | Copyright CiviCRM LLC. All rights reserved. | |
| 5 | | | |
| 6 | | This work is published under the GNU AGPLv3 license with some | |
| 7 | | permitted exceptions and without any warranty. For full license | |
| 8 | | and copyright information, see https://civicrm.org/licensing | |
| 9 | +--------------------------------------------------------------------+ |
| 10 | */ |
| 11 | |
| 12 | namespace Civi\Core\Event; |
| 13 | |
| 14 | /** |
| 15 | * Class QueryEvent |
| 16 | * @package Civi\Core\Event |
| 17 | * |
| 18 | * The QueryEvent fires whenever a SQL query is executed. |
| 19 | */ |
| 20 | class QueryEvent extends \Symfony\Component\EventDispatcher\Event { |
| 21 | |
| 22 | /** |
| 23 | * @var string |
| 24 | */ |
| 25 | public $query; |
| 26 | |
| 27 | private $verb = NULL; |
| 28 | |
| 29 | /** |
| 30 | * QueryEvent constructor. |
| 31 | * @param string $query |
| 32 | */ |
| 33 | public function __construct($query) { |
| 34 | $this->query = $query; |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * @return string|FALSE |
| 39 | * Ex: 'SELECT', 'INSERT', 'CREATE', 'ALTER' |
| 40 | * A FALSE value indicates that a singular verb could not be identified. |
| 41 | */ |
| 42 | public function getVerb() { |
| 43 | if ($this->verb === NULL) { |
| 44 | if (preg_match(';(/\*.*/\*\s*)?([a-zA-Z]+) ;', $this->query, $m)) { |
| 45 | $this->verb = strtolower($m[2]); |
| 46 | } |
| 47 | else { |
| 48 | $this->verb = FALSE; |
| 49 | } |
| 50 | } |
| 51 | return $this->verb; |
| 52 | } |
| 53 | |
| 54 | } |