Skip to content

Commit

Permalink
migrate profiles, imap/smtp/feed server list to repository trait with…
Browse files Browse the repository at this point in the history
… unique ID management to get rid of array index integer values mixing up when deleting entries
  • Loading branch information
kroky committed Feb 14, 2024
1 parent 180ce61 commit ce62109
Show file tree
Hide file tree
Showing 28 changed files with 206 additions and 1,567 deletions.
9 changes: 0 additions & 9 deletions config/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -724,15 +724,6 @@
// */
// 'feeds',

// /*
// | ----
// | POP3
// | ----
// |
// | POP3 E-mail account support
// */
// 'pop3',

// /*
// | -----
// | JMAP
Expand Down
1 change: 1 addition & 0 deletions lib/framework.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
define('VERSION', .1);

/* load the framework */
require APP_PATH.'lib/repository.php';
require APP_PATH.'lib/module.php';
require APP_PATH.'lib/modules.php';
require APP_PATH.'lib/modules_exec.php';
Expand Down
148 changes: 0 additions & 148 deletions lib/module.php
Original file line number Diff line number Diff line change
Expand Up @@ -283,151 +283,6 @@ private function validate_source($target, $source, $session, $request) {
}
}

abstract class Hm_Repository {

private $name;

private $entities;

private $default_entity_id;

private $session;

private $user_config;

public function __construct($name, $session, $user_config) {
$this->session = $session;
$this->name = $name;
$this->user_config = $user_config;
$this->load();
}

private function load() {
$this->entities = $this->user_config->get($this->name.'_entities');
$this->default_entity_id = $this->user_config->get($this->name.'_entity_default_id');
}

public function __call(string $name, array $arguments) {
if (preg_match('/findBy([A-Z])\w+/', $name)) {
return '';
}
throw new Exception("Argument " . $name . ' not found in ' . get_class($this));
}

public function setDefault($default) {
if (is_scalar($default)) {
$this->default_entity_id = $default;
}
if (is_array($default)) {
$this->default_entity_id = $default['id'];
}
if (is_object($default)) {
$this->default_entity_id = $default->id;
}
$this->commit();
}

public function count() {
return count($this->entities);
}

public function getAll() {
return $this->entities;
}

public function findById($id) {
if (array_key_exists($id, $this->entities)) {
return $this->entities[$id];
}
return false;
}

private function generateId() {
return uniqid();
}

private function commit() {
$this->user_config->set($this->name.'_entities', $this->entities);
$this->user_config->set($this->name.'_entity_default_id', $this->default_entity_id);
$this->session->set('user_data', $this->user_config->dump());
}

private function saveArrayEntity($entity) {
$created = false;
if (!array_key_exists('id', $entity)) {
$created = true;
$entity['id'] = $this->generateId();
}
$this->entities[$entity['id']] = $entity;
$this->commit();
if ($created) {
$this->session->record_unsaved(ucfirst($this->name).' added');
} else {
$this->session->record_unsaved(ucfirst($this->name).' updated');
}
}

public function save($entity) {
if (is_array($entity)) {
return $this->saveArrayEntity($entity);
}
throw new Exception("Entity must be an array or object");
}

public function deleteById($id) {
unset($this->entities[$id]);
$this->commit();
return true;
}

public function delete($id_or_entity) {
if (is_scalar($id_or_entity)) {
return $this->deleteById($id_or_entity);
}
}
}

class Hm_ModulesRepositoryRegistry {

private $modules;

private $repositories = array();

private $session;

private $user_config;

public function add_repository($name, $object) {
$this->repositories[$name] = $object;
}

public function __construct($modules, $session, $user_config) {
$this->modules = $modules;
$this->session = $session;
$this->user_config = $user_config;
$this->load_default_repositories();
#$this->repositories = array();
#$modules_path = getcwd() . '../modules';
#foreach (glob($modules_path . '/*' , GLOB_ONLYDIR) as $dir) {
#
#}
}

private function load_default_repositories() {
require_once getcwd() . '/lib/repository.php';
foreach ($this->modules as $module) {
$this->add_repository($module, new Hm_BaseRepository($module, $this->session, $this->user_config));
}
}

public function __get(string $name) {
if (array_key_exists($name, $this->repositories)) {
return $this->repositories[$name];
}
throw new Exception("Repository not registered");
}
}

/**
* Base class for data input processing modules, called "handler modules"
*
Expand Down Expand Up @@ -466,8 +321,6 @@ abstract class Hm_Handler_Module {

public $cache;

public $repositories;

/**
* Assign input and state sources
* @param object $parent instance of the Hm_Request_Handler class
Expand All @@ -484,7 +337,6 @@ public function __construct($parent, $page, $output=array(), $protected=array())
$this->user_config = $parent->user_config;
$this->output = $output;
$this->protected = $protected;
$this->repositories = new Hm_ModulesRepositoryRegistry($parent->site_config->get_modules(), $this->session, $this->user_config);
}

/**
Expand Down
1 change: 0 additions & 1 deletion lib/modules_exec.php
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,6 @@ class Hm_Module_Exec {
public $filters = array();
public $handlers = array();
public $outputs = array();
public $repositories = array();

/**
* @param object $config site config
Expand Down
73 changes: 70 additions & 3 deletions lib/repository.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,73 @@
<?php

class Hm_BaseRepository extends Hm_Repository
{
trait Hm_Repository {

}
protected static $name;
protected static $user_config;
protected static $session;
protected static $entities;

protected static function initRepo($name, $user_config, $session, &$entities) {
self::$name = $name;
self::$user_config = $user_config;
self::$session = $session;
self::$entities = &$entities;
$initial = self::$user_config->get(self::$name, []);
foreach ($initial as $entity) {
self::add($entity, false);
}
}

protected static function generateId() {
return uniqid();
}

public static function save() {
self::$user_config->set(self::$name, self::$entities);
self::$session->set('user_data', self::$user_config->dump());
}

public static function add($entity, $save = true) {
if (! array_key_exists('id', $entity)) {
$entity['id'] = self::generateId();
}
self::$entities[$entity['id']] = $entity;
if ($save) {
self::save();
}
}

public static function edit($id, $entity) {
if (array_key_exists($id, self::$entities)) {
self::$entities[$id] = $entity;
self::save();
return true;
}
return false;
}

public static function del($id) {
if (array_key_exists($id, self::$entities)) {
unset(self::$entities[$id]);
self::save();
return true;
}
return false;

}

public static function get($id) {
if (array_key_exists($id, self::$entities)) {
return self::$entities[$id];
}
return false;
}

public static function getAll() {
return self::$entities;
}

public static function count() {
return count(self::$entities);
}
}
46 changes: 15 additions & 31 deletions lib/servers.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,7 @@ public static function connect($id, $cache=false, $user=false, $pass=false, $sav
if (!array_key_exists($id, self::$server_list)) {
return false;
}
foreach (self::$server_list as $server_l) {
if ($server['id'] == $id) {
$server = $server_l;
}
}
$server = self::$server_list[$id];

if ($server['object']) {
return $server['object'];
Expand Down Expand Up @@ -179,6 +175,14 @@ public static function toggle_hidden($id, $hide) {
trait Hm_Server_List {

use Hm_Server_Modify;
use Hm_Repository {
Hm_Repository::add as repo_add;
Hm_Repository::get as repo_get;
}

public static function init($name, $user_config) {
self::initRepo($name, $user_config, self::$server_list);
}

/**
* Add a server definition
Expand All @@ -189,22 +193,7 @@ trait Hm_Server_List {
public static function add($atts) {
$atts['object'] = false;
$atts['connected'] = false;
self::$server_list[] = $atts;
}

/**
* Remove a server
* @param int $id server id
* @return bool true on success
*/
public static function del($id) {
foreach (self::$server_list as $idx => $srv) {
if ($srv['id'] == $id) {
unset(self::$server_list[$idx]);
return true;
}
}
return false;
self::repo_add($atts);
}

/**
Expand Down Expand Up @@ -233,24 +222,19 @@ public static function dump($id=false, $full=false) {
* @return array
*/
public static function get($id, $full) {
foreach (self::$server_list as $srv) {
if ($srv['id'] == $id) {
$server = $srv;
}
if (!$full) {
return self::clean($server);
}
return $server;
$server = self::repo_get($id);
if ($server && ! $full) {
return self::clean($server);
}
return array();
return $server;
}

/*
* @param array $server
* @return array
*/
public static function clean($server) {
if (!array_key_exists('pass', $server) || !$server['pass']) {
if (! array_key_exists('pass', $server) || ! $server['pass']) {
$server['nopass'] = true;
}
unset($server['pass']);
Expand Down
Loading

0 comments on commit ce62109

Please sign in to comment.