Skip to content
This repository has been archived by the owner on Jul 12, 2020. It is now read-only.

Introducing Container class #222

Merged
merged 4 commits into from
Jul 18, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 21 additions & 8 deletions common.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,32 +39,45 @@
});

/**
* Closure to create a config class
* Closure to create a container class
*/
$config = call_user_func_array(
$container = call_user_func_array(
function($custom = 'custom')
{
// Create Container
$container = new \YoutubeDownloader\Container\SimpleContainer;

// Create Config
$ds = DIRECTORY_SEPARATOR;

$config_dir = realpath(__DIR__) . $ds . 'config' . $ds;

return \YoutubeDownloader\Config::createFromFiles(
$config = \YoutubeDownloader\Config::createFromFiles(
$config_dir . 'default.php',
$config_dir . $custom . '.php'
);

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

// Create Template\Engine
$template = \YoutubeDownloader\Template\Engine::createFromDirectory(
__DIR__ . DIRECTORY_SEPARATOR . 'templates'
);

$container->set('template', $template);

return $container;
},
[getenv('CONFIG_ENV') ?: 'custom']
);

// Show all errors on debug
if ( $config->get('debug') === true )
if ( $container->get('config')->get('debug') === true )
{
error_reporting(E_ALL);
ini_set('display_errors', 1);
}

$template = \YoutubeDownloader\Template\Engine::createFromDirectory(
__DIR__ . DIRECTORY_SEPARATOR . 'templates'
);
date_default_timezone_set($container->get('config')->get('default_timezone'));

date_default_timezone_set($config->get('default_timezone'));
return $container;
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
},
"require-dev": {
"mikey179/vfsStream": "^1.6",
"phpunit/phpunit": "^4.8.35 || ^6.0"
"phpunit/phpunit": "^4.8.35 || ^6.0",
"psr/container": "^1.0"
},
"autoload": {
"psr-4": {
Expand Down
5 changes: 4 additions & 1 deletion download.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<?php
include_once('common.php');
$container = include_once('common.php');

$config = $container->get('config');
$template = $container->get('template');

// Check download token
if (empty($_GET['mime']) OR empty($_GET['token']))
Expand Down
6 changes: 4 additions & 2 deletions getimage.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<?PHP
include_once('common.php');
$container = include_once('common.php');

$config = $container->get('config');
$template = $container->get('template');

if ( ! isset($_GET['videoid']) )
{
Expand All @@ -11,7 +14,6 @@

$my_id = \YoutubeDownloader\YoutubeDownloader::validateVideoId($_GET['videoid']);


if ( $my_id === null )
{
echo $template->render('error.php', [
Expand Down
5 changes: 4 additions & 1 deletion getvideo.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
// Takes a VideoID and outputs a list of formats in which the video can be
// downloaded

include_once('common.php');
$container = include_once('common.php');

$config = $container->get('config');
$template = $container->get('template');

if( ! isset($_GET['videoid']) )
{
Expand Down
4 changes: 3 additions & 1 deletion index.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php
$container = include_once('common.php');

include_once('common.php');
$config = $container->get('config');
$template = $container->get('template');

echo $template->render('index.php', [
'is_chrome' => \YoutubeDownloader\YoutubeDownloader::is_chrome(),
Expand Down
36 changes: 36 additions & 0 deletions src/Container/Container.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace YoutubeDownloader\Container;

/**
* Describes the interface of a container that exposes methods to read its entries.
*
* This interface is compatible with PSR-11 Psr\Container\ContainerInterface
*/
interface Container
{
/**
* Finds an entry of the container by its identifier and returns it.
*
* @param string $id Identifier of the entry to look for.
*
* @throws NotFoundExceptionInterface No entry was found for **this** identifier.
* @throws ContainerExceptionInterface Error while retrieving the entry.
*
* @return mixed Entry.
*/
public function get($id);

/**
* Returns true if the container can return an entry for the given identifier.
* Returns false otherwise.
*
* `has($id)` returning true does not mean that `get($id)` will not throw an exception.
* It does however mean that `get($id)` will not throw a `NotFoundExceptionInterface`.
*
* @param string $id Identifier of the entry to look for.
*
* @return bool
*/
public function has($id);
}
12 changes: 12 additions & 0 deletions src/Container/ContainerException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace YoutubeDownloader\Container;

use Exception;

/**
* Base interface representing a generic exception in a container.
*/
class ContainerException extends Exception
{
}
10 changes: 10 additions & 0 deletions src/Container/NotFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace YoutubeDownloader\Container;

/**
* No entry was found in the container.
*/
class NotFoundException extends ContainerException
{
}
67 changes: 67 additions & 0 deletions src/Container/SimpleContainer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

namespace YoutubeDownloader\Container;

/**
* A simple container implementation with a setter
*/
class SimpleContainer implements Container
{
/**
* @var array
*/
private $data = [];

/**
* Finds an entry of the container by its identifier and returns it.
*
* @param string $id Identifier of the entry to look for.
*
* @throws NotFoundException No entry was found for **this** identifier.
* @throws ContainerException Error while retrieving the entry.
*
* @return mixed Entry.
*/
public function get($id)
{
if ($this->has($id))
{
return $this->data[$id];
}

throw new NotFoundException(
sprintf('Entry "%s" don\'t exists in the container', $id)
);
}

/**
* Returns true if the container can return an entry for the given identifier.
* Returns false otherwise.
*
* `has($id)` returning true does not mean that `get($id)` will not throw an exception.
* It does however mean that `get($id)` will not throw a `NotFoundExceptionInterface`.
*
* @param string $id Identifier of the entry to look for.
*
* @return bool
*/
public function has($id)
{
return array_key_exists($id, $this->data);
}

/**
* Set a entry with an identifier
*
* @param string $id Identifier of the entry to look for.
* @param mixed $value the entry
*
* @return void
*/
public function set($id, $value)
{
$id = strval($id);

$this->data[$id] = $value;
}
}
74 changes: 74 additions & 0 deletions tests/Fixture/Container/Psr11ContainerAdapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

namespace YoutubeDownloader\Tests\Fixture\Container;

use Psr\Container\ContainerExceptionInterface;
use Psr\Container\ContainerInterface;
use Psr\Container\NotFoundExceptionInterface;
use YoutubeDownloader\Container\Container;
use YoutubeDownloader\Container\ContainerException;
use YoutubeDownloader\Container\NotFoundException;
use YoutubeDownloader\Container\SimpleContainer;

/**
* A simple PSR-11 container as a compatibility proof for SimpleContainer
*/
class Psr11ContainerAdapter implements ContainerInterface, Container
{
/**
* @var YoutubeDownloader\Container\SimpleContainer
*/
private $container;

/**
* @param YoutubeDownloader\Container\SimpleContainer $container
*
* @return void
*/
public function __construct(SimpleContainer $container)
{
$this->container = $container;
}

/**
* Finds an entry of the container by its identifier and returns it.
*
* @param string $id Identifier of the entry to look for.
*
* @throws Psr\Container\NotFoundExceptionInterface No entry was found for **this** identifier.
* @throws Psr\Container\ContainerExceptionInterface Error while retrieving the entry.
*
* @return mixed Entry.
*/
public function get($id)
{
try
{
return $this->container->get($id);
}
catch (NotFoundException $e)
{
throw new Psr11NotFoundException($e->getMessage());
}
catch (ContainerException $e)
{
throw new Psr11ContainerException($e->getMessage());
}
}

/**
* Returns true if the container can return an entry for the given identifier.
* Returns false otherwise.
*
* `has($id)` returning true does not mean that `get($id)` will not throw an exception.
* It does however mean that `get($id)` will not throw a `NotFoundExceptionInterface`.
*
* @param string $id Identifier of the entry to look for.
*
* @return bool
*/
public function has($id)
{
return $this->container->has($id);
}
}
13 changes: 13 additions & 0 deletions tests/Fixture/Container/Psr11ContainerException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace YoutubeDownloader\Container;

use Exception;
use Psr\Container\ContainerExceptionInterface;

/**
* Base interface representing a generic exception in a container.
*/
class Psr11ContainerException extends Exception implements ContainerExceptionInterface
{
}
12 changes: 12 additions & 0 deletions tests/Fixture/Container/Psr11NotFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace YoutubeDownloader\Container;

use Psr\Container\NotFoundExceptionInterface;

/**
* No entry was found in the container.
*/
class NotFoundException extends Psr11ContainerException implements NotFoundExceptionInterface
{
}
Loading