類別 yii\db\sqlite\SqlTokenizer
SqlTokenizer 將 SQLite 查詢拆分為個別的 SQL 標記。
它用於從 CREATE TABLE
SQL 程式碼中取得 CHECK
約束資訊。
另請參閱
公開屬性
屬性 | 類型 | 描述 | 定義於 |
---|---|---|---|
$behaviors | yii\base\Behavior[] | 附加到此組件的行為列表。 | yii\base\Component |
$length | 整數 | SQL 程式碼字串長度。 | yii\db\SqlTokenizer |
$offset | 整數 | SQL 程式碼字串目前偏移量。 | yii\db\SqlTokenizer |
$sql | 字串 | SQL 程式碼。 | yii\db\SqlTokenizer |
公開方法
受保護的方法
方法 | 描述 | 定義於 |
---|---|---|
indexAfter() | 傳回 SQL 程式碼中指定字串之後的索引,從指定的偏移量開始。 | yii\db\SqlTokenizer |
isComment() | 傳回目前偏移量是否有註解。 | yii\db\sqlite\SqlTokenizer |
isIdentifier() | 傳回目前偏移量是否有識別符。 | yii\db\sqlite\SqlTokenizer |
isKeyword() | 傳回給定的字串是否為關鍵字。 | yii\db\sqlite\SqlTokenizer |
isOperator() | 傳回目前偏移量是否有運算子。 | yii\db\sqlite\SqlTokenizer |
isStringLiteral() | 傳回目前偏移量是否有字串字面值。 | yii\db\sqlite\SqlTokenizer |
isWhitespace() | 傳回目前偏移量是否有空白字元。 | yii\db\sqlite\SqlTokenizer |
startsWithAnyLongest() | 傳回最長共同前綴是否等於目前偏移量相同長度的 SQL 程式碼。 | yii\db\SqlTokenizer |
substring() | 傳回從指定偏移量開始,給定長度的字串。 | yii\db\SqlTokenizer |
方法詳情
定義於: yii\base\Component::__call()
呼叫指定的非類別方法。
此方法將檢查是否有任何附加的行為 (behavior) 具有指定的名稱方法,並在可用的情況下執行它。
請勿直接呼叫此方法,因為它是 PHP 魔術方法 (magic method),當調用未知方法時,將會隱式地呼叫它。
public mixed __call ( $name, $params ) | ||
$name | 字串 |
方法名稱 |
$params | array |
方法參數 |
return | mixed |
方法傳回值 |
---|---|---|
throws | yii\base\UnknownMethodException |
當呼叫未知方法時 |
public function __call($name, $params)
{
$this->ensureBehaviors();
foreach ($this->_behaviors as $object) {
if ($object->hasMethod($name)) {
return call_user_func_array([$object, $name], $params);
}
}
throw new UnknownMethodException('Calling unknown method: ' . get_class($this) . "::$name()");
}
public void __clone ( ) |
public function __clone()
{
$this->_events = [];
$this->_eventWildcards = [];
$this->_behaviors = null;
}
定義於: yii\db\SqlTokenizer::__construct()
建構子。
public void __construct ( $sql, $config = [] ) | ||
$sql | 字串 |
要進行符號化的 SQL 程式碼。 |
$config | array |
將用於初始化物件屬性的名稱-值組 (name-value pairs) |
public function __construct($sql, $config = [])
{
$this->sql = $sql;
parent::__construct($config);
}
定義於: yii\base\Component::__get()
傳回組件屬性的值。
此方法將依以下順序檢查並採取相應措施
- 由 getter 定義的屬性:傳回 getter 結果
- 行為 (behavior) 的屬性:傳回行為屬性值
請勿直接呼叫此方法,因為它是 PHP 魔術方法 (magic method),當執行 $value = $component->property;
時,將會隱式地呼叫它。
另請參閱 __set()。
public mixed __get ( $name ) | ||
$name | 字串 |
屬性名稱 |
return | mixed |
屬性值或行為 (behavior) 屬性的值 |
---|---|---|
throws | yii\base\UnknownPropertyException |
如果屬性未定義 |
throws | yii\base\InvalidCallException |
如果屬性為唯寫 (write-only)。 |
public function __get($name)
{
$getter = 'get' . $name;
if (method_exists($this, $getter)) {
// read property, e.g. getName()
return $this->$getter();
}
// behavior property
$this->ensureBehaviors();
foreach ($this->_behaviors as $behavior) {
if ($behavior->canGetProperty($name)) {
return $behavior->$name;
}
}
if (method_exists($this, 'set' . $name)) {
throw new InvalidCallException('Getting write-only property: ' . get_class($this) . '::' . $name);
}
throw new UnknownPropertyException('Getting unknown property: ' . get_class($this) . '::' . $name);
}
定義於: yii\base\Component::__isset()
檢查屬性是否已設定,即已定義且非 null。
此方法將依以下順序檢查並採取相應措施
- 由 setter 定義的屬性:傳回屬性是否已設定
- 行為 (behavior) 的屬性:傳回屬性是否已設定
- 針對不存在的屬性傳回
false
請勿直接呼叫此方法,因為它是 PHP 魔術方法 (magic method),當執行 isset($component->property)
時,將會隱式地呼叫它。
public boolean __isset ( $name ) | ||
$name | 字串 |
屬性名稱或事件名稱 |
return | boolean |
具名屬性是否已設定 |
---|
public function __isset($name)
{
$getter = 'get' . $name;
if (method_exists($this, $getter)) {
return $this->$getter() !== null;
}
// behavior property
$this->ensureBehaviors();
foreach ($this->_behaviors as $behavior) {
if ($behavior->canGetProperty($name)) {
return $behavior->$name !== null;
}
}
return false;
}
定義於: yii\base\Component::__set()
設定組件屬性的值。
此方法將依以下順序檢查並採取相應措施
- 由 setter 定義的屬性:設定屬性值
- 格式為 "on xyz" 的事件:將處理常式附加到事件 "xyz"
- 格式為 "as xyz" 的行為 (behavior):附加名為 "xyz" 的行為
- 行為 (behavior) 的屬性:設定行為屬性值
請勿直接呼叫此方法,因為它是 PHP 魔術方法 (magic method),當執行 $component->property = $value;
時,將會隱式地呼叫它。
另請參閱 __get()。
public void __set ( $name, $value ) | ||
$name | 字串 |
屬性名稱或事件名稱 |
$value | mixed |
屬性值 |
throws | yii\base\UnknownPropertyException |
如果屬性未定義 |
---|---|---|
throws | yii\base\InvalidCallException |
如果屬性為唯讀 (read-only)。 |
public function __set($name, $value)
{
$setter = 'set' . $name;
if (method_exists($this, $setter)) {
// set property
$this->$setter($value);
return;
} elseif (strncmp($name, 'on ', 3) === 0) {
// on event: attach event handler
$this->on(trim(substr($name, 3)), $value);
return;
} elseif (strncmp($name, 'as ', 3) === 0) {
// as behavior: attach behavior
$name = trim(substr($name, 3));
$this->attachBehavior($name, $value instanceof Behavior ? $value : Yii::createObject($value));
return;
}
// behavior property
$this->ensureBehaviors();
foreach ($this->_behaviors as $behavior) {
if ($behavior->canSetProperty($name)) {
$behavior->$name = $value;
return;
}
}
if (method_exists($this, 'get' . $name)) {
throw new InvalidCallException('Setting read-only property: ' . get_class($this) . '::' . $name);
}
throw new UnknownPropertyException('Setting unknown property: ' . get_class($this) . '::' . $name);
}
定義於: yii\base\Component::__unset()
將組件屬性設定為 null。
此方法將依以下順序檢查並採取相應措施
- 由 setter 定義的屬性:將屬性值設定為 null
- 行為 (behavior) 的屬性:將屬性值設定為 null
請勿直接呼叫此方法,因為它是 PHP 魔術方法 (magic method),當執行 unset($component->property)
時,將會隱式地呼叫它。
public void __unset ( $name ) | ||
$name | 字串 |
屬性名稱 |
throws | yii\base\InvalidCallException |
如果屬性為唯讀 (read only)。 |
---|
public function __unset($name)
{
$setter = 'set' . $name;
if (method_exists($this, $setter)) {
$this->$setter(null);
return;
}
// behavior property
$this->ensureBehaviors();
foreach ($this->_behaviors as $behavior) {
if ($behavior->canSetProperty($name)) {
$behavior->$name = null;
return;
}
}
throw new InvalidCallException('Unsetting an unknown or read-only property: ' . get_class($this) . '::' . $name);
}
定義於: yii\base\Component::attachBehavior()
將行為附加到此組件。
此方法將基於給定的組態建立行為 (behavior) 物件。之後,將透過呼叫 yii\base\Behavior::attach() 方法將行為物件附加到此元件。
另請參閱 detachBehavior()。
public yii\base\Behavior attachBehavior ( $name, $behavior ) | ||
$name | 字串 |
行為 (behavior) 的名稱。 |
$behavior | string|array|yii\base\Behavior |
行為 (behavior) 組態。這可以是以下其中之一
|
return | yii\base\Behavior |
行為物件 |
---|
public function attachBehavior($name, $behavior)
{
$this->ensureBehaviors();
return $this->attachBehaviorInternal($name, $behavior);
}
定義於: yii\base\Component::attachBehaviors()
將行為列表附加到組件。
每個行為 (behavior) 都以其名稱索引,並且應該是 yii\base\Behavior 物件、指定行為類別的字串,或是用於建立行為的組態陣列。
另請參閱 attachBehavior()。
public void attachBehaviors ( $behaviors ) | ||
$behaviors | array |
要附加到元件的行為 (behavior) 列表 |
public function attachBehaviors($behaviors)
{
$this->ensureBehaviors();
foreach ($behaviors as $name => $behavior) {
$this->attachBehaviorInternal($name, $behavior);
}
}
定義於: yii\base\Component::behaviors()
傳回此組件應表現為的行為列表。
子類別可以覆寫此方法,以指定它們想要表現為的行為 (behavior)。
此方法的傳回值應該是以行為名稱索引的行為物件或組態陣列。行為組態可以是指定行為類別的字串,或是以下結構的陣列
'behaviorName' => [
'class' => 'BehaviorClass',
'property1' => 'value1',
'property2' => 'value2',
]
請注意,行為類別必須從 yii\base\Behavior 擴展。行為可以使用名稱或匿名方式附加。當使用名稱作為陣列鍵時,使用此名稱,稍後可以使用 getBehavior() 檢索行為,或使用 detachBehavior() 分離行為。匿名行為無法檢索或分離。
在此方法中宣告的行為 (behavior) 將會自動 (依需求) 附加到元件。
public array behaviors ( ) | ||
return | array |
行為 (behavior) 組態。 |
---|
public function behaviors()
{
return [];
}
定義於: yii\base\Component::canGetProperty()
傳回一個值,指示是否可以讀取屬性。
如果符合以下條件,則可以讀取屬性
- 類別具有與指定名稱相關聯的 getter 方法 (在這種情況下,屬性名稱不區分大小寫);
- 類別具有具有指定名稱的成員變數 (當
$checkVars
為 true 時); - 附加的行為 (behavior) 具有給定名稱的可讀屬性 (當
$checkBehaviors
為 true 時)。
另請參閱 canSetProperty()。
public boolean canGetProperty ( $name, $checkVars = true, $checkBehaviors = true ) | ||
$name | 字串 |
屬性名稱 |
$checkVars | boolean |
是否將成員變數視為屬性 |
$checkBehaviors | boolean |
是否將行為 (behavior) 的屬性視為此元件的屬性 |
return | boolean |
屬性是否可以讀取 |
---|
public function canGetProperty($name, $checkVars = true, $checkBehaviors = true)
{
if (method_exists($this, 'get' . $name) || $checkVars && property_exists($this, $name)) {
return true;
} elseif ($checkBehaviors) {
$this->ensureBehaviors();
foreach ($this->_behaviors as $behavior) {
if ($behavior->canGetProperty($name, $checkVars)) {
return true;
}
}
}
return false;
}
定義於: yii\base\Component::canSetProperty()
傳回一個值,指示是否可以設定屬性。
如果符合以下條件,則可以寫入屬性
- 類別具有與指定名稱相關聯的 setter 方法 (在這種情況下,屬性名稱不區分大小寫);
- 類別具有具有指定名稱的成員變數 (當
$checkVars
為 true 時); - 附加的行為 (behavior) 具有給定名稱的可寫屬性 (當
$checkBehaviors
為 true 時)。
另請參閱 canGetProperty()。
public boolean canSetProperty ( $name, $checkVars = true, $checkBehaviors = true ) | ||
$name | 字串 |
屬性名稱 |
$checkVars | boolean |
是否將成員變數視為屬性 |
$checkBehaviors | boolean |
是否將行為 (behavior) 的屬性視為此元件的屬性 |
return | boolean |
屬性是否可以寫入 |
---|
public function canSetProperty($name, $checkVars = true, $checkBehaviors = true)
{
if (method_exists($this, 'set' . $name) || $checkVars && property_exists($this, $name)) {
return true;
} elseif ($checkBehaviors) {
$this->ensureBehaviors();
foreach ($this->_behaviors as $behavior) {
if ($behavior->canSetProperty($name, $checkVars)) {
return true;
}
}
}
return false;
}
::class
。
定義於: yii\base\BaseObject::className()
傳回此類別的完整限定名稱。
public static string className ( ) | ||
return | 字串 |
此類別的完整限定名稱 (fully qualified name)。 |
---|
public static function className()
{
return get_called_class();
}
定義於: yii\base\Component::detachBehavior()
從組件中分離行為。
將調用行為 (behavior) 的 yii\base\Behavior::detach() 方法。
public yii\base\Behavior|null detachBehavior ( $name ) | ||
$name | 字串 |
行為 (behavior) 的名稱。 |
return | yii\base\Behavior|null |
分離的行為 (behavior)。如果行為不存在,則為 Null。 |
---|
public function detachBehavior($name)
{
$this->ensureBehaviors();
if (isset($this->_behaviors[$name])) {
$behavior = $this->_behaviors[$name];
unset($this->_behaviors[$name]);
$behavior->detach();
return $behavior;
}
return null;
}
定義於: yii\base\Component::detachBehaviors()
從組件中分離所有行為。
public void detachBehaviors ( ) |
public function detachBehaviors()
{
$this->ensureBehaviors();
foreach ($this->_behaviors as $name => $behavior) {
$this->detachBehavior($name);
}
}
定義於: yii\base\Component::ensureBehaviors()
確保在 behaviors() 中宣告的行為已附加到此組件。
public void ensureBehaviors ( ) |
public function ensureBehaviors()
{
if ($this->_behaviors === null) {
$this->_behaviors = [];
foreach ($this->behaviors() as $name => $behavior) {
$this->attachBehaviorInternal($name, $behavior);
}
}
}
定義於: yii\base\Component::getBehavior()
傳回指定的行為物件。
public yii\base\Behavior|null getBehavior ( $name ) | ||
$name | 字串 |
行為 (behavior) 名稱 |
return | yii\base\Behavior|null |
行為 (behavior) 物件,如果行為不存在,則為 null |
---|
public function getBehavior($name)
{
$this->ensureBehaviors();
return isset($this->_behaviors[$name]) ? $this->_behaviors[$name] : null;
}
定義於: yii\base\Component::getBehaviors()
傳回附加到此組件的所有行為。
public yii\base\Behavior[] getBehaviors ( ) | ||
return | yii\base\Behavior[] |
附加到此元件的行為 (behavior) 列表 |
---|
public function getBehaviors()
{
$this->ensureBehaviors();
return $this->_behaviors;
}
定義於: yii\base\Component::hasEventHandlers()
傳回一個值,指示是否有任何處理常式附加到指定的事件。
public boolean hasEventHandlers ( $name ) | ||
$name | 字串 |
事件名稱 |
return | boolean |
是否有任何處理常式附加到事件。 |
---|
public function hasEventHandlers($name)
{
$this->ensureBehaviors();
if (!empty($this->_events[$name])) {
return true;
}
foreach ($this->_eventWildcards as $wildcard => $handlers) {
if (!empty($handlers) && StringHelper::matchWildcard($wildcard, $name)) {
return true;
}
}
return Event::hasHandlers($this, $name);
}
定義於: yii\base\Component::hasMethod()
傳回一個值,指示是否已定義方法。
如果符合以下條件,則定義了方法
- 類別具有具有指定名稱的方法
- 附加的行為 (behavior) 具有給定名稱的方法 (當
$checkBehaviors
為 true 時)。
public boolean hasMethod ( $name, $checkBehaviors = true ) | ||
$name | 字串 |
屬性名稱 |
$checkBehaviors | boolean |
是否將行為 (behavior) 的方法視為此元件的方法 |
return | boolean |
方法是否已定義 |
---|
public function hasMethod($name, $checkBehaviors = true)
{
if (method_exists($this, $name)) {
return true;
} elseif ($checkBehaviors) {
$this->ensureBehaviors();
foreach ($this->_behaviors as $behavior) {
if ($behavior->hasMethod($name)) {
return true;
}
}
}
return false;
}
定義於: yii\base\Component::hasProperty()
傳回一個值,指示是否為此組件定義了屬性。
如果符合以下條件,則定義了屬性
- 類別具有與指定名稱相關聯的 getter 或 setter 方法 (在這種情況下,屬性名稱不區分大小寫);
- 類別具有具有指定名稱的成員變數 (當
$checkVars
為 true 時); - 附加的行為 (behavior) 具有給定名稱的屬性 (當
$checkBehaviors
為 true 時)。
另請參閱
public boolean hasProperty ( $name, $checkVars = true, $checkBehaviors = true ) | ||
$name | 字串 |
屬性名稱 |
$checkVars | boolean |
是否將成員變數視為屬性 |
$checkBehaviors | boolean |
是否將行為 (behavior) 的屬性視為此元件的屬性 |
return | boolean |
屬性是否已定義 |
---|
public function hasProperty($name, $checkVars = true, $checkBehaviors = true)
{
return $this->canGetProperty($name, $checkVars, $checkBehaviors) || $this->canSetProperty($name, false, $checkBehaviors);
}
定義於: yii\db\SqlTokenizer::indexAfter()
傳回 SQL 程式碼中指定字串之後的索引,從指定的偏移量開始。
protected integer indexAfter ( $string, $offset = null ) | ||
$string | 字串 |
要尋找的字串。 |
$offset | integer|null |
SQL 程式碼偏移量,如果傳遞 |
return | 整數 |
在給定字串之後的索引,或字串結尾索引。 |
---|
protected function indexAfter($string, $offset = null)
{
if ($offset === null) {
$offset = $this->offset;
}
if ($offset + mb_strlen($string, 'UTF-8') > $this->length) {
return $this->length;
}
$afterIndexOf = mb_strpos($this->sql, $string, $offset, 'UTF-8');
if ($afterIndexOf === false) {
$afterIndexOf = $this->length;
} else {
$afterIndexOf += mb_strlen($string, 'UTF-8');
}
return $afterIndexOf;
}
public void init ( ) |
public function init()
{
}
傳回目前偏移量是否有註解。
如果此方法傳回 true
,則必須將 $length
參數設定為匹配字串的長度。
protected boolean isComment ( &$length ) | ||
$length | 整數 |
匹配字串的長度。 |
return | boolean |
目前偏移量是否有註解。 |
---|
protected function isComment(&$length)
{
static $comments = [
'--' => true,
'/*' => true,
];
$length = 2;
if (!isset($comments[$this->substring($length)])) {
return false;
}
if ($this->substring($length) === '--') {
$length = $this->indexAfter("\n") - $this->offset;
} else {
$length = $this->indexAfter('*/') - $this->offset;
}
return true;
}
傳回目前偏移量是否有識別符。
如果此方法傳回 true
,則必須將 $length
參數設定為匹配字串的長度。 它也可以將 $content
設定為將用作符號內容的字串。
protected boolean isIdentifier ( &$length, &$content ) | ||
$length | 整數 |
匹配字串的長度。 |
$content | 字串 |
選填內容,用以取代已比對字串。 |
return | boolean |
目前偏移位置是否有識別字。 |
---|
protected function isIdentifier(&$length, &$content)
{
static $identifierDelimiters = [
'"' => '"',
'[' => ']',
'`' => '`',
];
if (!isset($identifierDelimiters[$this->substring(1)])) {
return false;
}
$delimiter = $identifierDelimiters[$this->substring(1)];
$offset = $this->offset;
while (true) {
$offset = $this->indexAfter($delimiter, $offset + 1);
if ($delimiter === ']' || $this->substring(1, true, $offset) !== $delimiter) {
break;
}
}
$length = $offset - $this->offset;
$content = $this->substring($length - 2, true, $this->offset + 1);
if ($delimiter !== ']') {
$content = strtr($content, ["$delimiter$delimiter" => $delimiter]);
}
return true;
}
傳回給定的字串是否為關鍵字。
此方法可能會將 $content
設定為將用作權杖內容的字串。
protected boolean isKeyword ( $string, &$content ) | ||
$string | 字串 |
要比對的字串。 |
$content | 字串 |
選填內容,用以取代已比對字串。 |
return | boolean |
給定的字串是否為關鍵字。 |
---|
protected function isKeyword($string, &$content)
{
static $keywords = [
'ABORT' => true,
'ACTION' => true,
'ADD' => true,
'AFTER' => true,
'ALL' => true,
'ALTER' => true,
'ANALYZE' => true,
'AND' => true,
'AS' => true,
'ASC' => true,
'ATTACH' => true,
'AUTOINCREMENT' => true,
'BEFORE' => true,
'BEGIN' => true,
'BETWEEN' => true,
'BY' => true,
'CASCADE' => true,
'CASE' => true,
'CAST' => true,
'CHECK' => true,
'COLLATE' => true,
'COLUMN' => true,
'COMMIT' => true,
'CONFLICT' => true,
'CONSTRAINT' => true,
'CREATE' => true,
'CROSS' => true,
'CURRENT_DATE' => true,
'CURRENT_TIME' => true,
'CURRENT_TIMESTAMP' => true,
'DATABASE' => true,
'DEFAULT' => true,
'DEFERRABLE' => true,
'DEFERRED' => true,
'DELETE' => true,
'DESC' => true,
'DETACH' => true,
'DISTINCT' => true,
'DROP' => true,
'EACH' => true,
'ELSE' => true,
'END' => true,
'ESCAPE' => true,
'EXCEPT' => true,
'EXCLUSIVE' => true,
'EXISTS' => true,
'EXPLAIN' => true,
'FAIL' => true,
'FOR' => true,
'FOREIGN' => true,
'FROM' => true,
'FULL' => true,
'GLOB' => true,
'GROUP' => true,
'HAVING' => true,
'IF' => true,
'IGNORE' => true,
'IMMEDIATE' => true,
'IN' => true,
'INDEX' => true,
'INDEXED' => true,
'INITIALLY' => true,
'INNER' => true,
'INSERT' => true,
'INSTEAD' => true,
'INTERSECT' => true,
'INTO' => true,
'IS' => true,
'ISNULL' => true,
'JOIN' => true,
'KEY' => true,
'LEFT' => true,
'LIKE' => true,
'LIMIT' => true,
'MATCH' => true,
'NATURAL' => true,
'NO' => true,
'NOT' => true,
'NOTNULL' => true,
'NULL' => true,
'OF' => true,
'OFFSET' => true,
'ON' => true,
'OR' => true,
'ORDER' => true,
'OUTER' => true,
'PLAN' => true,
'PRAGMA' => true,
'PRIMARY' => true,
'QUERY' => true,
'RAISE' => true,
'RECURSIVE' => true,
'REFERENCES' => true,
'REGEXP' => true,
'REINDEX' => true,
'RELEASE' => true,
'RENAME' => true,
'REPLACE' => true,
'RESTRICT' => true,
'RIGHT' => true,
'ROLLBACK' => true,
'ROW' => true,
'SAVEPOINT' => true,
'SELECT' => true,
'SET' => true,
'TABLE' => true,
'TEMP' => true,
'TEMPORARY' => true,
'THEN' => true,
'TO' => true,
'TRANSACTION' => true,
'TRIGGER' => true,
'UNION' => true,
'UNIQUE' => true,
'UPDATE' => true,
'USING' => true,
'VACUUM' => true,
'VALUES' => true,
'VIEW' => true,
'VIRTUAL' => true,
'WHEN' => true,
'WHERE' => true,
'WITH' => true,
'WITHOUT' => true,
];
$string = mb_strtoupper($string, 'UTF-8');
if (!isset($keywords[$string])) {
return false;
}
$content = $string;
return true;
}
傳回目前偏移量是否有運算子。
如果此方法傳回 true
,則必須將 $length
參數設定為匹配字串的長度。 它也可以將 $content
設定為將用作符號內容的字串。
protected boolean isOperator ( &$length, &$content ) | ||
$length | 整數 |
匹配字串的長度。 |
$content | 字串 |
選填內容,用以取代已比對字串。 |
return | boolean |
目前偏移位置是否有運算子。 |
---|
protected function isOperator(&$length, &$content)
{
static $operators = [
'!=',
'%',
'&',
'(',
')',
'*',
'+',
',',
'-',
'.',
'/',
';',
'<',
'<<',
'<=',
'<>',
'=',
'==',
'>',
'>=',
'>>',
'|',
'||',
'~',
];
return $this->startsWithAnyLongest($operators, true, $length);
}
傳回目前偏移量是否有字串字面值。
如果此方法傳回 true
,則必須將 $length
參數設定為匹配字串的長度。 它也可以將 $content
設定為將用作符號內容的字串。
protected boolean isStringLiteral ( &$length, &$content ) | ||
$length | 整數 |
匹配字串的長度。 |
$content | 字串 |
選填內容,用以取代已比對字串。 |
return | boolean |
目前偏移位置是否有字串字面值。 |
---|
protected function isStringLiteral(&$length, &$content)
{
if ($this->substring(1) !== "'") {
return false;
}
$offset = $this->offset;
while (true) {
$offset = $this->indexAfter("'", $offset + 1);
if ($this->substring(1, true, $offset) !== "'") {
break;
}
}
$length = $offset - $this->offset;
$content = strtr($this->substring($length - 2, true, $this->offset + 1), ["''" => "'"]);
return true;
}
傳回目前偏移量是否有空白字元。
如果此方法傳回 true
,則必須將 $length
參數設定為匹配字串的長度。
protected boolean isWhitespace ( &$length ) | ||
$length | 整數 |
匹配字串的長度。 |
return | boolean |
目前偏移位置是否有空白字元。 |
---|
protected function isWhitespace(&$length)
{
static $whitespaces = [
"\f" => true,
"\n" => true,
"\r" => true,
"\t" => true,
' ' => true,
];
$length = 1;
return isset($whitespaces[$this->substring($length)]);
}
定義於: yii\base\Component::off()
從此組件分離現有的事件處理常式。
此方法與 on() 相反。
注意:如果為事件名稱傳遞了萬用字元模式,則只會移除使用此萬用字元註冊的處理程序,而使用與此萬用字元匹配的純名稱註冊的處理程序將會保留。
另請參閱 on()。
public boolean off ( $name, $handler = null ) | ||
$name | 字串 |
事件名稱 |
$handler | callable|null |
要移除的事件處理程序。 如果為 null,則會移除附加到具名事件的所有處理程序。 |
return | boolean |
如果找到並分離處理程序 |
---|
public function off($name, $handler = null)
{
$this->ensureBehaviors();
if (empty($this->_events[$name]) && empty($this->_eventWildcards[$name])) {
return false;
}
if ($handler === null) {
unset($this->_events[$name], $this->_eventWildcards[$name]);
return true;
}
$removed = false;
// plain event names
if (isset($this->_events[$name])) {
foreach ($this->_events[$name] as $i => $event) {
if ($event[0] === $handler) {
unset($this->_events[$name][$i]);
$removed = true;
}
}
if ($removed) {
$this->_events[$name] = array_values($this->_events[$name]);
return true;
}
}
// wildcard event names
if (isset($this->_eventWildcards[$name])) {
foreach ($this->_eventWildcards[$name] as $i => $event) {
if ($event[0] === $handler) {
unset($this->_eventWildcards[$name][$i]);
$removed = true;
}
}
if ($removed) {
$this->_eventWildcards[$name] = array_values($this->_eventWildcards[$name]);
// remove empty wildcards to save future redundant regex checks:
if (empty($this->_eventWildcards[$name])) {
unset($this->_eventWildcards[$name]);
}
}
}
return $removed;
}
將事件處理常式附加到事件。
事件處理程序必須是有效的 PHP 回呼函式。 以下是一些範例
function ($event) { ... } // anonymous function
[$object, 'handleClick'] // $object->handleClick()
['Page', 'handleClick'] // Page::handleClick()
'handleClick' // global function handleClick()
事件處理程序必須使用以下簽名定義:
function ($event)
其中 $event
是一個 yii\base\Event 物件,其中包含與事件相關聯的參數。
自 2.0.14 版起,您可以將事件名稱指定為萬用字元模式
$component->on('event.group.*', function ($event) {
Yii::trace($event->name . ' is triggered.');
});
另請參閱 off()。
public void on ( $name, $handler, $data = null, $append = true ) | ||
$name | 字串 |
事件名稱 |
$handler | 可呼叫 |
事件處理程序 |
$data | mixed |
事件觸發時,要傳遞給事件處理程序的資料。 當調用事件處理程序時,可以透過 yii\base\Event::$data 存取此資料。 |
$append | boolean |
是否將新的事件處理程序附加到現有處理程序列表的末尾。 如果為 false,則新的處理程序將插入到現有處理程序列表的開頭。 |
public function on($name, $handler, $data = null, $append = true)
{
$this->ensureBehaviors();
if (strpos($name, '*') !== false) {
if ($append || empty($this->_eventWildcards[$name])) {
$this->_eventWildcards[$name][] = [$handler, $data];
} else {
array_unshift($this->_eventWildcards[$name], [$handler, $data]);
}
return;
}
if ($append || empty($this->_events[$name])) {
$this->_events[$name][] = [$handler, $data];
} else {
array_unshift($this->_events[$name], [$handler, $data]);
}
}
定義於: yii\db\SqlTokenizer::startsWithAnyLongest()
傳回最長共同前綴是否等於目前偏移量相同長度的 SQL 程式碼。
protected boolean startsWithAnyLongest ( array &$with, $caseSensitive, &$length = null, &$content = null ) | ||
$with | 字串[] |
要測試的字串。 此方法**將會**修改此參數以加速查找。 |
$caseSensitive | boolean |
是否執行區分大小寫的比較。 |
$length | integer|null |
匹配字串的長度。 |
$content | string|null |
已比對的字串。 |
return | boolean |
是否找到匹配項。 |
---|
protected function startsWithAnyLongest(array &$with, $caseSensitive, &$length = null, &$content = null)
{
if (empty($with)) {
return false;
}
if (!is_array(reset($with))) {
usort($with, function ($string1, $string2) {
return mb_strlen($string2, 'UTF-8') - mb_strlen($string1, 'UTF-8');
});
$map = [];
foreach ($with as $string) {
$map[mb_strlen($string, 'UTF-8')][$caseSensitive ? $string : mb_strtoupper($string, 'UTF-8')] = true;
}
$with = $map;
}
foreach ($with as $testLength => $testValues) {
$content = $this->substring($testLength, $caseSensitive);
if (isset($testValues[$content])) {
$length = $testLength;
return true;
}
}
return false;
}
定義於: yii\db\SqlTokenizer::substring()
傳回從指定偏移量開始,給定長度的字串。
protected string substring ( $length, $caseSensitive = true, $offset = null ) | ||
$length | 整數 |
要傳回的字串長度。 |
$caseSensitive | boolean |
如果為 |
$offset | integer|null |
SQL 程式碼偏移量,如果傳遞 |
return | 字串 |
結果字串,如果沒有任何內容要傳回,則可能為空字串。 |
---|
protected function substring($length, $caseSensitive = true, $offset = null)
{
if ($offset === null) {
$offset = $this->offset;
}
if ($offset + $length > $this->length) {
return '';
}
$cacheKey = $offset . ',' . $length;
if (!isset($this->_substrings[$cacheKey . ',1'])) {
$this->_substrings[$cacheKey . ',1'] = mb_substr($this->sql, $offset, $length, 'UTF-8');
}
if (!$caseSensitive && !isset($this->_substrings[$cacheKey . ',0'])) {
$this->_substrings[$cacheKey . ',0'] = mb_strtoupper($this->_substrings[$cacheKey . ',1'], 'UTF-8');
}
return $this->_substrings[$cacheKey . ',' . (int) $caseSensitive];
}
定義於: yii\db\SqlTokenizer::tokenize()
標記化並傳回程式碼類型標記。
public yii\db\SqlToken tokenize ( ) | ||
return | yii\db\SqlToken |
程式碼類型權杖。 |
---|
public function tokenize()
{
$this->length = mb_strlen($this->sql, 'UTF-8');
$this->offset = 0;
$this->_substrings = [];
$this->_buffer = '';
$this->_token = new SqlToken([
'type' => SqlToken::TYPE_CODE,
'content' => $this->sql,
]);
$this->_tokenStack = new \SplStack();
$this->_tokenStack->push($this->_token);
$this->_token[] = new SqlToken(['type' => SqlToken::TYPE_STATEMENT]);
$this->_tokenStack->push($this->_token[0]);
$this->_currentToken = $this->_tokenStack->top();
while (!$this->isEof()) {
if ($this->isWhitespace($length) || $this->isComment($length)) {
$this->addTokenFromBuffer();
$this->advance($length);
continue;
}
if ($this->tokenizeOperator($length) || $this->tokenizeDelimitedString($length)) {
$this->advance($length);
continue;
}
$this->_buffer .= $this->substring(1);
$this->advance(1);
}
$this->addTokenFromBuffer();
if ($this->_token->getHasChildren() && !$this->_token[-1]->getHasChildren()) {
unset($this->_token[-1]);
}
return $this->_token;
}
public void trigger ( $name, yii\base\Event $event = null ) | ||
$name | 字串 |
事件名稱 |
$event | yii\base\Event|null |
事件實例。 如果未設定,則會建立預設的 yii\base\Event 物件。 |
public function trigger($name, Event $event = null)
{
$this->ensureBehaviors();
$eventHandlers = [];
foreach ($this->_eventWildcards as $wildcard => $handlers) {
if (StringHelper::matchWildcard($wildcard, $name)) {
$eventHandlers[] = $handlers;
}
}
if (!empty($this->_events[$name])) {
$eventHandlers[] = $this->_events[$name];
}
if (!empty($eventHandlers)) {
$eventHandlers = call_user_func_array('array_merge', $eventHandlers);
if ($event === null) {
$event = new Event();
}
if ($event->sender === null) {
$event->sender = $this;
}
$event->handled = false;
$event->name = $name;
foreach ($eventHandlers as $handler) {
$event->data = $handler[1];
call_user_func($handler[0], $event);
// stop further handling if the event is handled
if ($event->handled) {
return;
}
}
}
// invoke class-level attached handlers
Event::trigger($this, $name, $event);
}
註冊 或 登入 以發表評論。