Skip to content

Commit

Permalink
refactor to global F3 namespace, f3-factory#110
Browse files Browse the repository at this point in the history
  • Loading branch information
ikkez committed Apr 1, 2021
1 parent c730ca5 commit 9505003
Show file tree
Hide file tree
Showing 34 changed files with 185 additions and 125 deletions.
2 changes: 1 addition & 1 deletion f3.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
5 changes: 4 additions & 1 deletion audit.php → f3/audit.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,12 @@
with Fat-Free Framework. If not, see <http://www.gnu.org/licenses/>.
*/
namespace F3;

//! Data validator
class Audit extends Prefab {
class Audit {

use Prefab;

//@{ User agents
const
Expand Down
4 changes: 4 additions & 0 deletions auth.php → f3/auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,13 @@
*/

namespace F3;

//! Authorization/authentication plug-in
class Auth {

use Prefab;

//@{ Error messages
const
E_LDAP='LDAP connection failure',
Expand Down
32 changes: 21 additions & 11 deletions base.php → f3/base.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,18 @@
*/

namespace F3;

//! Factory class for single-instance objects
abstract class Prefab {
trait Prefab {

/**
* Return class instance
* @return static
**/
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);
Expand All @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -2558,7 +2562,9 @@ function($level,$text,$file,$line) {
}

//! Cache engine
class Cache extends Prefab {
class Cache {

use Prefab;

protected
//! Cache DSN
Expand Down Expand Up @@ -2828,7 +2834,9 @@ function __construct($dsn=FALSE) {
}

//! View handler
class View extends Prefab {
class View {

use Prefab;

private
//! Temporary hive
Expand All @@ -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();
}

/**
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -3519,15 +3529,15 @@ class ISO extends Prefab {
* @return array
**/
function languages() {
return \Base::instance()->constants($this,'LC_');
return Base::instance()->constants($this,'LC_');
}

/**
* Return list of countries indexed by ISO 3166-1 country code
* @return array
**/
function countries() {
return \Base::instance()->constants($this,'CC_');
return Base::instance()->constants($this,'CC_');
}

}
Expand Down
2 changes: 2 additions & 0 deletions basket.php → f3/basket.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
*/

namespace F3;

//! Session-based pseudo-mapper
class Basket extends Magic {

Expand Down
6 changes: 5 additions & 1 deletion bcrypt.php → f3/bcrypt.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion cli/ws.php → f3/cli/ws.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
*/

namespace CLI;
namespace F3\CLI;

//! RFC6455 WebSocket server
class WS {
Expand Down
4 changes: 2 additions & 2 deletions db/cursor.php → f3/db/cursor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions db/jig.php → f3/db/jig.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
*/

namespace DB;
namespace F3\DB;

//! In-memory/flat-file DB wrapper
class Jig {
Expand Down Expand Up @@ -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:
Expand All @@ -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);
Expand Down Expand Up @@ -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;
}
Expand Down
34 changes: 17 additions & 17 deletions db/jig/mapper.php → f3/db/jig/mapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
*/

namespace DB\Jig;
namespace F3\DB\Jig;

//! Flat-file DB mapper
class Mapper extends \DB\Cursor {
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -129,7 +129,7 @@ function($token) {
return '$'.preg_replace_callback(
'/(\.\w+)|\[((?:[^\[\]]*|(?R))*)\]/',
function($expr) {
$fw=\Base::instance();
$fw=\F3\Base::instance();
return
'['.
($expr[1]?
Expand Down Expand Up @@ -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=[];
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
}

Expand All @@ -390,15 +390,15 @@ 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;
$db->write($this->file,$data);
$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;
Expand All @@ -413,15 +413,15 @@ 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;
$db->write($this->file,$data);
$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;
}
Expand Down Expand Up @@ -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);
Expand All @@ -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)).'/';
}
Expand All @@ -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;
}
Expand All @@ -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)
Expand All @@ -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;
}
Expand All @@ -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();
Expand Down
Loading

0 comments on commit 9505003

Please sign in to comment.