Реализация начальной интеграции с Twig и компонентами Symfony #59 от @VasyOk

This commit is contained in:
Exile 2016-02-10 05:39:14 +03:00
commit f87fe295b1
14 changed files with 507 additions and 9 deletions

View file

@ -32,6 +32,11 @@ require_once(BB_ROOT . 'library/config.php');
$di = new \TorrentPier\Di(); $di = new \TorrentPier\Di();
// TODO: Need to get locale from settings
$di['settings.locale'] = function($di) {
return 'ru';
};
$di->register(new \TorrentPier\ServiceProviders\ConfigServiceProvider, [ $di->register(new \TorrentPier\ServiceProviders\ConfigServiceProvider, [
'config.file.system.main' => __DIR__ . '/library/config.php', 'config.file.system.main' => __DIR__ . '/library/config.php',
'config.file.local.main' => __DIR__ . '/library/config.local.php', 'config.file.local.main' => __DIR__ . '/library/config.local.php',
@ -45,6 +50,22 @@ $di->register(new \TorrentPier\ServiceProviders\SphinxServiceProvider, [
'config.sphinx' => $di->config->sphinx->toArray() 'config.sphinx' => $di->config->sphinx->toArray()
]); ]);
$di->register(new \TorrentPier\ServiceProviders\RequestServiceProvider());
$di->register(new \TorrentPier\ServiceProviders\ResponseServiceProvider());
$di->register(new \TorrentPier\ServiceProviders\ViewServiceProvider());
$di->register(new \TorrentPier\ServiceProviders\TranslationServiceProvider(), [
'config.debug' => $di->config->debug,
'config.translator.dir_cache' => $di->config->translator->dir_cache,
'config.translator.resources' => $di->config->translator->resources->toArray()
]);
$di->register(new \TorrentPier\ServiceProviders\TwigServiceProvider, [
'config.debug' => $di->config->debug,
'config.twig.dir_templates' => $di->config->twig->dir_templates,
'config.twig.dir_cache' => $di->config->twig->dir_cache
]);
$bb_cfg = $di->config->toArray(); $bb_cfg = $di->config->toArray();
$page_cfg = $di->config->page->toArray(); $page_cfg = $di->config->page->toArray();
$tr_cfg = $di->config->tracker->toArray(); $tr_cfg = $di->config->tracker->toArray();

View file

@ -1,7 +1,6 @@
{ {
"name": "torrentpier/torrentpier", "name": "torrentpier/torrentpier",
"description": "TorrentPier. Bittorrent-tracker engine", "description": "TorrentPier. Bittorrent-tracker engine",
"version": "2.2.0-dev",
"type": "project", "type": "project",
"keywords": ["bittorrent","forum","torrent","tracker"], "keywords": ["bittorrent","forum","torrent","tracker"],
"homepage": "https://torrentpier.me/", "homepage": "https://torrentpier.me/",
@ -29,6 +28,9 @@
"pimple/pimple": "^3.0", "pimple/pimple": "^3.0",
"ripaclub/sphinxsearch": "^0.8.0", "ripaclub/sphinxsearch": "^0.8.0",
"rych/bencode": "^1.0", "rych/bencode": "^1.0",
"symfony/http-foundation": "^3.0",
"symfony/twig-bridge": "^3.0",
"symfony/translation": "^3.0",
"twbs/bootstrap": "v4.0.0-alpha.2", "twbs/bootstrap": "v4.0.0-alpha.2",
"twig/twig": "^1.0", "twig/twig": "^1.0",
"zendframework/zend-authentication": "^2.5", "zendframework/zend-authentication": "^2.5",
@ -49,6 +51,5 @@
} }
}, },
"minimum-stability": "stable", "minimum-stability": "stable",
"require-dev": {},
"prefer-stable": true "prefer-stable": true
} }

270
composer.lock generated
View file

@ -4,8 +4,8 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
"This file is @generated automatically" "This file is @generated automatically"
], ],
"hash": "e46f8da865b2faa0352586ebde1ddff4", "hash": "4c8ce64b7d919053dcf290ac5df1e97d",
"content-hash": "348b73c472823deef7d81e9895d5d93b", "content-hash": "c15ef109f287e872cadf52c757c15947",
"packages": [ "packages": [
{ {
"name": "container-interop/container-interop", "name": "container-interop/container-interop",
@ -241,6 +241,262 @@
], ],
"time": "2014-01-13 00:34:03" "time": "2014-01-13 00:34:03"
}, },
{
"name": "symfony/http-foundation",
"version": "v3.0.2",
"source": {
"type": "git",
"url": "https://github.com/symfony/http-foundation.git",
"reference": "9344a87ceedfc50354a39653e54257ee9aa6a77d"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/symfony/http-foundation/zipball/9344a87ceedfc50354a39653e54257ee9aa6a77d",
"reference": "9344a87ceedfc50354a39653e54257ee9aa6a77d",
"shasum": ""
},
"require": {
"php": ">=5.5.9"
},
"require-dev": {
"symfony/expression-language": "~2.8|~3.0"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "3.0-dev"
}
},
"autoload": {
"psr-4": {
"Symfony\\Component\\HttpFoundation\\": ""
},
"exclude-from-classmap": [
"/Tests/"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Fabien Potencier",
"email": "fabien@symfony.com"
},
{
"name": "Symfony Community",
"homepage": "https://symfony.com/contributors"
}
],
"description": "Symfony HttpFoundation Component",
"homepage": "https://symfony.com",
"time": "2016-02-02 13:44:19"
},
{
"name": "symfony/polyfill-mbstring",
"version": "v1.1.0",
"source": {
"type": "git",
"url": "https://github.com/symfony/polyfill-mbstring.git",
"reference": "1289d16209491b584839022f29257ad859b8532d"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/symfony/polyfill-mbstring/zipball/1289d16209491b584839022f29257ad859b8532d",
"reference": "1289d16209491b584839022f29257ad859b8532d",
"shasum": ""
},
"require": {
"php": ">=5.3.3"
},
"suggest": {
"ext-mbstring": "For best performance"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "1.1-dev"
}
},
"autoload": {
"psr-4": {
"Symfony\\Polyfill\\Mbstring\\": ""
},
"files": [
"bootstrap.php"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Nicolas Grekas",
"email": "p@tchwork.com"
},
{
"name": "Symfony Community",
"homepage": "https://symfony.com/contributors"
}
],
"description": "Symfony polyfill for the Mbstring extension",
"homepage": "https://symfony.com",
"keywords": [
"compatibility",
"mbstring",
"polyfill",
"portable",
"shim"
],
"time": "2016-01-20 09:13:37"
},
{
"name": "symfony/translation",
"version": "v3.0.2",
"source": {
"type": "git",
"url": "https://github.com/symfony/translation.git",
"reference": "2de0b6f7ebe43cffd8a06996ebec6aab79ea9e91"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/symfony/translation/zipball/2de0b6f7ebe43cffd8a06996ebec6aab79ea9e91",
"reference": "2de0b6f7ebe43cffd8a06996ebec6aab79ea9e91",
"shasum": ""
},
"require": {
"php": ">=5.5.9",
"symfony/polyfill-mbstring": "~1.0"
},
"conflict": {
"symfony/config": "<2.8"
},
"require-dev": {
"psr/log": "~1.0",
"symfony/config": "~2.8|~3.0",
"symfony/intl": "~2.8|~3.0",
"symfony/yaml": "~2.8|~3.0"
},
"suggest": {
"psr/log": "To use logging capability in translator",
"symfony/config": "",
"symfony/yaml": ""
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "3.0-dev"
}
},
"autoload": {
"psr-4": {
"Symfony\\Component\\Translation\\": ""
},
"exclude-from-classmap": [
"/Tests/"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Fabien Potencier",
"email": "fabien@symfony.com"
},
{
"name": "Symfony Community",
"homepage": "https://symfony.com/contributors"
}
],
"description": "Symfony Translation Component",
"homepage": "https://symfony.com",
"time": "2016-02-02 13:44:19"
},
{
"name": "symfony/twig-bridge",
"version": "v3.0.2",
"source": {
"type": "git",
"url": "https://github.com/symfony/twig-bridge.git",
"reference": "9f25144b9dabd772bede30aaee3a27e52af05784"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/symfony/twig-bridge/zipball/9f25144b9dabd772bede30aaee3a27e52af05784",
"reference": "9f25144b9dabd772bede30aaee3a27e52af05784",
"shasum": ""
},
"require": {
"php": ">=5.5.9",
"twig/twig": "~1.23|~2.0"
},
"require-dev": {
"symfony/asset": "~2.8|~3.0",
"symfony/console": "~2.8|~3.0",
"symfony/expression-language": "~2.8|~3.0",
"symfony/finder": "~2.8|~3.0",
"symfony/form": "~2.8|~3.0",
"symfony/http-kernel": "~2.8|~3.0",
"symfony/polyfill-intl-icu": "~1.0",
"symfony/routing": "~2.8|~3.0",
"symfony/security": "~2.8|~3.0",
"symfony/security-acl": "~2.8|~3.0",
"symfony/stopwatch": "~2.8|~3.0",
"symfony/templating": "~2.8|~3.0",
"symfony/translation": "~2.8|~3.0",
"symfony/var-dumper": "~2.8|~3.0",
"symfony/yaml": "~2.8|~3.0"
},
"suggest": {
"symfony/asset": "For using the AssetExtension",
"symfony/expression-language": "For using the ExpressionExtension",
"symfony/finder": "",
"symfony/form": "For using the FormExtension",
"symfony/http-kernel": "For using the HttpKernelExtension",
"symfony/routing": "For using the RoutingExtension",
"symfony/security": "For using the SecurityExtension",
"symfony/stopwatch": "For using the StopwatchExtension",
"symfony/templating": "For using the TwigEngine",
"symfony/translation": "For using the TranslationExtension",
"symfony/var-dumper": "For using the DumpExtension",
"symfony/yaml": "For using the YamlExtension"
},
"type": "symfony-bridge",
"extra": {
"branch-alias": {
"dev-master": "3.0-dev"
}
},
"autoload": {
"psr-4": {
"Symfony\\Bridge\\Twig\\": ""
},
"exclude-from-classmap": [
"/Tests/"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Fabien Potencier",
"email": "fabien@symfony.com"
},
{
"name": "Symfony Community",
"homepage": "https://symfony.com/contributors"
}
],
"description": "Symfony Twig Bridge",
"homepage": "https://symfony.com",
"time": "2016-01-30 16:03:33"
},
{ {
"name": "twbs/bootstrap", "name": "twbs/bootstrap",
"version": "v4.0.0-alpha.2", "version": "v4.0.0-alpha.2",
@ -740,16 +996,16 @@
}, },
{ {
"name": "zendframework/zend-filter", "name": "zendframework/zend-filter",
"version": "2.6.0", "version": "2.6.1",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/zendframework/zend-filter.git", "url": "https://github.com/zendframework/zend-filter.git",
"reference": "8682c12e9870c431cf29cbb7010627f3fa88dec8" "reference": "202014ee64e2aae23140a1719f6d362a602713ed"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/zendframework/zend-filter/zipball/8682c12e9870c431cf29cbb7010627f3fa88dec8", "url": "https://api.github.com/repos/zendframework/zend-filter/zipball/202014ee64e2aae23140a1719f6d362a602713ed",
"reference": "8682c12e9870c431cf29cbb7010627f3fa88dec8", "reference": "202014ee64e2aae23140a1719f6d362a602713ed",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
@ -792,7 +1048,7 @@
"filter", "filter",
"zf2" "zf2"
], ],
"time": "2016-02-04 19:52:41" "time": "2016-02-08 18:02:37"
}, },
{ {
"name": "zendframework/zend-hydrator", "name": "zendframework/zend-hydrator",

View file

@ -7,6 +7,9 @@ $domain_name = (!empty($_SERVER['SERVER_NAME'])) ? $_SERVER['SERVER_NAME'] : $do
$config = [ $config = [
// Debug (twig etc.)
'debug' => true,
// Increase number after changing js or css // Increase number after changing js or css
'js_ver' => 1, 'js_ver' => 1,
'css_ver' => 1, 'css_ver' => 1,
@ -26,6 +29,7 @@ $config = [
'charset' => 'utf8' 'charset' => 'utf8'
], ],
// Sphinx
'sphinx' => [ 'sphinx' => [
'driver' => 'Pdo_Mysql', 'driver' => 'Pdo_Mysql',
'hostname' => 'localhost', 'hostname' => 'localhost',
@ -35,6 +39,27 @@ $config = [
'charset' => 'utf8' 'charset' => 'utf8'
], ],
// Twig
'twig' => [
'dir_templates' => __DIR__ . '/../styles/templates/default',
'dir_cache' => __DIR__ . '/../internal_data/cache',
],
// Translation
'translator' => [
'dir_cache' => __DIR__ . '/../internal_data/cache',
'resources' => [
[
'resource' => __DIR__ . '/../messages/ru.php',
'locale' => 'ru',
],
[
'resource' => __DIR__ . '/../messages/en.php',
'locale' => 'en',
]
]
],
// Aliases // Aliases
// TODO: удалить // TODO: удалить
'db_alias' => [ 'db_alias' => [

12
messages/en.php Normal file
View file

@ -0,0 +1,12 @@
<?php
/**
* TorrentPier english localization
* In progress
*/
return [
'Style guide' => 'Style guide',
'Send' => 'Send',
'Hello, %name%' => 'Hello, %name%',
];

12
messages/ru.php Normal file
View file

@ -0,0 +1,12 @@
<?php
/**
* TorrentPier russian localization
* In progress
*/
return [
'Style guide' => 'Гид по стилю',
'Send' => 'Отправить',
'Hello, %name%' => 'Добро пожаловать, %name%',
];

View file

@ -0,0 +1,17 @@
<?php
namespace TorrentPier\ServiceProviders;
use Pimple\Container;
use Pimple\ServiceProviderInterface;
use Symfony\Component\HttpFoundation\Request;
class RequestServiceProvider implements ServiceProviderInterface
{
public function register(Container $container)
{
$container['request'] = function(Container $container) {
return Request::createFromGlobals();
};
}
}

View file

@ -0,0 +1,19 @@
<?php
namespace TorrentPier\ServiceProviders;
use Pimple\Container;
use Pimple\ServiceProviderInterface;
use Symfony\Component\HttpFoundation\Response;
class ResponseServiceProvider implements ServiceProviderInterface
{
public function register(Container $container)
{
$container['response'] = function(Container $container) {
$response = Response::create();
$response->prepare($container['request']);
return $response;
};
}
}

View file

@ -0,0 +1,31 @@
<?php
namespace TorrentPier\ServiceProviders;
use Pimple\Container;
use Pimple\ServiceProviderInterface;
use Symfony\Component\Translation\Loader\PhpFileLoader;
use Symfony\Component\Translation\Translator;
class TranslationServiceProvider implements ServiceProviderInterface
{
public function register(Container $container)
{
$container['translator'] = function(Container $container) {
$translator = new Translator(
$container['settings.locale'],
null,
null, // $container['config.translator.dir_cache'],
$container['config.debug']
);
$translator->addLoader('php', new PhpFileLoader());
foreach ($container['config.translator.resources'] as $item) {
$translator->addResource('php', $item['resource'], $item['locale']);
}
return $translator;
};
}
}

View file

@ -0,0 +1,30 @@
<?php
namespace TorrentPier\ServiceProviders;
use Pimple\Container;
use Pimple\ServiceProviderInterface;
use Symfony\Bridge\Twig\Extension\TranslationExtension;
class TwigServiceProvider implements ServiceProviderInterface
{
public function register(Container $container)
{
$container['twig'] = function(Container $container) {
$loader = new \Twig_Loader_Filesystem($container['config.twig.dir_templates']);
$twig = new \Twig_Environment($loader, [
'debug' => $container['config.debug'],
'cache' => $container['config.twig.dir_cache']
]);
$twig->addExtension(new \Twig_Extension_Core);
$twig->addExtension(new \Twig_Extension_Escaper);
$twig->addExtension(new \Twig_Extension_Optimizer);
$twig->addExtension(new \Twig_Extension_Debug);
$twig->addExtension(new TranslationExtension($container['translator']));
return $twig;
};
}
}

View file

@ -0,0 +1,17 @@
<?php
namespace TorrentPier\ServiceProviders;
use Pimple\Container;
use Pimple\ServiceProviderInterface;
use TorrentPier\View;
class ViewServiceProvider implements ServiceProviderInterface
{
public function register(Container $container)
{
$container['view'] = function(Container $container) {
return new View($container['twig']);
};
}
}

26
src/View.php Normal file
View file

@ -0,0 +1,26 @@
<?php
namespace TorrentPier;
class View
{
/**
* @var \Twig_Environment
*/
protected $twig;
public function __construct(\Twig_Environment $twig)
{
$this->twig = $twig;
}
/**
* @param $template
* @param array $params
* @return string
*/
public function make($template, $params = [])
{
return $this->twig->render($template . '.twig', $params);
}
}

21
styleguide.php Normal file
View file

@ -0,0 +1,21 @@
<?php
$t = microtime(true);
define('BB_SCRIPT', 'styleguide');
define('IN_LOGIN', true);
define('BB_ROOT', './');
require_once __DIR__ . '/common.php';
$user->session_start();
$di = \TorrentPier\Di::getInstance();
$content = $di->view->make('styleguide', [
'name' => $di->request->get('name', $user->data['username'])
]);
/** @var \Symfony\Component\HttpFoundation\Response $response */
$response = $di->response;
$response->setContent($content);
$response->send();

View file

@ -0,0 +1,10 @@
<h1>{% trans %}Style guide{% endtrans %}</h1>
<hr>
<form action="/styleguide.php" method="get">
<input type="text" name="name" value="{{ name }}">
<button type="submit">{% trans %}Send{% endtrans %}</button>
</form>
<pre>{% trans with { '%name%': name|e } %}Hello, %name%{% endtrans %}</pre>