From 95050036c7439e7169c44718ff2f088b4546feca Mon Sep 17 00:00:00 2001 From: ikkez Date: Thu, 1 Apr 2021 23:45:33 +0200 Subject: [PATCH] refactor to global F3 namespace, #110 --- f3.php | 2 +- audit.php => f3/audit.php | 5 +++- auth.php => f3/auth.php | 4 ++++ base.php => f3/base.php | 32 +++++++++++++++++--------- basket.php => f3/basket.php | 2 ++ bcrypt.php => f3/bcrypt.php | 6 ++++- {cli => f3/cli}/ws.php | 2 +- {db => f3/db}/cursor.php | 4 ++-- {db => f3/db}/jig.php | 10 ++++---- {db => f3/db}/jig/mapper.php | 34 ++++++++++++++-------------- {db => f3/db}/jig/session.php | 10 ++++---- {db => f3/db}/mongo.php | 4 ++-- {db => f3/db}/mongo/mapper.php | 32 +++++++++++++------------- {db => f3/db}/mongo/session.php | 10 ++++---- {db => f3/db}/sql.php | 16 ++++++------- {db => f3/db}/sql/mapper.php | 28 +++++++++++------------ {db => f3/db}/sql/session.php | 8 +++---- image.php => f3/image.php | 2 ++ log.php => f3/log.php | 2 ++ magic.php => f3/magic.php | 4 +++- markdown.php => f3/markdown.php | 6 ++++- matrix.php => f3/matrix.php | 6 ++++- session.php => f3/session.php | 2 ++ smtp.php => f3/smtp.php | 2 ++ template.php => f3/template.php | 2 ++ test.php => f3/test.php | 2 ++ utf.php => f3/utf.php | 6 ++++- web.php => f3/web.php | 6 ++++- {web => f3/web}/geo.php | 15 +++++++----- {web => f3/web}/google/recaptcha.php | 6 ++--- {web => f3/web}/google/staticmap.php | 6 ++--- {web => f3/web}/oauth2.php | 6 ++--- {web => f3/web}/openid.php | 10 ++++---- {web => f3/web}/pingback.php | 18 +++++++++------ 34 files changed, 185 insertions(+), 125 deletions(-) rename audit.php => f3/audit.php (99%) rename auth.php => f3/auth.php (99%) rename base.php => f3/base.php (99%) rename basket.php => f3/basket.php (99%) rename bcrypt.php => f3/bcrypt.php (98%) rename {cli => f3/cli}/ws.php (99%) rename {db => f3/db}/cursor.php (98%) rename {db => f3/db}/jig.php (95%) rename {db => f3/db}/jig/mapper.php (93%) rename {db => f3/db}/jig/session.php (95%) rename {db => f3/db}/mongo.php (97%) rename {db => f3/db}/mongo/mapper.php (91%) rename {db => f3/db}/mongo/session.php (95%) rename {db => f3/db}/sql.php (98%) rename {db => f3/db}/sql/mapper.php (96%) rename {db => f3/db}/sql/session.php (96%) rename image.php => f3/image.php (99%) rename log.php => f3/log.php (99%) rename magic.php => f3/magic.php (97%) rename markdown.php => f3/markdown.php (99%) rename matrix.php => f3/matrix.php (98%) rename session.php => f3/session.php (99%) rename smtp.php => f3/smtp.php (99%) rename template.php => f3/template.php (99%) rename test.php => f3/test.php (99%) rename utf.php => f3/utf.php (99%) rename web.php => f3/web.php (99%) rename {web => f3/web}/geo.php (94%) rename {web => f3/web}/google/recaptcha.php (94%) rename {web => f3/web}/google/staticmap.php (94%) rename {web => f3/web}/oauth2.php (97%) rename {web => f3/web}/openid.php (97%) rename {web => f3/web}/pingback.php (95%) diff --git a/f3.php b/f3.php index ae95942e..8757170a 100644 --- a/f3.php +++ b/f3.php @@ -35,7 +35,7 @@ class F3 { **/ static function __callstatic($func,array $args) { if (!self::$fw) - self::$fw=Base::instance(); + self::$fw=\F3\Base::instance(); return call_user_func_array([self::$fw,$func],$args); } diff --git a/audit.php b/f3/audit.php similarity index 99% rename from audit.php rename to f3/audit.php index a0d4338e..b1758bea 100644 --- a/audit.php +++ b/f3/audit.php @@ -19,9 +19,12 @@ with Fat-Free Framework. If not, see . */ +namespace F3; //! Data validator -class Audit extends Prefab { +class Audit { + + use Prefab; //@{ User agents const diff --git a/auth.php b/f3/auth.php similarity index 99% rename from auth.php rename to f3/auth.php index a150ce49..bbb1fe6f 100644 --- a/auth.php +++ b/f3/auth.php @@ -20,9 +20,13 @@ */ +namespace F3; + //! Authorization/authentication plug-in class Auth { + use Prefab; + //@{ Error messages const E_LDAP='LDAP connection failure', diff --git a/base.php b/f3/base.php similarity index 99% rename from base.php rename to f3/base.php index 380ef55a..a36c13fd 100644 --- a/base.php +++ b/f3/base.php @@ -20,8 +20,10 @@ */ +namespace F3; + //! Factory class for single-instance objects -abstract class Prefab { +trait Prefab { /** * Return class instance @@ -29,7 +31,7 @@ abstract class Prefab { **/ static function instance() { if (!Registry::exists($class=get_called_class())) { - $ref=new ReflectionClass($class); + $ref=new \ReflectionClass($class); $args=func_get_args(); Registry::set($class, $args?$ref->newinstanceargs($args):new $class); @@ -40,7 +42,9 @@ static function instance() { } //! Base structure -final class Base extends Prefab implements ArrayAccess { +final class Base implements \ArrayAccess { + + use Prefab; //@{ Framework details const @@ -323,7 +327,7 @@ function &ref($key,$add=TRUE,&$var=NULL) { elseif ($obj) { $obj=FALSE; if (!is_object($var)) - $var=new stdClass; + $var=new \stdClass; if ($add || property_exists($var,$part)) $var=&$var->$part; else { @@ -2558,7 +2562,9 @@ function($level,$text,$file,$line) { } //! Cache engine -class Cache extends Prefab { +class Cache { + + use Prefab; protected //! Cache DSN @@ -2828,7 +2834,9 @@ function __construct($dsn=FALSE) { } //! View handler -class View extends Prefab { +class View { + + use Prefab; private //! Temporary hive @@ -2842,11 +2850,11 @@ class View extends Prefab { //! Nesting level $level=0; - /** @var \Base Framework instance */ + /** @var Base Framework instance */ protected $fw; function __construct() { - $this->fw=\Base::instance(); + $this->fw=Base::instance(); } /** @@ -3172,7 +3180,9 @@ function beforerender($func) { } //! ISO language/country codes -class ISO extends Prefab { +class ISO { + + use Prefab; //@{ ISO 3166-1 country codes const @@ -3519,7 +3529,7 @@ class ISO extends Prefab { * @return array **/ function languages() { - return \Base::instance()->constants($this,'LC_'); + return Base::instance()->constants($this,'LC_'); } /** @@ -3527,7 +3537,7 @@ function languages() { * @return array **/ function countries() { - return \Base::instance()->constants($this,'CC_'); + return Base::instance()->constants($this,'CC_'); } } diff --git a/basket.php b/f3/basket.php similarity index 99% rename from basket.php rename to f3/basket.php index 70caceed..b9b8c31f 100644 --- a/basket.php +++ b/f3/basket.php @@ -20,6 +20,8 @@ */ +namespace F3; + //! Session-based pseudo-mapper class Basket extends Magic { diff --git a/bcrypt.php b/f3/bcrypt.php similarity index 98% rename from bcrypt.php rename to f3/bcrypt.php index 414daa73..ae2c3ed5 100644 --- a/bcrypt.php +++ b/f3/bcrypt.php @@ -19,11 +19,15 @@ * **/ +namespace F3; + /** * Lightweight password hashing library (PHP 5.5+ only) * @deprecated Use http://php.net/manual/en/ref.password.php instead **/ -class Bcrypt extends Prefab { +class Bcrypt { + + use Prefab; //@{ Error messages const diff --git a/cli/ws.php b/f3/cli/ws.php similarity index 99% rename from cli/ws.php rename to f3/cli/ws.php index 4545e9bc..c7d53a37 100644 --- a/cli/ws.php +++ b/f3/cli/ws.php @@ -20,7 +20,7 @@ */ -namespace CLI; +namespace F3\CLI; //! RFC6455 WebSocket server class WS { diff --git a/db/cursor.php b/f3/db/cursor.php similarity index 98% rename from db/cursor.php rename to f3/db/cursor.php index 2fd324a0..915c0d58 100644 --- a/db/cursor.php +++ b/f3/db/cursor.php @@ -20,10 +20,10 @@ */ -namespace DB; +namespace F3\DB; //! Simple cursor implementation -abstract class Cursor extends \Magic implements \IteratorAggregate { +abstract class Cursor extends \F3\Magic implements \IteratorAggregate { //@{ Error messages const diff --git a/db/jig.php b/f3/db/jig.php similarity index 95% rename from db/jig.php rename to f3/db/jig.php index bcc29cac..4b757dc0 100644 --- a/db/jig.php +++ b/f3/db/jig.php @@ -20,7 +20,7 @@ */ -namespace DB; +namespace F3\DB; //! In-memory/flat-file DB wrapper class Jig { @@ -58,7 +58,7 @@ function &read($file) { } if ($this->lazy && isset($this->data[$file])) return $this->data[$file]; - $fw=\Base::instance(); + $fw=\F3\Base::instance(); $raw=$fw->read($dst); switch ($this->format) { case self::FORMAT_JSON: @@ -81,7 +81,7 @@ function &read($file) { function write($file,array $data=NULL) { if (!$this->dir || $this->lazy) return count($this->data[$file]=$data); - $fw=\Base::instance(); + $fw=\F3\Base::instance(); switch ($this->format) { case self::FORMAT_JSON: $out=json_encode($data,JSON_PRETTY_PRINT); @@ -155,8 +155,8 @@ private function __clone() { **/ function __construct($dir=NULL,$format=self::FORMAT_JSON,$lazy=FALSE) { if ($dir && !is_dir($dir)) - mkdir($dir,\Base::MODE,TRUE); - $this->uuid=\Base::instance()->hash($this->dir=$dir); + mkdir($dir,\F3\Base::MODE,TRUE); + $this->uuid=\F3\Base::instance()->hash($this->dir=$dir); $this->format=$format; $this->lazy=$lazy; } diff --git a/db/jig/mapper.php b/f3/db/jig/mapper.php similarity index 93% rename from db/jig/mapper.php rename to f3/db/jig/mapper.php index 5d26427f..a0a0a3ad 100644 --- a/db/jig/mapper.php +++ b/f3/db/jig/mapper.php @@ -20,7 +20,7 @@ */ -namespace DB\Jig; +namespace F3\DB\Jig; //! Flat-file DB mapper class Mapper extends \DB\Cursor { @@ -101,7 +101,7 @@ function factory($id,$row) { $mapper->document[$field]=$val; $mapper->query=[clone($mapper)]; if (isset($mapper->trigger['load'])) - \Base::instance()->call($mapper->trigger['load'],$mapper); + \F3\Base::instance()->call($mapper->trigger['load'],$mapper); return $mapper; } @@ -129,7 +129,7 @@ function($token) { return '$'.preg_replace_callback( '/(\.\w+)|\[((?:[^\[\]]*|(?R))*)\]/', function($expr) { - $fw=\Base::instance(); + $fw=\F3\Base::instance(); return '['. ($expr[1]? @@ -165,8 +165,8 @@ function find($filter=NULL,array $options=NULL,$ttl=0,$log=TRUE) { 'offset'=>0, 'group'=>NULL, ]; - $fw=\Base::instance(); - $cache=\Cache::instance(); + $fw=\F3\Base::instance(); + $cache=\F3\Cache::instance(); $db=$this->db; $now=microtime(TRUE); $data=[]; @@ -310,7 +310,7 @@ function($_row) use($fw,$args,$tokens) { * @return mixed */ protected function sort($data,$cond) { - $cols=\Base::instance()->split($cond); + $cols=\F3\Base::instance()->split($cond); uasort( $data, function($val1,$val2) use($cols) { @@ -370,7 +370,7 @@ function skip($ofs=1) { $this->document=($out=parent::skip($ofs))?$out->document:[]; $this->id=$out?$out->id:NULL; if ($this->document && isset($this->trigger['load'])) - \Base::instance()->call($this->trigger['load'],$this); + \F3\Base::instance()->call($this->trigger['load'],$this); return $out; } @@ -390,7 +390,7 @@ function insert() { $this->id=$id; $pkey=['_id'=>$this->id]; if (isset($this->trigger['beforeinsert']) && - \Base::instance()->call($this->trigger['beforeinsert'], + \F3\Base::instance()->call($this->trigger['beforeinsert'], [$this,$pkey])===FALSE) return $this->document; $data[$id]=$this->document; @@ -398,7 +398,7 @@ function insert() { $db->jot('('.sprintf('%.1f',1e3*(microtime(TRUE)-$now)).'ms) '. $this->file.' [insert] '.json_encode($this->document)); if (isset($this->trigger['afterinsert'])) - \Base::instance()->call($this->trigger['afterinsert'], + \F3\Base::instance()->call($this->trigger['afterinsert'], [$this,$pkey]); $this->load(['@_id=?',$this->id]); return $this->document; @@ -413,7 +413,7 @@ function update() { $now=microtime(TRUE); $data=&$db->read($this->file); if (isset($this->trigger['beforeupdate']) && - \Base::instance()->call($this->trigger['beforeupdate'], + \F3\Base::instance()->call($this->trigger['beforeupdate'], [$this,['_id'=>$this->id]])===FALSE) return $this->document; $data[$this->id]=$this->document; @@ -421,7 +421,7 @@ function update() { $db->jot('('.sprintf('%.1f',1e3*(microtime(TRUE)-$now)).'ms) '. $this->file.' [update] '.json_encode($this->document)); if (isset($this->trigger['afterupdate'])) - \Base::instance()->call($this->trigger['afterupdate'], + \F3\Base::instance()->call($this->trigger['afterupdate'], [$this,['_id'=>$this->id]]); return $this->document; } @@ -450,7 +450,7 @@ function erase($filter=NULL,$quick=FALSE) { else return FALSE; if (!$quick && isset($this->trigger['beforeerase']) && - \Base::instance()->call($this->trigger['beforeerase'], + \F3\Base::instance()->call($this->trigger['beforeerase'], [$this,$pkey])===FALSE) return FALSE; $db->write($this->file,$data); @@ -460,7 +460,7 @@ function erase($filter=NULL,$quick=FALSE) { array_slice($filter,1,NULL,TRUE); $args=is_array($args)?$args:[1=>$args]; foreach ($args as $key=>$val) { - $vals[]=\Base::instance()-> + $vals[]=\F3\Base::instance()-> stringify(is_array($val)?$val[0]:$val); $keys[]='/'.(is_numeric($key)?'\?':preg_quote($key)).'/'; } @@ -469,7 +469,7 @@ function erase($filter=NULL,$quick=FALSE) { $this->file.' [erase] '. ($filter?preg_replace($keys,$vals,$filter[0],1):'')); if (!$quick && isset($this->trigger['aftererase'])) - \Base::instance()->call($this->trigger['aftererase'], + \F3\Base::instance()->call($this->trigger['aftererase'], [$this,$pkey]); return TRUE; } @@ -492,7 +492,7 @@ function reset() { **/ function copyfrom($var,$func=NULL) { if (is_string($var)) - $var=\Base::instance()->$var; + $var=\F3\Base::instance()->$var; if ($func) $var=call_user_func($func,$var); foreach ($var as $key=>$val) @@ -505,7 +505,7 @@ function copyfrom($var,$func=NULL) { * @param $key string **/ function copyto($key) { - $var=&\Base::instance()->ref($key); + $var=&\F3\Base::instance()->ref($key); foreach ($this->document as $key=>$field) $var[$key]=$field; } @@ -532,7 +532,7 @@ function getiterator() { * @param $db object * @param $file string **/ - function __construct(\DB\Jig $db,$file) { + function __construct(\F3\DB\Jig $db,$file) { $this->db=$db; $this->file=$file; $this->reset(); diff --git a/db/jig/session.php b/f3/db/jig/session.php similarity index 95% rename from db/jig/session.php rename to f3/db/jig/session.php index eee13395..023d3b20 100644 --- a/db/jig/session.php +++ b/f3/db/jig/session.php @@ -20,7 +20,7 @@ */ -namespace DB\Jig; +namespace F3\DB\Jig; //! Jig-managed session handler class Session extends Mapper { @@ -67,7 +67,7 @@ function read($id) { if ($this->dry()) return ''; if ($this->get('ip')!=$this->_ip || $this->get('agent')!=$this->_agent) { - $fw=\Base::instance(); + $fw=\F3\Base::instance(); if (!isset($this->onsuspect) || $fw->call($this->onsuspect,[$this,$id])===FALSE) { // NB: `session_destroy` can't be called at that stage; @@ -161,12 +161,12 @@ function agent() { /** * Instantiate class - * @param $db \DB\Jig + * @param $db \F3\DB\Jig * @param $file string * @param $onsuspect callback * @param $key string **/ - function __construct(\DB\Jig $db,$file='sessions',$onsuspect=NULL,$key=NULL) { + function __construct(\F3\DB\Jig $db,$file='sessions',$onsuspect=NULL,$key=NULL) { parent::__construct($db,$file); $this->onsuspect=$onsuspect; session_set_save_handler( @@ -178,7 +178,7 @@ function __construct(\DB\Jig $db,$file='sessions',$onsuspect=NULL,$key=NULL) { [$this,'cleanup'] ); register_shutdown_function('session_commit'); - $fw=\Base::instance(); + $fw=\F3\Base::instance(); $headers=$fw->HEADERS; $this->_csrf=$fw->hash($fw->SEED. extension_loaded('openssl')? diff --git a/db/mongo.php b/f3/db/mongo.php similarity index 97% rename from db/mongo.php rename to f3/db/mongo.php index 46d00be3..18ec713e 100644 --- a/db/mongo.php +++ b/f3/db/mongo.php @@ -20,7 +20,7 @@ */ -namespace DB; +namespace F3\DB; //! MongoDB wrapper class Mongo { @@ -131,7 +131,7 @@ private function __clone() { * @param $options array **/ function __construct($dsn,$dbname,array $options=NULL) { - $this->uuid=\Base::instance()->hash($this->dsn=$dsn); + $this->uuid=\F3\Base::instance()->hash($this->dsn=$dsn); if ($this->legacy=class_exists('\MongoClient')) { $this->db=new \MongoDB(new \MongoClient($dsn,$options?:[]),$dbname); $this->db->setprofilinglevel(2); diff --git a/db/mongo/mapper.php b/f3/db/mongo/mapper.php similarity index 91% rename from db/mongo/mapper.php rename to f3/db/mongo/mapper.php index 0246f6d7..67aa3ff0 100644 --- a/db/mongo/mapper.php +++ b/f3/db/mongo/mapper.php @@ -20,7 +20,7 @@ */ -namespace DB\Mongo; +namespace F3\DB\Mongo; //! MongoDB mapper class Mapper extends \DB\Cursor { @@ -98,7 +98,7 @@ function factory($row) { $mapper->document[$key]=$val; $mapper->query=[clone($mapper)]; if (isset($mapper->trigger['load'])) - \Base::instance()->call($mapper->trigger['load'],$mapper); + \F3\Base::instance()->call($mapper->trigger['load'],$mapper); return $mapper; } @@ -133,8 +133,8 @@ function select($fields=NULL,$filter=NULL,array $options=NULL,$ttl=0) { $tag=''; if (is_array($ttl)) list($ttl,$tag)=$ttl; - $fw=\Base::instance(); - $cache=\Cache::instance(); + $fw=\F3\Base::instance(); + $cache=\F3\Cache::instance(); if (!($cached=$cache->exists($hash=$fw->hash($this->db->dsn(). $fw->stringify([$fields,$filter,$options])).($tag?'.'.$tag:'').'.mongo', $result)) || !$ttl || $cached[0]+$ttldocument=($out=parent::skip($ofs))?$out->document:[]; if ($this->document && isset($this->trigger['load'])) - \Base::instance()->call($this->trigger['load'],$this); + \F3\Base::instance()->call($this->trigger['load'],$this); return $out; } @@ -256,7 +256,7 @@ function insert() { if (isset($this->document['_id'])) return $this->update(); if (isset($this->trigger['beforeinsert']) && - \Base::instance()->call($this->trigger['beforeinsert'], + \F3\Base::instance()->call($this->trigger['beforeinsert'], [$this,['_id'=>$this->document['_id']]])===FALSE) return $this->document; if ($this->legacy) { @@ -268,7 +268,7 @@ function insert() { $pkey=['_id'=>$result->getinsertedid()]; } if (isset($this->trigger['afterinsert'])) - \Base::instance()->call($this->trigger['afterinsert'], + \F3\Base::instance()->call($this->trigger['afterinsert'], [$this,$pkey]); $this->load($pkey); return $this->document; @@ -281,7 +281,7 @@ function insert() { function update() { $pkey=['_id'=>$this->document['_id']]; if (isset($this->trigger['beforeupdate']) && - \Base::instance()->call($this->trigger['beforeupdate'], + \F3\Base::instance()->call($this->trigger['beforeupdate'], [$this,$pkey])===FALSE) return $this->document; $upsert=['upsert'=>TRUE]; @@ -290,7 +290,7 @@ function update() { else $this->collection->replaceone($pkey,$this->document,$upsert); if (isset($this->trigger['afterupdate'])) - \Base::instance()->call($this->trigger['afterupdate'], + \F3\Base::instance()->call($this->trigger['afterupdate'], [$this,$pkey]); return $this->document; } @@ -315,7 +315,7 @@ function erase($filter=NULL,$quick=TRUE) { } $pkey=['_id'=>$this->document['_id']]; if (isset($this->trigger['beforeerase']) && - \Base::instance()->call($this->trigger['beforeerase'], + \F3\Base::instance()->call($this->trigger['beforeerase'], [$this,$pkey])===FALSE) return FALSE; $result=$this->legacy? @@ -323,7 +323,7 @@ function erase($filter=NULL,$quick=TRUE) { $this->collection->deleteone(['_id'=>$this->document['_id']]); parent::erase(); if (isset($this->trigger['aftererase'])) - \Base::instance()->call($this->trigger['aftererase'], + \F3\Base::instance()->call($this->trigger['aftererase'], [$this,$pkey]); return $result; } @@ -345,7 +345,7 @@ function reset() { **/ function copyfrom($var,$func=NULL) { if (is_string($var)) - $var=\Base::instance()->$var; + $var=\F3\Base::instance()->$var; if ($func) $var=call_user_func($func,$var); foreach ($var as $key=>$val) @@ -358,7 +358,7 @@ function copyfrom($var,$func=NULL) { * @param $key string **/ function copyto($key) { - $var=&\Base::instance()->ref($key); + $var=&\F3\Base::instance()->ref($key); foreach ($this->document as $key=>$field) $var[$key]=$field; } @@ -394,7 +394,7 @@ function getiterator() { * @param $collection string * @param $fields array **/ - function __construct(\DB\Mongo $db,$collection,$fields=NULL) { + function __construct(\F3\DB\Mongo $db,$collection,$fields=NULL) { $this->db=$db; $this->legacy=$db->legacy(); $this->collection=$db->selectcollection($collection); diff --git a/db/mongo/session.php b/f3/db/mongo/session.php similarity index 95% rename from db/mongo/session.php rename to f3/db/mongo/session.php index 013f1717..86a1f257 100644 --- a/db/mongo/session.php +++ b/f3/db/mongo/session.php @@ -20,7 +20,7 @@ */ -namespace DB\Mongo; +namespace F3\DB\Mongo; //! MongoDB-managed session handler class Session extends Mapper { @@ -67,7 +67,7 @@ function read($id) { if ($this->dry()) return ''; if ($this->get('ip')!=$this->_ip || $this->get('agent')!=$this->_agent) { - $fw=\Base::instance(); + $fw=\F3\Base::instance(); if (!isset($this->onsuspect) || $fw->call($this->onsuspect,[$this,$id])===FALSE) { // NB: `session_destroy` can't be called at that stage; @@ -161,12 +161,12 @@ function agent() { /** * Instantiate class - * @param $db \DB\Mongo + * @param $db \F3\DB\Mongo * @param $table string * @param $onsuspect callback * @param $key string **/ - function __construct(\DB\Mongo $db,$table='sessions',$onsuspect=NULL,$key=NULL) { + function __construct(\F3\DB\Mongo $db,$table='sessions',$onsuspect=NULL,$key=NULL) { parent::__construct($db,$table); $this->onsuspect=$onsuspect; session_set_save_handler( @@ -178,7 +178,7 @@ function __construct(\DB\Mongo $db,$table='sessions',$onsuspect=NULL,$key=NULL) [$this,'cleanup'] ); register_shutdown_function('session_commit'); - $fw=\Base::instance(); + $fw=\F3\Base::instance(); $headers=$fw->HEADERS; $this->_csrf=$fw->hash($fw->SEED. extension_loaded('openssl')? diff --git a/db/sql.php b/f3/db/sql.php similarity index 98% rename from db/sql.php rename to f3/db/sql.php index 566b94a7..2048b590 100644 --- a/db/sql.php +++ b/f3/db/sql.php @@ -20,7 +20,7 @@ */ -namespace DB; +namespace F3\DB; //! PDO wrapper class SQL { @@ -174,8 +174,8 @@ function exec($cmds,$args=NULL,$ttl=0,$log=TRUE,$stamp=FALSE) { } if ($this->log===FALSE) $log=FALSE; - $fw=\Base::instance(); - $cache=\Cache::instance(); + $fw=\F3\Base::instance(); + $cache=\F3\Cache::instance(); $result=FALSE; for ($i=0;$i<$count;++$i) { $cmd=$cmds[$i]; @@ -315,8 +315,8 @@ function exists($table) { * @param $ttl int|array **/ function schema($table,$fields=NULL,$ttl=0) { - $fw=\Base::instance(); - $cache=\Cache::instance(); + $fw=\F3\Base::instance(); + $cache=\F3\Cache::instance(); if ($fw->CACHE && $ttl && ($cached=$cache->exists( $hash=$fw->hash($this->dsn.$table).'.schema',$result)) && @@ -396,7 +396,7 @@ function schema($table,$fields=NULL,$ttl=0) { 'FIELD','TYPE','DEFVAL','NULLABLE','Y','PKEY','P'] ]; if (is_string($fields)) - $fields=\Base::instance()->split($fields); + $fields=\F3\Base::instance()->split($fields); $conv=[ 'int\b|integer'=>\PDO::PARAM_INT, 'bool'=>\PDO::PARAM_BOOL, @@ -447,7 +447,7 @@ function schema($table,$fields=NULL,$ttl=0) { function quote($val,$type=\PDO::PARAM_STR) { return $this->engine=='odbc'? (is_string($val)? - \Base::instance()->stringify(str_replace('\'','\'\'',$val)): + \F3\Base::instance()->stringify(str_replace('\'','\'\'',$val)): $val): $this->pdo->quote($val,$type); } @@ -536,7 +536,7 @@ private function __clone() { * @param $options array **/ function __construct($dsn,$user=NULL,$pw=NULL,array $options=NULL) { - $fw=\Base::instance(); + $fw=\F3\Base::instance(); $this->uuid=$fw->hash($this->dsn=$dsn); if (preg_match('/^.+?(?:dbname|database)=(.+?)(?=;|$)/is',$dsn,$parts)) $this->dbname=str_replace('\\ ',' ',$parts[1]); diff --git a/db/sql/mapper.php b/f3/db/sql/mapper.php similarity index 96% rename from db/sql/mapper.php rename to f3/db/sql/mapper.php index 574cc9fb..de737e09 100644 --- a/db/sql/mapper.php +++ b/f3/db/sql/mapper.php @@ -20,10 +20,10 @@ */ -namespace DB\SQL; +namespace F3\DB\SQL; //! SQL data mapper -class Mapper extends \DB\Cursor { +class Mapper extends \F3\DB\Cursor { //@{ Error messages const @@ -180,7 +180,7 @@ function factory($row) { } $mapper->query=[clone($mapper)]; if (isset($mapper->trigger['load'])) - \Base::instance()->call($mapper->trigger['load'],$mapper); + \F3\Base::instance()->call($mapper->trigger['load'],$mapper); return $mapper; } @@ -411,7 +411,7 @@ function skip($ofs=1) { unset($field); } if (!$dry && isset($this->trigger['load'])) - \Base::instance()->call($this->trigger['load'],$this); + \F3\Base::instance()->call($this->trigger['load'],$this); return $out; } @@ -434,7 +434,7 @@ function insert() { if ($field['pkey']) $pkeys[$key]=$field['previous']; if (isset($this->trigger['beforeinsert']) && - \Base::instance()->call($this->trigger['beforeinsert'], + \F3\Base::instance()->call($this->trigger['beforeinsert'], [$this,$pkeys])===FALSE) return $this; if ($this->valid()) @@ -494,7 +494,7 @@ function insert() { $this->fields[$inc]['pdo_type'],$this->_id)]: [$filter,$nkeys]); if (isset($this->trigger['afterinsert'])) - \Base::instance()->call($this->trigger['afterinsert'], + \F3\Base::instance()->call($this->trigger['afterinsert'], [$this,$pkeys]); // reset changed flag after calling afterinsert if (!$reload) @@ -520,7 +520,7 @@ function update() { if ($field['pkey']) $pkeys[$key]=$field['previous']; if (isset($this->trigger['beforeupdate']) && - \Base::instance()->call($this->trigger['beforeupdate'], + \F3\Base::instance()->call($this->trigger['beforeupdate'], [$this,$pkeys])===FALSE) return $this; foreach ($this->fields as $key=>$field) @@ -542,7 +542,7 @@ function update() { $this->db->exec($sql,$args); } if (isset($this->trigger['afterupdate'])) - \Base::instance()->call($this->trigger['afterupdate'], + \F3\Base::instance()->call($this->trigger['afterupdate'], [$this,$pkeys]); // reset changed flag after calling afterupdate foreach ($this->fields as $key=>&$field) { @@ -639,13 +639,13 @@ function erase($filter=NULL,$quick=TRUE) { } parent::erase(); if (isset($this->trigger['beforeerase']) && - \Base::instance()->call($this->trigger['beforeerase'], + \F3\Base::instance()->call($this->trigger['beforeerase'], [$this,$pkeys])===FALSE) return 0; $out=$this->db-> exec('DELETE FROM '.$this->table.' WHERE '.$filter.';',$args); if (isset($this->trigger['aftererase'])) - \Base::instance()->call($this->trigger['aftererase'], + \F3\Base::instance()->call($this->trigger['aftererase'], [$this,$pkeys]); return $out; } @@ -678,7 +678,7 @@ function reset() { **/ function copyfrom($var,$func=NULL) { if (is_string($var)) - $var=\Base::instance()->$var; + $var=\F3\Base::instance()->$var; if ($func) $var=call_user_func($func,$var); foreach ($var as $key=>$val) @@ -692,7 +692,7 @@ function copyfrom($var,$func=NULL) { * @param $key string **/ function copyto($key) { - $var=&\Base::instance()->ref($key); + $var=&\F3\Base::instance()->ref($key); foreach ($this->fields+$this->adhoc as $key=>$field) $var[$key]=$field['value']; } @@ -746,12 +746,12 @@ function alias($alias) { /** * Instantiate class - * @param $db \DB\SQL + * @param $db \F3\DB\SQL * @param $table string * @param $fields array|string * @param $ttl int|array **/ - function __construct(\DB\SQL $db,$table,$fields=NULL,$ttl=60) { + function __construct(\F3\DB\SQL $db,$table,$fields=NULL,$ttl=60) { $this->db=$db; $this->engine=$db->driver(); if ($this->engine=='oci') diff --git a/db/sql/session.php b/f3/db/sql/session.php similarity index 96% rename from db/sql/session.php rename to f3/db/sql/session.php index 8defbf40..7601d754 100644 --- a/db/sql/session.php +++ b/f3/db/sql/session.php @@ -20,7 +20,7 @@ */ -namespace DB\SQL; +namespace F3\DB\SQL; //! SQL-managed session handler class Session extends Mapper { @@ -67,7 +67,7 @@ function read($id) { if ($this->dry()) return ''; if ($this->get('ip')!=$this->_ip || $this->get('agent')!=$this->_agent) { - $fw=\Base::instance(); + $fw=\F3\Base::instance(); if (!isset($this->onsuspect) || $fw->call($this->onsuspect,[$this,$id])===FALSE) { //NB: `session_destroy` can't be called at that stage (`session_start` not completed) @@ -167,7 +167,7 @@ function agent() { * @param $key string * @param $type string, column type for data field **/ - function __construct(\DB\SQL $db,$table='sessions',$force=TRUE,$onsuspect=NULL,$key=NULL,$type='TEXT') { + function __construct(\F3\DB\SQL $db,$table='sessions',$force=TRUE,$onsuspect=NULL,$key=NULL,$type='TEXT') { if ($force) { $eol="\n"; $tab="\t"; @@ -203,7 +203,7 @@ function __construct(\DB\SQL $db,$table='sessions',$force=TRUE,$onsuspect=NULL,$ [$this,'cleanup'] ); register_shutdown_function('session_commit'); - $fw=\Base::instance(); + $fw=\F3\Base::instance(); $headers=$fw->HEADERS; $this->_csrf=$fw->hash($fw->SEED. extension_loaded('openssl')? diff --git a/image.php b/f3/image.php similarity index 99% rename from image.php rename to f3/image.php index b7f149ce..c9d61754 100644 --- a/image.php +++ b/f3/image.php @@ -20,6 +20,8 @@ */ +namespace F3; + //! Image manipulation tools class Image { diff --git a/log.php b/f3/log.php similarity index 99% rename from log.php rename to f3/log.php index 5b7341d1..68d46176 100644 --- a/log.php +++ b/f3/log.php @@ -20,6 +20,8 @@ */ +namespace F3; + //! Custom logger class Log { diff --git a/magic.php b/f3/magic.php similarity index 97% rename from magic.php rename to f3/magic.php index f676506f..f87f3ced 100644 --- a/magic.php +++ b/f3/magic.php @@ -20,8 +20,10 @@ */ +namespace F3; + //! PHP magic wrapper -abstract class Magic implements ArrayAccess { +abstract class Magic implements \ArrayAccess { /** * Return TRUE if key is not empty diff --git a/markdown.php b/f3/markdown.php similarity index 99% rename from markdown.php rename to f3/markdown.php index 4be4c561..37b88867 100644 --- a/markdown.php +++ b/f3/markdown.php @@ -20,8 +20,12 @@ */ +namespace F3; + //! Markdown-to-HTML converter -class Markdown extends Prefab { +class Markdown { + + use Prefab; protected //! Parsing rules diff --git a/matrix.php b/f3/matrix.php similarity index 98% rename from matrix.php rename to f3/matrix.php index 6c22bae2..7ca62734 100644 --- a/matrix.php +++ b/f3/matrix.php @@ -20,8 +20,12 @@ */ +namespace F3; + //! Generic array utilities -class Matrix extends Prefab { +class Matrix { + + use Prefab; /** * Retrieve values from a specified column of a multi-dimensional diff --git a/session.php b/f3/session.php similarity index 99% rename from session.php rename to f3/session.php index 168e5b6d..2af83285 100644 --- a/session.php +++ b/f3/session.php @@ -20,6 +20,8 @@ */ +namespace F3; + //! Cache-based session handler class Session { diff --git a/smtp.php b/f3/smtp.php similarity index 99% rename from smtp.php rename to f3/smtp.php index 8cef9393..5f91e93b 100644 --- a/smtp.php +++ b/f3/smtp.php @@ -20,6 +20,8 @@ */ +namespace F3; + //! SMTP plug-in class SMTP extends Magic { diff --git a/template.php b/f3/template.php similarity index 99% rename from template.php rename to f3/template.php index fb6f21dd..27b59047 100644 --- a/template.php +++ b/f3/template.php @@ -20,6 +20,8 @@ */ +namespace F3; + //! XML-style template engine class Template extends Preview { diff --git a/test.php b/f3/test.php similarity index 99% rename from test.php rename to f3/test.php index d45bb18c..79bd6ce4 100644 --- a/test.php +++ b/f3/test.php @@ -20,6 +20,8 @@ */ +namespace F3; + //! Unit test kit class Test { diff --git a/utf.php b/f3/utf.php similarity index 99% rename from utf.php rename to f3/utf.php index 34b82308..df4eb7ee 100644 --- a/utf.php +++ b/f3/utf.php @@ -20,8 +20,12 @@ */ +namespace F3; + //! Unicode string manager -class UTF extends Prefab { +class UTF { + + use Prefab; /** * Get string length diff --git a/web.php b/f3/web.php similarity index 99% rename from web.php rename to f3/web.php index 7a69051d..c1463627 100644 --- a/web.php +++ b/f3/web.php @@ -20,8 +20,12 @@ */ +namespace F3; + //! Wrapper for various HTTP utilities -class Web extends Prefab { +class Web { + + use Prefab; //@{ Error messages const diff --git a/web/geo.php b/f3/web/geo.php similarity index 94% rename from web/geo.php rename to f3/web/geo.php index 98ba2041..75e1ee49 100644 --- a/web/geo.php +++ b/f3/web/geo.php @@ -20,10 +20,13 @@ */ -namespace Web; +namespace F3\Web; //! Geo plug-in -class Geo extends \Prefab { + +class Geo { + + use \F3\Prefab; /** * Return information about specified Unix time zone @@ -52,8 +55,8 @@ function tzinfo($zone) { * @param $ip string **/ function location($ip=NULL) { - $fw=\Base::instance(); - $web=\Web::instance(); + $fw=\F3\Base::instance(); + $web=\F3\Web::instance(); if (!$ip) $ip=$fw->IP; $public=filter_var($ip,FILTER_VALIDATE_IP, @@ -93,8 +96,8 @@ function location($ip=NULL) { * @param $key string **/ function weather($latitude,$longitude,$key) { - $fw=\Base::instance(); - $web=\Web::instance(); + $fw=\F3\Base::instance(); + $web=\F3\Web::instance(); $query=[ 'lat'=>$latitude, 'lon'=>$longitude, diff --git a/web/google/recaptcha.php b/f3/web/google/recaptcha.php similarity index 94% rename from web/google/recaptcha.php rename to f3/web/google/recaptcha.php index 38fd2d07..cf0527a2 100644 --- a/web/google/recaptcha.php +++ b/f3/web/google/recaptcha.php @@ -20,7 +20,7 @@ */ -namespace Web\Google; +namespace F3\Web\Google; //! Google ReCAPTCHA v2 plug-in class Recaptcha { @@ -36,12 +36,12 @@ class Recaptcha { * @return bool **/ static function verify($secret,$response=NULL) { - $fw=\Base::instance(); + $fw=\F3\Base::instance(); if (!isset($response)) $response=$fw->{'POST.g-recaptcha-response'}; if (!$response) return FALSE; - $web=\Web::instance(); + $web=\F3\Web::instance(); $out=$web->request(self::URL_Recaptcha,[ 'method'=>'POST', 'content'=>http_build_query([ diff --git a/web/google/staticmap.php b/f3/web/google/staticmap.php similarity index 94% rename from web/google/staticmap.php rename to f3/web/google/staticmap.php index 023103d5..0c87d2dc 100644 --- a/web/google/staticmap.php +++ b/f3/web/google/staticmap.php @@ -20,7 +20,7 @@ */ -namespace Web\Google; +namespace F3\Web\Google; //! Google Static Maps API v2 plug-in class StaticMap { @@ -49,8 +49,8 @@ function __call($func,array $args) { * @return string **/ function dump() { - $fw=\Base::instance(); - $web=\Web::instance(); + $fw=\F3\Base::instance(); + $web=\F3\Web::instance(); $out=''; return ($req=$web->request( self::URL_Static.'?'.array_reduce( diff --git a/web/oauth2.php b/f3/web/oauth2.php similarity index 97% rename from web/oauth2.php rename to f3/web/oauth2.php index eda6e45f..3ab1bf73 100644 --- a/web/oauth2.php +++ b/f3/web/oauth2.php @@ -20,10 +20,10 @@ */ -namespace Web; +namespace F3\Web; //! Lightweight OAuth2 client -class OAuth2 extends \Magic { +class OAuth2 extends \F3\Magic { protected //! Scopes and claims @@ -50,7 +50,7 @@ function uri($endpoint,$query=TRUE) { * @param $token string **/ function request($uri,$method,$token=NULL) { - $web=\Web::instance(); + $web=\F3\Web::instance(); $options=[ 'method'=>$method, 'content'=>http_build_query($this->args,null,'&',$this->enc_type), diff --git a/web/openid.php b/f3/web/openid.php similarity index 97% rename from web/openid.php rename to f3/web/openid.php index 6e84b612..ce776d2c 100644 --- a/web/openid.php +++ b/f3/web/openid.php @@ -20,10 +20,10 @@ */ -namespace Web; +namespace F3\Web; //! OpenID consumer -class OpenID extends \Magic { +class OpenID extends \F3\Magic { protected //! OpenID provider endpoint URL @@ -49,7 +49,7 @@ protected function discover($proxy) { strtolower($url['host']).(isset($url['path'])?$url['path']:'/'). (isset($url['query'])?('?'.$url['query']):''); // HTML-based discovery of OpenID provider - $req=\Web::instance()-> + $req=\F3\Web::instance()-> request($this->args['endpoint'],['proxy'=>$proxy]); if (!$req) return FALSE; @@ -142,7 +142,7 @@ protected function discover($proxy) { * @param $reqd string|array **/ function auth($proxy=NULL,$attr=[],array $reqd=NULL) { - $fw=\Base::instance(); + $fw=\F3\Base::instance(); $root=$fw->SCHEME.'://'.$fw->HOST; if (empty($this->args['trust_root'])) $this->args['trust_root']=$root.$fw->BASE.'/'; @@ -183,7 +183,7 @@ function verified($proxy=NULL) { $var=[]; foreach ($this->args as $key=>$val) $var['openid.'.$key]=$val; - $req=\Web::instance()->request( + $req=\F3\Web::instance()->request( $this->url, [ 'method'=>'POST', diff --git a/web/pingback.php b/f3/web/pingback.php similarity index 95% rename from web/pingback.php rename to f3/web/pingback.php index 28c51a5a..e784ce54 100644 --- a/web/pingback.php +++ b/f3/web/pingback.php @@ -20,10 +20,14 @@ */ -namespace Web; +namespace F3\Web; //! Pingback 1.0 protocol (client and server) implementation -class Pingback extends \Prefab { +use F3\Prefab; + +class Pingback { + + use Prefab; protected //! Transaction history @@ -35,7 +39,7 @@ class Pingback extends \Prefab { * @param $url **/ protected function enabled($url) { - $web=\Web::instance(); + $web=\F3\Web::instance(); $req=$web->request($url); $found=FALSE; if ($req['body']) { @@ -62,8 +66,8 @@ protected function enabled($url) { * @param $source string **/ function inspect($source) { - $fw=\Base::instance(); - $web=\Web::instance(); + $fw=\F3\Base::instance(); + $web=\F3\Web::instance(); $parts=parse_url($source); if (empty($parts['scheme']) || empty($parts['host']) || $parts['host']==$fw->HOST) { @@ -108,7 +112,7 @@ function inspect($source) { * @param $path string **/ function listen($func,$path=NULL) { - $fw=\Base::instance(); + $fw=\F3\Base::instance(); if (PHP_SAPI!='cli') { header('X-Powered-By: '.$fw->PACKAGE); header('Content-Type: application/xml; '. @@ -116,7 +120,7 @@ function listen($func,$path=NULL) { } if (!$path) $path=$fw->BASE; - $web=\Web::instance(); + $web=\F3\Web::instance(); $args=xmlrpc_decode_request($fw->BODY,$method,$charset); $options=['encoding'=>$charset]; if ($method=='pingback.ping' && isset($args[0],$args[1])) {