[Best Practices] Remove direct DI access from view layer.

This commit is contained in:
Buster Silver 2017-08-02 03:05:29 -05:00
parent 8f5a2a983b
commit 77300569b5
10 changed files with 52 additions and 45 deletions

View File

@ -242,13 +242,40 @@ return function (\Slim\Container $di, $settings) {
};
$di['view'] = $di->factory(function ($di) {
$di['view'] = $di->factory(function (\Slim\Container $di) {
$view = new \App\Mvc\View(APP_INCLUDE_BASE . '/templates');
$view->setFileExtension('phtml');
$view->addAppCommands($di);
$view->loadExtension(new \App\Mvc\View\Paginator($di['url']));
$view->registerFunction('service', function($service) use ($di) {
return $di->get($service);
});
$view->registerFunction('escapeJs', function($string) {
return json_encode($string);
});
$view->registerFunction('mailto', function ($address, $link_text = null) {
$address = substr(chunk_split(bin2hex(" $address"), 2, ";&#x"), 3, -3);
$link_text = (is_null($link_text)) ? $address : $link_text;
return '<a href="mailto:' . $address . '">' . $link_text . '</a>';
});
$view->registerFunction('pluralize', function ($word, $num = 0) {
if ((int)$num == 1) {
return $word;
} else {
return \Doctrine\Common\Inflector\Inflector::pluralize($word);
}
});
$view->registerFunction('truncate', function ($text, $length = 80) {
return \App\Utilities::truncate_text($text, $length);
});
$view->addData([
'di' => $di,
'assets' => $di['assets'],
'auth' => $di['auth'],
'acl' => $di['acl'],

View File

@ -18,7 +18,17 @@ class IndexController extends BaseController
return $this->render('controller::noaccess');
}
$this->view->stations = $stations;
$view_stations = [];
foreach($stations as $row) {
/** @var Entity\Station $row */
$view_stations[] = [
'station' => $row,
'short_name' => $row->getShortName(),
'stream_url' => $row->getFrontendAdapter($this->di)->getStreamUrl(),
];
}
$this->view->stations = $view_stations;
/** @var \App\Cache $cache */
$cache = $this->di->get('cache');

View File

@ -16,7 +16,11 @@ class PublicController extends BaseController
public function preDispatch()
{
$this->station = $this->_getStation();
$this->view->station = $this->station;
$frontend = $this->station->getFrontendAdapter($this->di);
$this->view->stream_url = $frontend->getStreamUrl();
}
public function indexAction()

View File

@ -73,14 +73,14 @@
</thead>
<tbody>
<?php foreach($stations as $row): ?>
<tr class="input" id="station_<?=$row->id ?>">
<tr class="input" id="station_<?=$row['station']->id ?>">
<td class="text-center">
<a class="btn-audio" href="#" data-url="<?=$row->getFrontendAdapter($di)->getStreamUrl() ?>">
<a class="btn-audio" href="#" data-url="<?=$row['stream_url'] ?>">
<i class="zmdi zmdi-play"></i>
</a>
</td>
<td>
<big><a href="<?=$url->route(['controller' => 'public', 'station' => $row->getShortName()]) ?>" target="_blank"><?=$this->e($row->name) ?></a></big>
<big><a href="<?=$url->route(['controller' => 'public', 'station' => $row['short_name']]) ?>" target="_blank"><?=$this->e($row['station']->name) ?></a></big>
</td>
<td class="text-center">
<span class="nowplaying-listeners">0</span>
@ -90,7 +90,7 @@
<span class="nowplaying-artist"><?=_('Song Artist') ?></span>
</td>
<td>
<a class="btn btn-primary" href="<?=$url->route(['module' => 'stations', 'station' => $row->id]) ?>"><?=_('Manage') ?></a>
<a class="btn btn-primary" href="<?=$url->route(['module' => 'stations', 'station' => $row['station']->id]) ?>"><?=_('Manage') ?></a>
</td>
</tr>
<?php endforeach; ?>
@ -201,7 +201,7 @@ function nowPlaying() {
if (station_row.length) {
if ('mediaSession' in navigator) {
if (station_row.find('.btn-audio').hasClass('playing') && 'mediaSession' in navigator) {
navigator.mediaSession.metadata = new MediaMetadata({
title: row.now_playing.song.title,
artist: row.now_playing.song.artist

View File

@ -11,7 +11,7 @@
<div class="stations nowplaying">
<div class="media media-left" id="station_<?=$station->id ?>">
<div class="pull-left">
<a class="btn-audio" href="#" data-url="<?=$station->getFrontendAdapter($di)->getStreamUrl() ?>">
<a class="btn-audio" href="#" data-url="<?=$stream_url ?>">
<i class="zmdi zmdi-play"></i>
</a>
</div>

View File

@ -22,7 +22,7 @@
<div class="stations nowplaying">
<div class="media media-left" id="station_<?=$station->id ?>">
<div class="pull-left">
<a class="btn-audio" href="#" data-url="<?=$station->getFrontendAdapter($di)->getStreamUrl() ?>">
<a class="btn-audio" href="#" data-url="<?=$stream_url ?>">
<i class="zmdi zmdi-play"></i>
</a>
</div>

View File

@ -6,37 +6,6 @@ use League\Plates\Template\Data;
class View extends \League\Plates\Engine
{
/**
* Add "View Helpers" for common functions.
*/
public function addAppCommands(ContainerInterface $di)
{
$this->loadExtension(new View\Paginator($di['url']));
$this->registerFunction('escapeJs', function($string) {
return json_encode((string)$string);
});
$this->registerFunction('mailto', function ($address, $link_text = null) {
$address = substr(chunk_split(bin2hex(" $address"), 2, ";&#x"), 3, -3);
$link_text = (is_null($link_text)) ? $address : $link_text;
return '<a href="mailto:' . $address . '">' . $link_text . '</a>';
});
$this->registerFunction('pluralize', function ($word, $num = 0) {
if ((int)$num == 1) {
return $word;
} else {
return \Doctrine\Common\Inflector\Inflector::pluralize($word);
}
});
$this->registerFunction('truncate', function ($text, $length = 80) {
return \App\Utilities::truncate_text($text, $length);
});
}
protected $rendered = false;
protected $disabled = false;

View File

@ -2,7 +2,6 @@
<?php
/**
* @var \League\Plates\Template\Template $this
* @var \Psr\Container\ContainerInterface $di
* @var \App\Auth $auth
* @var \AzuraCast\Acl\StationAcl $acl
* @var \App\Url $url

View File

@ -2,7 +2,6 @@
<?php
/**
* @var \League\Plates\Template\Template $this
* @var \Psr\Container\ContainerInterface $di
* @var \App\Auth $auth
* @var \AzuraCast\Acl\StationAcl $acl
* @var \App\Url $url

View File

@ -2,7 +2,6 @@
<?php
/**
* @var \League\Plates\Template\Template $this
* @var \Psr\Container\ContainerInterface $di
* @var \App\Auth $auth
* @var \AzuraCast\Acl\StationAcl $acl
* @var \App\Url $url