Skip to content

Commit

Permalink
init MVC framework refactory #14
Browse files Browse the repository at this point in the history
  • Loading branch information
ghost committed Sep 23, 2023
1 parent c4f5409 commit a600a08
Show file tree
Hide file tree
Showing 28 changed files with 1,230 additions and 920 deletions.
116 changes: 116 additions & 0 deletions src/app/controller/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
<?php

class AppControllerIndex
{
private $_db;
private $_sphinx;
private $_memory;

public function __construct()
{
require_once __DIR__ . '/../../library/database.php';
require_once __DIR__ . '/../../library/sphinx.php';
require_once __DIR__ . '/../../library/scrapeer.php';
require_once __DIR__ . '/../../library/time.php';
require_once __DIR__ . '/../../library/curl.php';
require_once __DIR__ . '/../../library/valid.php';
require_once __DIR__ . '/../../library/filter.php';

require_once __DIR__ . '/../../../vendor/autoload.php';

try
{
$this->_db = new Database(
DB_HOST,
DB_PORT,
DB_NAME,
DB_USERNAME,
DB_PASSWORD
);

$this->_sphinx = new Sphinx(
SPHINX_HOST,
SPHINX_PORT
);

$this->_memory = new \Yggverse\Cache\Memory(
MEMCACHED_HOST,
MEMCACHED_PORT,
MEMCACHED_NAMESPACE,
MEMCACHED_TIMEOUT + time()
);
}

catch (Exception $error)
{
require_once __DIR__ . '/error/500.php';

$controller = new AppControllerError500(
print_r($error, true)
);

$controller->render();

exit;
}
}

public function render()
{
$page = isset($_GET['page']) ? (int) $_GET['page'] : 1;

$pages = [];

require_once __DIR__ . '/module/pagination.php';

$appControllerModulePagination = new appControllerModulePagination();

require_once __DIR__ . '/module/head.php';

$appControllerModuleHead = new AppControllerModuleHead(
WEBSITE_URL,
$page > 1 ?
sprintf(
_('Page %s - BitTorrent Registry for Yggdrasil - %s'),
$page,
WEBSITE_NAME
) :
sprintf(
_('%s - BitTorrent Registry for Yggdrasil'),
WEBSITE_NAME
),
[
[
'rel' => 'stylesheet',
'type' => 'text/css',
'href' => sprintf(
'assets/theme/default/css/common.css?%s',
WEBSITE_CSS_VERSION
),
],
[
'rel' => 'stylesheet',
'type' => 'text/css',
'href' => sprintf(
'assets/theme/default/css/framework.css?%s',
WEBSITE_CSS_VERSION
),
],
]
);

require_once __DIR__ . '/module/profile.php';

$appControllerModuleProfile = new AppControllerModuleProfile($user->userId);

require_once __DIR__ . '/module/header.php';

$appControllerModuleHeader = new AppControllerModuleHeader();

require_once __DIR__ . '/module/footer.php';

$appControllerModuleFooter = new AppControllerModuleFooter();

include __DIR__ . '/../view/theme/default/index.phtml';
}
}
25 changes: 25 additions & 0 deletions src/app/controller/module/footer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

class AppControllerModuleFooter
{
public function render()
{
$response['trackers'] = [];

if ($trackers = json_decode(file_get_contents(__DIR__ . '/../../../config/trackers.json')))
{
foreach ($trackers as $tracker)
{
if (!empty($tracker->announce) && !empty($tracker->stats))
{
$response['trackers'][] = [
'announce' => $tracker->announce,
'stats' => $tracker->stats,
];
}
}
}

include __DIR__ . '../../../view/theme/default/module/footer.phtml';
}
}
54 changes: 54 additions & 0 deletions src/app/controller/module/head.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

class AppControllerModuleHead
{
private $_title;
private $_base;
private $_links = [];

public function __construct(string $base, string $title, array $links = [])
{
$this->setBase($base);
$this->setTitle($title);

foreach ($links as $link)
{
$this->addLink(
$link['rel'],
$link['type'],
$link['href'],
);
}
}

public function setBase(string $base) : void
{
$this->_base = $base;
}

public function setTitle(string $title) : void
{
$this->_title = $title;
}

public function addLink(string $rel, string $type, string $href) : void
{
$this->_links[] = (object)
[
'rel' => $rel,
'type' => $type,
'href' => $href,
];
}

public function render()
{
$base = $this->_base;

$links = $this->_links;

$title = htmlentities($this->_title);

include __DIR__ . '../../../view/theme/default/module/head.phtml';
}
}
19 changes: 19 additions & 0 deletions src/app/controller/module/header.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

class AppControllerModuleHeader
{
public function render()
{
$name = str_replace(
'YGG',
'<span>YGG</span>',
WEBSITE_NAME
);

require_once __DIR__ . '/search.php';

$appControllerModuleSearch = new AppControllerModuleSearch();

include __DIR__ . '../../../view/theme/default/module/header.phtml';
}
}
9 changes: 9 additions & 0 deletions src/app/controller/module/page.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

class AppControllerCommonPage
{
public function render(int $pageId)
{
include __DIR__ . '../../../view/theme/default/common/page.phtml';
}
}
43 changes: 43 additions & 0 deletions src/app/controller/module/pagination.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

class AppControllerModulePagination
{
public function render(string $url, int $total, int $limit)
{
if ($total > $limit)
{
parse_str($url, $query);

$pagination->page = isset($query['total']) ? (int) $query['total'] : 1;
$pagination->pages = ceil($total / $limit);

// Previous
if ($page > 1)
{
$query['page'] = $page - 1;

$pagination->back = sprintf('%s', WEBSITE_URL, http_build_query($query));
}

else
{
$pagination->back = false;
}

// Next
if ($page < ceil($total / $limit))
{
$query['page'] = $page + 1;

$pagination->next = sprintf('%s', WEBSITE_URL, http_build_query($query));
}

else
{
$pagination->next = false;
}

// Render
}
}
}
11 changes: 11 additions & 0 deletions src/app/controller/module/search.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

class AppControllerModuleSearch
{
public function render()
{
$query = empty($_GET['query']) ? false : urldecode($_GET['query']);

include __DIR__ . '../../../view/theme/default/module/search.phtml';
}
}
65 changes: 65 additions & 0 deletions src/app/controller/response.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

class AppControllerResponse
{
private $_title;
private $_h1;
private $_text;
private $_code;

public function __construct(string $title, string $h1, string $text, int $code = 200)
{
$this->_title = $title;
$this->_h1 = $h1;
$this->_text = $text;
$this->_code = $code;
}

public function render()
{
header(
sprintf(
'HTTP/1.0 %s Not Found',
$this->_code
)
);

$h1 = $this->_h1;
$text = $this->_text;

require_once __DIR__ . '/module/head.php';

$appControllerModuleHead = new AppControllerModuleHead(
WEBSITE_URL,
$this->_title,
[
[
'rel' => 'stylesheet',
'type' => 'text/css',
'href' => sprintf(
'assets/theme/default/css/common.css?%s',
WEBSITE_CSS_VERSION
),
],
[
'rel' => 'stylesheet',
'type' => 'text/css',
'href' => sprintf(
'assets/theme/default/css/framework.css?%s',
WEBSITE_CSS_VERSION
),
],
]
);

require_once __DIR__ . '/module/header.php';

$appControllerModuleHeader = new AppControllerModuleHeader();

require_once __DIR__ . '/module/footer.php';

$appControllerModuleFooter = new AppControllerModuleFooter();

include __DIR__ . '../../view/theme/default/response.phtml';
}
}
Loading

0 comments on commit a600a08

Please sign in to comment.