Skip to content

Instantly share code, notes, and snippets.

View dg's full-sized avatar
🏠
Working from home

David Grudl dg

🏠
Working from home
View GitHub Profile
@dg
dg / composing.presenters.php
Created May 31, 2018 14:00
Composing presenters without inheritance
<?php
// composing presenters without inheritance
// (requires nette/application 2.4.11)
trait RequireLoggedUser
{
public function injectRequireLoggedUser()
{
@dg
dg / benchmark.php
Created February 2, 2021 18:23
Benchmark Tracy Dumper vs Symfony VarDumper
<?php
declare(strict_types=1);
require __DIR__ . '/vendor/autoload.php';
// create container
$configurator = new Nette\Configurator;
$configurator->setTempDirectory(__DIR__ . '/temp');
@dg
dg / patch-CVE-2020-15227.php
Last active December 22, 2020 16:28
CVE-2020-15227 nette/application RCE in-place patch
<?php
# In-place apply the CVE-2020-15227 nette/application patch
# This is a universal patcher for all affected versions.
# Run with `php patch-CVE-2020-15227.php`
# Inspiration: @spazef0rze
@dg
dg / websocket.html
Created August 11, 2013 15:54
WebSocket communication between PHP and single client
<!doctype html>
<script>
if ("WebSocket" in window) {
var ws = new WebSocket("ws://127.0.0.1:31339");
ws.onopen = function() {
console.log('connected');
};
ws.onerror = function(e) {
@dg
dg / typehints.php
Created May 27, 2020 18:35
Typehints for PHP 7.4
<?php
declare(strict_types=1);
require '/nette.phar';
class TypeHints
{
public $ignoredTypes = ['mixed', 'resource'];
@dg
dg / template.latte
Created August 26, 2011 04:34
Výhody šablonovacího systému Latte

Zadáním je jednoduchý a zcela běžný úkol:

  • vypisuju nebufferované data z databáze (tj. neznám dopředu počet položek) jako elementy
  • každý lichý bude mít class="lichy"
  • poslední bude mít třídu class="posledni" (tedy class="posledni lichy", bude-li poslední zároveň lichý)
  • protože jsme puntičkáři, nechceme v kódu žádné prázdné
  • apod.
  • a samozřejmě veškerý výstup musí být escapovaný (tj. ošetřený)
@dg
dg / Nette
Created August 19, 2014 21:32
DI: Symfony versus Nette
services:
newsletter_factory: NewsletterFactory
newsletter_manager: @newsletter_factory::get(@templating)
@dg
dg / gist:1009307
Created June 5, 2011 19:29
Routing in Django verus Nette Framework

DJANGO

In urls.py

# urls like "articles/2011/tutorial03" or "articles/2011/tutorial03.html" or "articles/2011/tutorial03.htm"

urlpatterns = patterns('',
    (r'articles/(?P<year>\d+)/(?P<item>[^/]+)(?:\.htm(?:l)?)?/?\$', 'articles.detail'),
)
@dg
dg / persistent.parameter.trait.php
Created May 31, 2018 13:54
Transfer of a persistent parameter between two presenters without an inheritance
<?php
// transfer of a persistent parameter between two presenters without an inheritance
// (requires nette/application 3.0.0)
trait LangParameter
{
/** @persistent */
public $lang;
@dg
dg / StrictObject.php
Created June 11, 2018 08:51
Nette\StrictObject, simplified alternative for Nette\SmartObject (compatible with nette/utils 2.4)
<?php
/**
* This file is part of the Nette Framework (https://nette.org)
* Copyright (c) 2004 David Grudl (https://davidgrudl.com)
*/
namespace Nette;
use Nette\Utils\ObjectHelpers;