Skip to content

Commit

Permalink
mvc - cleanups for opnsense#6389
Browse files Browse the repository at this point in the history
* remove unused FactoryDefault() in tests
* refactor FactoryDefault() config access to new AppConfig class
* remove unused code in afterExecuteRoute() `$this->response->getHeaders()->get("Status")` will never be null
  • Loading branch information
AdSchellevis committed May 4, 2024
1 parent 3c17903 commit ab76fb0
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -395,19 +395,14 @@ public function beforeExecuteRoute($dispatcher)
*/
public function afterExecuteRoute($dispatcher)
{
// exit when response headers are already set
if ($this->response->getHeaders()->get("Status") != null) {
return false;
} else {
// process response, serialize to json object
$data = $dispatcher->getReturnedValue();
if (is_array($data)) {
$this->response->setContentType('application/json', 'UTF-8');
if ($this->isExternalClient()) {
$this->response->setContent(json_encode($data));
} else {
$this->response->setContent(htmlspecialchars(json_encode($data), ENT_NOQUOTES));
}
// process response, serialize to json object
$data = $dispatcher->getReturnedValue();
if (is_array($data)) {
$this->response->setContentType('application/json', 'UTF-8');
if ($this->isExternalClient()) {
$this->response->setContent(json_encode($data));
} else {
$this->response->setContent(htmlspecialchars(json_encode($data), ENT_NOQUOTES));
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/opnsense/mvc/app/library/OPNsense/Core/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

namespace OPNsense\Core;

use Phalcon\Di\FactoryDefault;
use OPNsense\Core\AppConfig;
use OPNsense\Core\Syslog;

/**
Expand Down Expand Up @@ -324,7 +324,7 @@ public function object()
protected function init()
{
$this->statusIsLocked = false;
$this->config_file = FactoryDefault::getDefault()->get('config')->globals->config_path . "config.xml";
$this->config_file = (new AppConfig())->globals->config_path . "config.xml";
try {
$this->load();
} catch (\Exception $e) {
Expand Down
7 changes: 4 additions & 3 deletions src/opnsense/mvc/app/library/OPNsense/Core/Shell.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

namespace OPNsense\Core;

use Phalcon\Di\FactoryDefault;
use OPNsense\Core\AppConfig;

/**
* Class Shell shell/command handling routines
Expand All @@ -54,8 +54,9 @@ class Shell
public function __construct()
{
// init, set simulation mode / debug autoput
$this->simulate = FactoryDefault::getDefault()->get('config')->globals->simulate_mode;
$this->debug = FactoryDefault::getDefault()->get('config')->globals->debug;
$appconfig = new AppConfig();
$this->simulate = $appconfig->globals->simulate_mode;
$this->debug = $appconfig->globals->debug;
}

/**
Expand Down
11 changes: 1 addition & 10 deletions src/opnsense/mvc/tests/setup.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

/*
* Copyright (C) 2016 Deciso B.V.
* Copyright (C) 2016 - 2024 Deciso B.V.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
Expand All @@ -26,9 +26,6 @@
* POSSIBILITY OF SUCH DAMAGE.
*/

use Phalcon\Di\Di;
use Phalcon\Di\FactoryDefault;

/**
* Read the configuration
*/
Expand All @@ -41,9 +38,3 @@
include __DIR__ . "/../app/config/loader.php";


$di = new FactoryDefault();
Di::reset();

$di->set('config', $config);

Di::setDefault($di);

0 comments on commit ab76fb0

Please sign in to comment.