Skip to content

Commit

Permalink
Inject logger instead of using it globally
Browse files Browse the repository at this point in the history
We now inject the logger everywhere.

This means some spouts (spouts\rss\feed and spouts\reddit\reddit2
in particular) now take dependencies in their constructor.
That also means their children will need to pass them up in constructor
chain if they have constructors of their own.

Spouts also no longer have getImageHelper method, the helper is injected
through constructor where necessary.

Similarly, the WebClient is injected to its users instead of getting
it using singleton class.

Lastly, the return type annotation of daos\Sources::validate
was narrowed down  to get rid of Psalm error.
  • Loading branch information
jtojnar committed May 1, 2020
1 parent 7fd0c84 commit 96c0c10
Show file tree
Hide file tree
Showing 25 changed files with 296 additions and 141 deletions.
18 changes: 11 additions & 7 deletions src/common.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,13 @@
return $dice->create($class);
});

$dice->addRule(Logger::class, [
'shared' => true,
'constructParams' => ['selfoss'],
]);

// init logger
$log = new Logger('selfoss');
$log = $dice->create(Logger::class);
if ($f3->get('logger_level') === 'NONE') {
$log->pushHandler(new NullHandler());
} else {
Expand All @@ -174,20 +179,19 @@
$handler->setFormatter($formatter);
$log->pushHandler($handler);
}
$f3->set('logger', $log);

// init error handling
$f3->set('ONERROR',
function(Base $f3) {
function(Base $f3) use ($log) {
$exception = $f3->get('EXCEPTION');

if ($exception) {
\F3::get('logger')->error($exception->getMessage(), ['exception' => $exception]);
$log->error($exception->getMessage(), ['exception' => $exception]);
} else {
\F3::get('logger')->error($f3->get('ERROR.text'));
$log->error($f3->get('ERROR.text'));
}

if (\F3::get('DEBUG') != 0) {
if ($f3->get('DEBUG') != 0) {
echo $f3->get('lang_error') . ': ';
echo $f3->get('ERROR.text') . "\n";
echo $f3->get('ERROR.trace');
Expand All @@ -197,6 +201,6 @@ function(Base $f3) {
}
);

if (\F3::get('DEBUG') != 0) {
if ($f3->get('DEBUG') != 0) {
ini_set('display_errors', '0');
}
23 changes: 14 additions & 9 deletions src/controllers/Opml/Export.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use helpers\Authentication;
use helpers\SpoutLoader;
use Monolog\Logger;

/**
* OPML loading and exporting controller
Expand All @@ -17,6 +18,9 @@ class Export {
/** @var Authentication authentication helper */
private $authentication;

/** @var Logger */
private $logger;

/** @var SpoutLoader */
private $spoutLoader;

Expand All @@ -29,8 +33,9 @@ class Export {
/** @var \daos\Tags */
private $tagsDao;

public function __construct(Authentication $authentication, \daos\Sources $sourcesDao, SpoutLoader $spoutLoader, \daos\Tags $tagsDao, \XMLWriter $writer) {
public function __construct(Authentication $authentication, Logger $logger, \daos\Sources $sourcesDao, SpoutLoader $spoutLoader, \daos\Tags $tagsDao, \XMLWriter $writer) {
$this->authentication = $authentication;
$this->logger = $logger;
$this->sourcesDao = $sourcesDao;
$this->spoutLoader = $spoutLoader;
$this->tagsDao = $tagsDao;
Expand Down Expand Up @@ -71,7 +76,7 @@ private function writeSource(array $source) {
$this->writer->writeAttributeNS('selfoss', 'params', null, html_entity_decode($source['params']));

$this->writer->endElement(); // outline
\F3::get('logger')->debug('done exporting source ' . $source['title']);
$this->logger->debug('done exporting source ' . $source['title']);
}

/**
Expand All @@ -84,7 +89,7 @@ private function writeSource(array $source) {
public function export() {
$this->authentication->needsLoggedIn();

\F3::get('logger')->debug('start OPML export');
$this->logger->debug('start OPML export');
$this->writer->openMemory();
$this->writer->setIndent(true);
$this->writer->setIndentString(' ');
Expand All @@ -101,13 +106,13 @@ public function export() {
$this->writer->writeAttribute('version', '1.0');
$this->writer->writeAttribute('createdOn', date('r'));
$this->writer->endElement(); // meta
\F3::get('logger')->debug('OPML export: finished writing meta');
$this->logger->debug('OPML export: finished writing meta');

$this->writer->startElement('head');
$user = \F3::get('username');
$this->writer->writeElement('title', ($user ? $user . '\'s' : 'My') . ' subscriptions in selfoss');
$this->writer->endElement(); // head
\F3::get('logger')->debug('OPML export: finished writing head');
$this->logger->debug('OPML export: finished writing head');

$this->writer->startElement('body');

Expand All @@ -126,13 +131,13 @@ public function export() {
// create associative array with tag names as keys, colors as values
$tagColors = [];
foreach ($this->tagsDao->get() as $key => $tag) {
\F3::get('logger')->debug('OPML export: tag ' . $tag['tag'] . ' has color ' . $tag['color']);
$this->logger->debug('OPML export: tag ' . $tag['tag'] . ' has color ' . $tag['color']);
$tagColors[$tag['tag']] = $tag['color'];
}

// generate outline elements for all sources
foreach ($sources['tagged'] as $tag => $children) {
\F3::get('logger')->debug("OPML export: exporting tag $tag sources");
$this->logger->debug("OPML export: exporting tag $tag sources");
$this->writer->startElement('outline');
$this->writer->writeAttribute('title', $tag);
$this->writer->writeAttribute('text', $tag);
Expand All @@ -146,15 +151,15 @@ public function export() {
$this->writer->endElement(); // outline
}

\F3::get('logger')->debug('OPML export: exporting untagged sources');
$this->logger->debug('OPML export: exporting untagged sources');
foreach ($sources['untagged'] as $key => $source) {
$this->writeSource($source);
}

$this->writer->endElement(); // body

$this->writer->endDocument();
\F3::get('logger')->debug('finished OPML export');
$this->logger->debug('finished OPML export');

// save content as file and suggest file name
$exportName = 'selfoss-subscriptions-' . date('YmdHis') . '.xml';
Expand Down
29 changes: 17 additions & 12 deletions src/controllers/Opml/Import.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use helpers\Authentication;
use helpers\View;
use Monolog\Logger;
use SimpleXMLElement;

/**
Expand All @@ -21,6 +22,9 @@ class Import {
/** @var array Sources that have been imported from the OPML file */
private $imported = [];

/** @var Logger */
private $logger;

/** @var \daos\Sources */
private $sourcesDao;

Expand All @@ -30,8 +34,9 @@ class Import {
/** @var View view helper */
private $view;

public function __construct(Authentication $authentication, \daos\Sources $sourcesDao, \daos\Tags $tagsDao, View $view) {
public function __construct(Authentication $authentication, Logger $logger, \daos\Sources $sourcesDao, \daos\Tags $tagsDao, View $view) {
$this->authentication = $authentication;
$this->logger = $logger;
$this->sourcesDao = $sourcesDao;
$this->tagsDao = $tagsDao;
$this->view = $view;
Expand Down Expand Up @@ -60,15 +65,15 @@ public function add() {
throw new \Exception('Unsupported file type: ' . $opml['type']);
}

\F3::get('logger')->debug('start OPML import ');
$this->logger->debug('start OPML import ');

$subs = simplexml_load_file($opml['tmp_name']);
$errors = $this->processGroup($subs->body);

// cleanup tags
$this->tagsDao->cleanup($this->sourcesDao->getAllTags());

\F3::get('logger')->debug('finished OPML import ');
$this->logger->debug('finished OPML import ');

// show errors
if (count($errors) > 0) {
Expand Down Expand Up @@ -166,9 +171,9 @@ private function addSubscription(SimpleXMLElement $xml, array $tags) {
// set spout for new item
if ($nsattrs->spout || $nsattrs->params) {
if (!($nsattrs->spout && $nsattrs->params)) {
\F3::get('logger')->warning("OPML import: failed to import '$title'");
$this->logger->warning("OPML import: failed to import '$title'");
$missingAttr = $nsattrs->spout ? '"selfoss:params"' : '"selfoss:spout"';
\F3::get('logger')->debug("Missing attribute: $missingAttr");
$this->logger->debug("Missing attribute: $missingAttr");

return $title;
}
Expand All @@ -177,17 +182,17 @@ private function addSubscription(SimpleXMLElement $xml, array $tags) {
} elseif (in_array((string) $attrs->type, ['rss', 'atom'], true)) {
$spout = 'spouts\rss\feed';
} else {
\F3::get('logger')->warning("OPML import: failed to import '$title'");
\F3::get('logger')->debug("Invalid type '$attrs->type': only 'rss' and 'atom' are supported");
$this->logger->warning("OPML import: failed to import '$title'");
$this->logger->debug("Invalid type '$attrs->type': only 'rss' and 'atom' are supported");

return $title;
}

// validate new item
$validation = @$this->sourcesDao->validate($title, $spout, $data);
if ($validation !== true) {
\F3::get('logger')->warning("OPML import: failed to import '$title'");
\F3::get('logger')->debug('Invalid source', $validation);
$this->logger->warning("OPML import: failed to import '$title'");
$this->logger->debug('Invalid source', $validation);

return $title;
}
Expand All @@ -198,16 +203,16 @@ private function addSubscription(SimpleXMLElement $xml, array $tags) {
$this->imported[$hash]['tags'] = array_unique(array_merge($this->imported[$hash]['tags'], $tags));
$tags = $this->imported[$hash]['tags'];
$this->sourcesDao->edit($this->imported[$hash]['id'], $title, $tags, '', $spout, $data);
\F3::get('logger')->debug("OPML import: updated tags for '$title'");
$this->logger->debug("OPML import: updated tags for '$title'");
} elseif ($id = $this->sourcesDao->checkIfExists($title, $spout, $data)) {
$tags = array_unique(array_merge($this->sourcesDao->getTags($id), $tags));
$this->sourcesDao->edit($id, $title, $tags, '', $spout, $data);
$this->imported[$hash] = ['id' => $id, 'tags' => $tags];
\F3::get('logger')->debug("OPML import: updated tags for '$title'");
$this->logger->debug("OPML import: updated tags for '$title'");
} else {
$id = $this->sourcesDao->add($title, $tags, '', $spout, $data);
$this->imported[$hash] = ['id' => $id, 'tags' => $tags];
\F3::get('logger')->debug("OPML import: successfully imported '$title'");
$this->logger->debug("OPML import: successfully imported '$title'");
}

// success
Expand Down
10 changes: 8 additions & 2 deletions src/daos/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace daos;

use Monolog\Logger;

const PARAM_INT = 1;
const PARAM_BOOL = 2;
const PARAM_CSV = 3;
Expand All @@ -18,12 +20,16 @@ class Database {
/** @var DatabaseInterface Instance of backend specific database access class */
private $backend = null;

/** @var Logger */
private $logger;

/**
* establish connection and
* create undefined tables
*/
public function __construct(DatabaseInterface $backend) {
public function __construct(DatabaseInterface $backend, Logger $logger) {
$this->backend = $backend;
$this->logger = $logger;
}

/**
Expand All @@ -38,7 +44,7 @@ public function __call($name, $args) {
if (method_exists($this->backend, $name)) {
return call_user_func_array([$this->backend, $name], $args);
} else {
\F3::get('logger')->error('Unimplemented method for ' . \F3::get('db_type') . ': ' . $name);
$this->logger->error('Unimplemented method for ' . \F3::get('db_type') . ': ' . $name);
}
}
}
9 changes: 7 additions & 2 deletions src/daos/Items.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace daos;

use helpers\Authentication;
use Monolog\Logger;

/**
* Class for accessing persistent saved items
Expand All @@ -19,14 +20,18 @@ class Items {
/** @var Authentication authentication helper */
private $authentication;

/** @var Logger */
private $logger;

/**
* Constructor.
*
* @return void
*/
public function __construct(Authentication $authentication, ItemsInterface $backend) {
public function __construct(Authentication $authentication, Logger $logger, ItemsInterface $backend) {
$this->authentication = $authentication;
$this->backend = $backend;
$this->logger = $logger;
}

/**
Expand All @@ -41,7 +46,7 @@ public function __call($name, $args) {
if (method_exists($this->backend, $name)) {
return call_user_func_array([$this->backend, $name], $args);
} else {
\F3::get('logger')->error('Unimplemented method for ' . \F3::get('db_type') . ': ' . $name);
$this->logger->error('Unimplemented method for ' . \F3::get('db_type') . ': ' . $name);
}
}

Expand Down
11 changes: 8 additions & 3 deletions src/daos/Sources.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use helpers\Authentication;
use helpers\SpoutLoader;
use Monolog\Logger;

/**
* Class for accessing persistent saved sources
Expand All @@ -21,6 +22,9 @@ class Sources {
/** @var Authentication authentication helper */
private $authentication;

/** @var Logger */
private $logger;

/** @var SpoutLoader spout loader */
private $spoutLoader;

Expand All @@ -29,9 +33,10 @@ class Sources {
*
* @return void
*/
public function __construct(Authentication $authentication, SourcesInterface $backend, SpoutLoader $spoutLoader) {
public function __construct(Authentication $authentication, Logger $logger, SourcesInterface $backend, SpoutLoader $spoutLoader) {
$this->authentication = $authentication;
$this->backend = $backend;
$this->logger = $logger;
$this->spoutLoader = $spoutLoader;
}

Expand All @@ -47,7 +52,7 @@ public function __call($name, $args) {
if (method_exists($this->backend, $name)) {
return call_user_func_array([$this->backend, $name], $args);
} else {
\F3::get('logger')->error('Unimplemented method for ' . \F3::get('db_type') . ': ' . $name);
$this->logger->error('Unimplemented method for ' . \F3::get('db_type') . ': ' . $name);
}
}

Expand Down Expand Up @@ -78,7 +83,7 @@ public function get($id = null) {
* @param string $spout class path for the spout
* @param array $params parameters supplied to the spout
*
* @return bool|array true on success or array of errors on failure
* @return true|array true on success or array of errors on failure
*
* @author Tobias Zeising
*/
Expand Down
Loading

0 comments on commit 96c0c10

Please sign in to comment.