Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
Merge branch 'release/2.0.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
Rudloff committed Apr 28, 2019
2 parents ff5f518 + 6a126d7 commit 033e363
Show file tree
Hide file tree
Showing 52 changed files with 4,732 additions and 2,562 deletions.
17 changes: 9 additions & 8 deletions .appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@
install:
- sc config wuauserv start= auto
- net start wuauserv
- cinst php composer ffmpeg phantomjs
- cinst php --version 7.1.28
- cinst composer ffmpeg phantomjs
- refreshenv
- copy C:\tools\php72\php.ini-development C:\tools\php72\php.ini
- echo extension=C:\tools\php72\ext\php_gmp.dll >> C:\tools\php72\php.ini
- echo extension=C:\tools\php72\ext\php_gettext.dll >> C:\tools\php72\php.ini
- echo extension=C:\tools\php72\ext\php_intl.dll >> C:\tools\php72\php.ini
- echo extension=C:\tools\php72\ext\php_openssl.dll >> C:\tools\php72\php.ini
- echo extension=C:\tools\php72\ext\php_mbstring.dll >> C:\tools\php72\php.ini
- copy C:\tools\php71\php.ini-development C:\tools\php71\php.ini
- echo extension=C:\tools\php71\ext\php_gmp.dll >> C:\tools\php71\php.ini
- echo extension=C:\tools\php71\ext\php_gettext.dll >> C:\tools\php71\php.ini
- echo extension=C:\tools\php71\ext\php_intl.dll >> C:\tools\php71\php.ini
- echo extension=C:\tools\php71\ext\php_openssl.dll >> C:\tools\php71\php.ini
- echo extension=C:\tools\php71\ext\php_mbstring.dll >> C:\tools\php71\php.ini
- composer install --no-dev
- composer global require phpunit/phpunit
- composer global require phpunit/phpunit:^6.0
- C:\Python36\python.exe -m pip install youtube-dl

test_script:
Expand Down
18 changes: 16 additions & 2 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,19 @@ module.exports = function (grunt) {
src: ['tests/*.php']
}
},
phpstan: {
options: {
level: 'max',
bin: 'vendor/bin/phpstan',
config: 'phpstan.neon'
},
php: {
src: ['*.php', 'classes/*.php', 'controllers/*.php']
},
tests: {
src: ['tests/*.php']
}
},
jslint: {
js: {
src: ['js/*.js']
Expand All @@ -57,7 +70,7 @@ module.exports = function (grunt) {
options: {
archive: 'alltube-<%= githash.main.tag %>.zip'
},
src: ['*.php', 'config/*', '!config/config.yml', 'dist/**', '.htaccess', 'img/**', 'LICENSE', 'README.md', 'robots.txt', 'resources/sitemap.xml', 'resources/manifest.json', 'templates/**', 'templates_c/', 'vendor/**', 'classes/**', 'controllers/**', 'bower_components/**', 'i18n/**', '!vendor/ffmpeg/**', '!vendor/bin/ffmpeg', '!vendor/anam/phantomjs-linux-x86-binary/**', '!vendor/bin/phantomjs', '!vendor/phpunit/**', '!vendor/squizlabs/**', '!vendor/rinvex/country/resources/geodata/*.json', '!vendor/rinvex/country/resources/flags/*.svg', 'node_modules/open-sans-fontface/fonts/**']
src: ['*.php', 'config/*', '!config/config.yml', 'dist/**', '.htaccess', 'img/**', 'LICENSE', 'README.md', 'robots.txt', 'resources/sitemap.xml', 'resources/manifest.json', 'templates/**', 'templates_c/', 'vendor/**', 'classes/**', 'controllers/**', 'bower_components/**', 'i18n/**', '!vendor/ffmpeg/**', '!vendor/bin/ffmpeg', '!vendor/anam/phantomjs-linux-x86-binary/**', '!vendor/bin/phantomjs', '!vendor/phpunit/**', '!vendor/squizlabs/**', '!vendor/rinvex/countries/resources/geodata/*.json', '!vendor/countries/country/resources/flags/*.svg', 'node_modules/open-sans-fontface/fonts/**']
}
},
phpdocumentor: {
Expand Down Expand Up @@ -125,9 +138,10 @@ module.exports = function (grunt) {
grunt.loadNpmTasks('grunt-potomo');
grunt.loadNpmTasks('grunt-contrib-csslint');
grunt.loadNpmTasks('grunt-markdownlint');
grunt.loadNpmTasks('grunt-phpstan');

grunt.registerTask('default', ['cssmin', 'potomo']);
grunt.registerTask('lint', ['csslint', 'fixpack', 'jsonlint', 'markdownlint', 'phpcs']);
grunt.registerTask('lint', ['csslint', 'fixpack', 'jsonlint', 'markdownlint', 'phpcs', 'phpstan']);
grunt.registerTask('test', ['phpunit']);
grunt.registerTask('doc', ['phpdocumentor']);
grunt.registerTask('release', ['default', 'githash', 'compress']);
Expand Down
16 changes: 7 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,19 +162,17 @@ You can then use it in your PHP code:

```php
use Alltube\Config;
use Alltube\VideoDownload;
use Alltube\Video;

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

$downloader = new VideoDownload(
new Config(
[
'youtubedl' => '/usr/local/bin/youtube-dl',
]
)
Config::setOptions(
[
'youtubedl' => '/usr/local/bin/youtube-dl',
]
);

$downloader->getURL('https://www.youtube.com/watch?v=dQw4w9WgXcQ');
$video = new Video('https://www.youtube.com/watch?v=dQw4w9WgXcQ');
$video->getUrl();
```

The library documentation is available on [alltube.surge.sh](https://alltube.surge.sh/classes/Alltube.VideoDownload.html).
Expand Down
105 changes: 77 additions & 28 deletions classes/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Config
/**
* Singleton instance.
*
* @var Config
* @var Config|null
*/
private static $instance;

Expand Down Expand Up @@ -129,18 +129,50 @@ class Config
/**
* Config constructor.
*
* @param array $options Options (see `config/config.example.yml` for available options)
* @param array $options Options
*/
private function __construct(array $options = [])
{
$this->applyOptions($options);
$this->getEnv();
$this->validateOptions();
}

/**
* Throw an exception if some of the options are invalid.
*
* @throws Exception If youtube-dl is missing
* @throws Exception If Python is missing
*
* @return void
*/
public function __construct(array $options)
private function validateOptions()
{
if (isset($options) && is_array($options)) {
foreach ($options as $option => $value) {
if (isset($this->$option) && isset($value)) {
$this->$option = $value;
}
/*
We don't translate these exceptions because they usually occur before Slim can catch them
so they will go to the logs.
*/
if (!is_file($this->youtubedl)) {
throw new Exception("Can't find youtube-dl at ".$this->youtubedl);
} elseif (!Video::checkCommand([$this->python, '--version'])) {
throw new Exception("Can't find Python at ".$this->python);
}
}

/**
* Apply the provided options.
*
* @param array $options Options
*
* @return void
*/
private function applyOptions(array $options)
{
foreach ($options as $option => $value) {
if (isset($this->$option) && isset($value)) {
$this->$option = $value;
}
}
$this->getEnv();
}

/**
Expand All @@ -161,34 +193,51 @@ private function getEnv()
}

/**
* Get Config singleton instance from YAML config file.
*
* @param string $yamlfile YAML config file name
* Get Config singleton instance.
*
* @return Config
*/
public static function getInstance($yamlfile = 'config/config.yml')
public static function getInstance()
{
$yamlPath = __DIR__.'/../'.$yamlfile;
if (is_null(self::$instance) || self::$instance->file != $yamlfile) {
if (is_file($yamlfile)) {
$options = Yaml::parse(file_get_contents($yamlPath));
} elseif ($yamlfile == 'config/config.yml' || empty($yamlfile)) {
/*
Allow for the default file to be missing in order to
not surprise users that did not create a config file
*/
$options = [];
} else {
throw new Exception("Can't find config file at ".$yamlPath);
}
self::$instance = new self($options);
self::$instance->file = $yamlfile;
if (!isset(self::$instance)) {
self::$instance = new self();
}

return self::$instance;
}

/**
* Set options from a YAML file.
*
* @param string $file Path to the YAML file
*/
public static function setFile($file)
{
if (is_file($file)) {
$options = Yaml::parse(file_get_contents($file));
self::$instance = new self($options);
} else {
throw new Exception("Can't find config file at ".$file);
}
}

/**
* Manually set some options.
*
* @param array $options Options (see `config/config.example.yml` for available options)
* @param bool $update True to update an existing instance
*/
public static function setOptions(array $options, $update = true)
{
if ($update) {
$config = self::getInstance();
$config->applyOptions($options);
$config->validateOptions();
} else {
self::$instance = new self($options);
}
}

/**
* Destroy singleton instance.
*
Expand Down
12 changes: 4 additions & 8 deletions classes/LocaleManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
namespace Alltube;

use Aura\Session\Segment;
use Aura\Session\SessionFactory;
use Symfony\Component\Process\Process;

/**
Expand All @@ -24,7 +23,7 @@ class LocaleManager
/**
* Current locale.
*
* @var Locale
* @var Locale|null
*/
private $curLocale;

Expand All @@ -37,13 +36,10 @@ class LocaleManager

/**
* LocaleManager constructor.
*
* @param array $cookies Cookie array
*/
public function __construct(array $cookies = [])
public function __construct()
{
$session_factory = new SessionFactory();
$session = $session_factory->newInstance($cookies);
$session = SessionManager::getSession();
$this->sessionSegment = $session->getSegment(self::class);
$cookieLocale = $this->sessionSegment->get('locale');
if (isset($cookieLocale)) {
Expand Down Expand Up @@ -78,7 +74,7 @@ public function getSupportedLocales()
/**
* Get the current locale.
*
* @return Locale
* @return Locale|null
*/
public function getLocale()
{
Expand Down
4 changes: 2 additions & 2 deletions classes/LocaleMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function __construct(ContainerInterface $container)
*
* @param array $proposedLocale Locale array created by AcceptLanguage::parse()
*
* @return string Locale name if chosen, nothing otherwise
* @return Locale Locale if chosen, nothing otherwise
*/
public function testLocale(array $proposedLocale)
{
Expand Down Expand Up @@ -65,7 +65,7 @@ public function __invoke(Request $request, Response $response, callable $next)
{
$headers = $request->getHeader('Accept-Language');
$curLocale = $this->localeManager->getLocale();
if (!isset($curLocale)) {
if (is_null($curLocale)) {
if (isset($headers[0])) {
$this->localeManager->setLocale(
AcceptLanguage::detect([$this, 'testLocale'], new Locale('en_US'), $headers[0])
Expand Down
Loading

0 comments on commit 033e363

Please sign in to comment.