1
0
mirror of https://github.com/processwire/processwire.git synced 2025-08-14 10:45:54 +02:00

Minor optimizations to Wire class

This commit is contained in:
Ryan Cramer
2021-05-05 13:55:59 -04:00
parent 1906d94a9c
commit 8d9a66fc38
2 changed files with 46 additions and 22 deletions

View File

@@ -138,6 +138,14 @@ class WireDatabaseQueryException extends WireException {}
*/ */
class WireCSRFException extends WireException {} class WireCSRFException extends WireException {}
/**
* Thrown when fatal error from $files API var (or related) occurs
*
* @since 3.0.178
*
*/
class WireFilesException extends WireException {}
/** /**
* Thrown when a requested Process or Process method is requested that doesnt exist * Thrown when a requested Process or Process method is requested that doesnt exist
* *

View File

@@ -19,7 +19,7 @@
* #pw-summary-changes Methods to support tracking and retrieval of changes made to the object. * #pw-summary-changes Methods to support tracking and retrieval of changes made to the object.
* #pw-summary-hooks Methods for managing hooks for an object instance or class. * #pw-summary-hooks Methods for managing hooks for an object instance or class.
* *
* ProcessWire 3.x, Copyright 2020 by Ryan Cramer * ProcessWire 3.x, Copyright 2021 by Ryan Cramer
* https://processwire.com * https://processwire.com
* *
* #pw-use-constants * #pw-use-constants
@@ -187,7 +187,8 @@ abstract class Wire implements WireTranslatable, WireFuelable, WireTrackable {
*/ */
public static function setFuel($name, $value, $lock = false) { public static function setFuel($name, $value, $lock = false) {
$wire = ProcessWire::getCurrentInstance(); $wire = ProcessWire::getCurrentInstance();
if($wire->wire('log')) $wire->wire('log')->deprecatedCall(); $log = $wire->wire()->log;
if($log) $log->deprecatedCall();
$wire->fuel()->set($name, $value, $lock); $wire->fuel()->set($name, $value, $lock);
} }
@@ -205,7 +206,8 @@ abstract class Wire implements WireTranslatable, WireFuelable, WireTrackable {
*/ */
public static function getFuel($name = '') { public static function getFuel($name = '') {
$wire = ProcessWire::getCurrentInstance(); $wire = ProcessWire::getCurrentInstance();
if($wire->wire('log')) $wire->wire('log')->deprecatedCall(); $log = $wire->wire()->log;
if($log) $log->deprecatedCall();
if(empty($name)) return $wire->fuel(); if(empty($name)) return $wire->fuel();
return $wire->fuel()->$name; return $wire->fuel()->$name;
} }
@@ -222,7 +224,8 @@ abstract class Wire implements WireTranslatable, WireFuelable, WireTrackable {
*/ */
public static function getAllFuel() { public static function getAllFuel() {
$wire = ProcessWire::getCurrentInstance(); $wire = ProcessWire::getCurrentInstance();
if($wire->wire('log')) $wire->wire('log')->deprecatedCall(); $log = $wire->wire()->log;
if($log) $log->deprecatedCall();
return $wire->fuel(); return $wire->fuel();
} }
@@ -240,7 +243,8 @@ abstract class Wire implements WireTranslatable, WireFuelable, WireTrackable {
*/ */
public function fuel($name = '') { public function fuel($name = '') {
$wire = $this->wire(); $wire = $this->wire();
if($wire->wire('log')) $wire->wire('log')->deprecatedCall(); $log = $wire->wire()->log;
if($log) $log->deprecatedCall();
return $wire->fuel($name); return $wire->fuel($name);
} }
@@ -262,7 +266,7 @@ abstract class Wire implements WireTranslatable, WireFuelable, WireTrackable {
* *
*/ */
public function useFuel($useFuel = null) { public function useFuel($useFuel = null) {
if(!is_null($useFuel)) $this->useFuel = $useFuel ? true : false; if($useFuel !== null) $this->useFuel = $useFuel ? true : false;
return $this->useFuel; return $this->useFuel;
} }
@@ -1359,8 +1363,8 @@ abstract class Wire implements WireTranslatable, WireFuelable, WireTrackable {
* *
*/ */
public function ___trackException(\Exception $e, $severe = true, $text = null) { public function ___trackException(\Exception $e, $severe = true, $text = null) {
$config = $this->wire('config'); $config = $this->wire()->config;
$log = $this->wire('log'); $log = $this->wire()->log;
$msg = $e->getMessage(); $msg = $e->getMessage();
if($text !== null) { if($text !== null) {
if($text === true) $text = $msg; if($text === true) $text = $msg;
@@ -1371,7 +1375,7 @@ abstract class Wire implements WireTranslatable, WireFuelable, WireTrackable {
$msg .= " (in " . str_replace($config->paths->root, '/', $e->getFile()) . " line " . $e->getLine() . ")"; $msg .= " (in " . str_replace($config->paths->root, '/', $e->getFile()) . " line " . $e->getLine() . ")";
$log->save('exceptions', $msg); $log->save('exceptions', $msg);
} }
if($severe && $this->wire('config')->allowExceptions) { if($severe && $config->allowExceptions) {
throw $e; // re-throw, if requested throw $e; // re-throw, if requested
} }
return $this; return $this;
@@ -1485,26 +1489,38 @@ abstract class Wire implements WireTranslatable, WireFuelable, WireTrackable {
*/ */
public function messages($options = array()) { public function messages($options = array()) {
if(!is_array($options)) $options = explode(' ', strtolower($options)); if(!is_array($options)) $options = explode(' ', strtolower($options));
if(in_array('errors', $options)) $type = 'errors'; if(in_array('errors', $options)) {
else if(in_array('warnings', $options)) $type = 'warnings'; $type = 'errors';
else $type = 'messages'; } else if(in_array('warnings', $options)) {
$type = 'warnings';
} else {
$type = 'messages';
}
$clear = in_array('clear', $options); $clear = in_array('clear', $options);
if(in_array('all', $options)) { if(in_array('all', $options)) {
// get all of either messages, warnings or errors (either in or out of this object instance) // get all of either messages, warnings or errors (either in or out of this object instance)
$value = $this->wire(new Notices()); $notices = $this->wire()->notices;
foreach($this->wire('notices') as $notice) { $value = $this->wire(new Notices()); /** @var Notices $value */
foreach($notices as $notice) {
if($notice->getName() != $type) continue; if($notice->getName() != $type) continue;
$value->add($notice); $value->add($notice);
if($clear) $this->wire('notices')->remove($notice); // clear global if($clear) $notices->remove($notice); // clear global
} }
if($clear) $this->_notices[$type] = null; // clear local if($clear) $this->_notices[$type] = null; // clear local
} else { } else {
// get messages, warnings or errors specific to this object instance // get messages, warnings or errors specific to this object instance
$value = is_null($this->_notices[$type]) ? $this->wire(new Notices()) : $this->_notices[$type]; /** @var Notices $value */
if(in_array('first', $options)) $value = $clear ? $value->shift() : $value->first(); $value = $this->_notices[$type] === null ? $this->wire(new Notices()) : $this->_notices[$type];
else if(in_array('last', $options)) $value = $clear ? $value->pop() : $value->last(); if(in_array('first', $options)) {
else if($clear) $this->_notices[$type] = null; $value = $clear ? $value->shift() : $value->first();
if($clear && $value) $this->wire('notices')->removeItems($value); // clear from global notices } else if(in_array('last', $options)) {
$value = $clear ? $value->pop() : $value->last();
} else if($clear) {
$this->_notices[$type] = null;
}
if($clear && $value) {
$this->wire()->notices->removeItems($value); // clear from global notices
}
} }
if(in_array('array', $options) || in_array('string', $options)) { if(in_array('array', $options) || in_array('string', $options)) {
if($value instanceof Notice) { if($value instanceof Notice) {
@@ -1543,7 +1559,7 @@ abstract class Wire implements WireTranslatable, WireFuelable, WireTrackable {
* *
*/ */
public function ___log($str = '', array $options = array()) { public function ___log($str = '', array $options = array()) {
$log = $this->wire('log'); $log = $this->wire()->log;
if($log && strlen($str)) { if($log && strlen($str)) {
if(isset($options['name'])) { if(isset($options['name'])) {
$name = $options['name']; $name = $options['name'];
@@ -1755,7 +1771,7 @@ abstract class Wire implements WireTranslatable, WireFuelable, WireTrackable {
} }
$value = $name; // return the provided instance $value = $name; // return the provided instance
} else { } else {
throw new WireException("Wire::wire(\$o) expected WireFuelable for \$o and was given " . get_class($name)); throw new WireException('Wire::wire($o) expected WireFuelable for $o and was given ' . get_class($name));
} }
} else if($value !== null) { } else if($value !== null) {