Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ukol 2 - Milan Stanek #21

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions app/AppKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public function registerBundles()
new Doctrine\Bundle\DoctrineBundle\DoctrineBundle(),
new Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle(),
new AppBundle\AppBundle(),
new Knp\Bundle\PaginatorBundle\KnpPaginatorBundle(),
];

if (in_array($this->getEnvironment(), ['dev', 'test'], true)) {
Expand Down
4 changes: 4 additions & 0 deletions app/Resources/views/homepage/homepage.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,9 @@
<div class="row">
{% include 'components/list.html.twig' %}
</div>

<div class="navigation text-center">
{{ knp_pagination_render(products) }}
</div>
{% endblock %}

11 changes: 11 additions & 0 deletions app/config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,14 @@ swiftmailer:
username: "%mailer_user%"
password: "%mailer_password%"
spool: { type: memory }

knp_paginator:
page_range: 8
default_options:
page_name: page
sort_field_name: sort
sort_direction_name: direction
distinct: true
template:
pagination: KnpPaginatorBundle:Pagination:twitter_bootstrap_v3_pagination.html.twig
sortable: KnpPaginatorBundle:Pagination:sortable_link.html.twig
13 changes: 8 additions & 5 deletions bin/symfony_requirements
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ echo '> Checking Symfony requirements:'.PHP_EOL.' ';

$messages = array();
foreach ($symfonyRequirements->getRequirements() as $req) {
/** @var $req Requirement */
if ($helpText = get_error_message($req, $lineSize)) {
echo_style('red', 'E');
$messages['error'][] = $helpText;
Expand Down Expand Up @@ -121,10 +120,14 @@ function echo_block($style, $title, $message)

echo PHP_EOL.PHP_EOL;

echo_style($style, str_repeat(' ', $width).PHP_EOL);
echo_style($style, str_pad(' ['.$title.']', $width, ' ', STR_PAD_RIGHT).PHP_EOL);
echo_style($style, str_pad($message, $width, ' ', STR_PAD_RIGHT).PHP_EOL);
echo_style($style, str_repeat(' ', $width).PHP_EOL);
echo_style($style, str_repeat(' ', $width));
echo PHP_EOL;
echo_style($style, str_pad(' ['.$title.']', $width, ' ', STR_PAD_RIGHT));
echo PHP_EOL;
echo_style($style, $message);
echo PHP_EOL;
echo_style($style, str_repeat(' ', $width));
echo PHP_EOL;
}

function has_color_support()
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
"symfony/polyfill-apcu": "^1.0",
"sensio/distribution-bundle": "^5.0",
"sensio/framework-extra-bundle": "^3.0.2",
"incenteev/composer-parameter-handler": "^2.0"
"incenteev/composer-parameter-handler": "^2.0",
"knplabs/knp-paginator-bundle": "^2.5"
},
"require-dev": {
"sensio/generator-bundle": "^3.0",
Expand Down
88 changes: 50 additions & 38 deletions src/AppBundle/Controller/HomepageController.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php
namespace AppBundle\Controller;

use AppBundle\Entity\Category;
use AppBundle\Entity\Product;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
Expand All @@ -11,42 +12,53 @@
*/
class HomepageController extends Controller
{

/**
* @Route("/", name="homepage")
* @param Request $request
* @return \Symfony\Component\HttpFoundation\Response
*/
public function homepageAction(Request $request)
{
return $this->render("homepage/homepage.html.twig", [
"products" => $this->getDoctrine()->getRepository(Product::class)->findBy(
[],
[
"rank" => "desc"
],
20
),
"categories" => $this->getDoctrine()->getRepository(Category::class)->findBy(
[],
[
"rank" => "desc",
]
),
]);
}


/**
* @Route("/hello", name="hello")
* @param Request $request
* @return \Symfony\Component\HttpFoundation\Response
*/
public function helloAction(Request $request)
{
return $this->render("homepage/hello.html.twig", [
"who" => "world",
]);
}


/**
* @Route("/", name="homepage")
* @param Request $request
* @return \Symfony\Component\HttpFoundation\Response
*/
public function homepageAction(Request $request)
{

$paginator = $this->get('knp_paginator');
$em = $this->getDoctrine()->getManager();
$all = $em->getRepository(Product::class)->findAll();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tady pozor tohle ti vytáhne celou tabulku do paměti. To není zrovna efektivní. Při pár tisících položek a pár requestech ti umře jakýkoliv server. :)

$products = $paginator->paginate(
$all,
$request->query->getInt('page', 1),
9
);

return $this->render(
"homepage/homepage.html.twig",
[
"products" => $products
,
"categories" => $em->getRepository(Category::class)->findBy(
[],
[
"rank" => "desc",
]
),
]
);
}


/**
* @Route("/hello", name="hello")
* @param Request $request
* @return \Symfony\Component\HttpFoundation\Response
*/
public function helloAction(Request $request)
{
return $this->render(
"homepage/hello.html.twig",
[
"who" => "world",
]
);
}

}
74 changes: 56 additions & 18 deletions var/SymfonyRequirements.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,9 @@ public function __construct($cfgName, $evaluation, $approveCfgAbsence = false, $
*/
class RequirementCollection implements IteratorAggregate
{
/**
* @var Requirement[]
*/
private $requirements = array();

/**
Expand Down Expand Up @@ -265,7 +268,7 @@ public function addCollection(RequirementCollection $collection)
/**
* Returns both requirements and recommendations.
*
* @return array Array of Requirement instances
* @return Requirement[]
*/
public function all()
{
Expand All @@ -275,7 +278,7 @@ public function all()
/**
* Returns all mandatory requirements.
*
* @return array Array of Requirement instances
* @return Requirement[]
*/
public function getRequirements()
{
Expand All @@ -292,7 +295,7 @@ public function getRequirements()
/**
* Returns the mandatory requirements that were not met.
*
* @return array Array of Requirement instances
* @return Requirement[]
*/
public function getFailedRequirements()
{
Expand All @@ -309,7 +312,7 @@ public function getFailedRequirements()
/**
* Returns all optional recommendations.
*
* @return array Array of Requirement instances
* @return Requirement[]
*/
public function getRecommendations()
{
Expand All @@ -326,7 +329,7 @@ public function getRecommendations()
/**
* Returns the recommendations that were not met.
*
* @return array Array of Requirement instances
* @return Requirement[]
*/
public function getFailedRecommendations()
{
Expand Down Expand Up @@ -376,7 +379,8 @@ public function getPhpIniConfigPath()
*/
class SymfonyRequirements extends RequirementCollection
{
const REQUIRED_PHP_VERSION = '5.3.3';
const LEGACY_REQUIRED_PHP_VERSION = '5.3.3';
const REQUIRED_PHP_VERSION = '5.5.9';

/**
* Constructor that initializes the requirements.
Expand All @@ -386,16 +390,26 @@ public function __construct()
/* mandatory requirements follow */

$installedPhpVersion = phpversion();
$requiredPhpVersion = $this->getPhpRequiredVersion();

$this->addRequirement(
version_compare($installedPhpVersion, self::REQUIRED_PHP_VERSION, '>='),
sprintf('PHP version must be at least %s (%s installed)', self::REQUIRED_PHP_VERSION, $installedPhpVersion),
sprintf('You are running PHP version "<strong>%s</strong>", but Symfony needs at least PHP "<strong>%s</strong>" to run.
Before using Symfony, upgrade your PHP installation, preferably to the latest version.',
$installedPhpVersion, self::REQUIRED_PHP_VERSION),
sprintf('Install PHP %s or newer (installed version is %s)', self::REQUIRED_PHP_VERSION, $installedPhpVersion)
$this->addRecommendation(
$requiredPhpVersion,
'Vendors should be installed in order to check all requirements.',
'Run the <code>composer install</code> command.',
'Run the "composer install" command.'
);

if (false !== $requiredPhpVersion) {
$this->addRequirement(
version_compare($installedPhpVersion, $requiredPhpVersion, '>='),
sprintf('PHP version must be at least %s (%s installed)', $requiredPhpVersion, $installedPhpVersion),
sprintf('You are running PHP version "<strong>%s</strong>", but Symfony needs at least PHP "<strong>%s</strong>" to run.
Before using Symfony, upgrade your PHP installation, preferably to the latest version.',
$installedPhpVersion, $requiredPhpVersion),
sprintf('Install PHP %s or newer (installed version is %s)', $requiredPhpVersion, $installedPhpVersion)
);
}

$this->addRequirement(
version_compare($installedPhpVersion, '5.3.16', '!='),
'PHP version must not be 5.3.16 as Symfony won\'t work properly with it',
Expand Down Expand Up @@ -433,7 +447,7 @@ public function __construct()
);
}

if (version_compare($installedPhpVersion, self::REQUIRED_PHP_VERSION, '>=')) {
if (false !== $requiredPhpVersion && version_compare($installedPhpVersion, $requiredPhpVersion, '>=')) {
$timezones = array();
foreach (DateTimeZone::listAbbreviations() as $abbreviations) {
foreach ($abbreviations as $abbreviation) {
Expand Down Expand Up @@ -689,7 +703,7 @@ function_exists('posix_isatty'),
$this->addRecommendation(
\Symfony\Component\Intl\Intl::getIcuDataVersion() === \Symfony\Component\Intl\Intl::getIcuVersion(),
sprintf('intl ICU version installed on your system (%s) does not match the ICU data bundled with Symfony (%s)', \Symfony\Component\Intl\Intl::getIcuVersion(), \Symfony\Component\Intl\Intl::getIcuDataVersion()),
'To avoid internationalization data incosistencies upgrade the symfony/intl component.'
'To avoid internationalization data inconsistencies upgrade the symfony/intl component.'
);
}
}
Expand Down Expand Up @@ -725,9 +739,9 @@ function_exists('posix_isatty'),

if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
$this->addRecommendation(
$this->getRealpathCacheSize() > 1000,
'realpath_cache_size should be above 1024 in php.ini',
'Set "<strong>realpath_cache_size</strong>" to e.g. "<strong>1024</strong>" in php.ini<a href="#phpini">*</a> to improve performance on windows.'
$this->getRealpathCacheSize() >= 5 * 1024 * 1024,
'realpath_cache_size should be at least 5M in php.ini',
'Setting "<strong>realpath_cache_size</strong>" to e.g. "<strong>5242880</strong>" or "<strong>5M</strong>" in php.ini<a href="#phpini">*</a> may improve performance on Windows significantly in some cases.'
);
}

Expand Down Expand Up @@ -778,4 +792,28 @@ protected function getRealpathCacheSize()
return (int) $size;
}
}

/**
* Defines PHP required version from Symfony version.
*
* @return string|false The PHP required version or false if it could not be guessed
*/
protected function getPhpRequiredVersion()
{
if (!file_exists($path = __DIR__.'/../composer.lock')) {
return false;
}

$composerLock = json_decode(file_get_contents($path), true);
foreach ($composerLock['packages'] as $package) {
$name = $package['name'];
if ('symfony/symfony' !== $name && 'symfony/http-kernel' !== $name) {
continue;
}

return (int) $package['version'][1] > 2 ? self::REQUIRED_PHP_VERSION : self::LEGACY_REQUIRED_PHP_VERSION;
}

return false;
}
}
12 changes: 7 additions & 5 deletions web/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@

$majorProblems = $symfonyRequirements->getFailedRequirements();
$minorProblems = $symfonyRequirements->getFailedRecommendations();
$hasMajorProblems = (bool) count($majorProblems);
$hasMinorProblems = (bool) count($minorProblems);

?>
<!DOCTYPE html>
Expand Down Expand Up @@ -158,7 +160,7 @@
ready to run Symfony applications.
</p>

<?php if (count($majorProblems)): ?>
<?php if ($hasMajorProblems): ?>
<h2 class="ko">Major problems</h2>
<p>Major problems have been detected and <strong>must</strong> be fixed before continuing:</p>
<ol>
Expand All @@ -170,10 +172,10 @@
</ol>
<?php endif; ?>

<?php if (count($minorProblems)): ?>
<?php if ($hasMinorProblems): ?>
<h2>Recommendations</h2>
<p>
<?php if (count($majorProblems)): ?>Additionally, to<?php else: ?>To<?php endif; ?> enhance your Symfony experience,
<?php if ($hasMajorProblems): ?>Additionally, to<?php else: ?>To<?php endif; ?> enhance your Symfony experience,
it’s recommended that you fix the following:
</p>
<ol>
Expand All @@ -195,12 +197,12 @@
</p>
<?php endif; ?>

<?php if (!count($majorProblems) && !count($minorProblems)): ?>
<?php if (!$hasMajorProblems && !$hasMinorProblems): ?>
<p class="ok">All checks passed successfully. Your system is ready to run Symfony applications.</p>
<?php endif; ?>

<ul class="symfony-install-continue">
<?php if (count($majorProblems) || count($minorProblems)): ?>
<?php if ($hasMajorProblems || $hasMinorProblems): ?>
<li><a href="config.php">Re-check configuration</a></li>
<?php endif; ?>
</ul>
Expand Down