Skip to content

Zadání a řešení úkolů pro workshop "Moderní PHP"

License

Notifications You must be signed in to change notification settings

peoplepath/workshop-php

Repository files navigation

Where to start

Literature and other sources for PHP developers

Feel free to add yours favorite, thanks.

Tools

Feel free to add yours favorite, thanks.


Zadání

Napiště "univerzální" program, který přečte libovolně dlouhý textový soubor. Řádek po řádku bude aplikovat uživatelské filtry a dekorátory. Výstupem programu bude počet stejných (upravených) řádků a jejich četností.

Použijte co nejvíce vlastností moderního PHP. Doporučení:

Bonus

Upravte program tak, aby vypisoval průběžný stav nekonečného streamu.

Řešení

Bude zveřejněno během workshopu ve středu 28.3.2018. Přijďte :-)

Příklad

php old.php example.log

Vstupní soubor

[2018-03-13 12:16:10] test.DEBUG: Test message [] []
[2018-03-13 12:16:10] test.ERROR: Test message [] []
[2018-03-13 12:16:10] test.WARNING: Test message [] []
[2018-03-13 12:16:10] test.WARNING: Test message [] []
[2018-03-13 12:16:10] test.INFO: Test message [] []
[2018-03-13 12:16:10] test.NOTICE: Test message [] []
[2018-03-13 12:16:10] test.EMERGENCY: Test message [] []
[2018-03-13 12:16:10] test.ALERT: Test message [] []
[2018-03-13 12:16:10] test.ERROR: Test message [] []
[2018-03-13 12:16:10] test.NOTICE: Test message [] []

Výstup

error: 2
warning: 2
notice: 2
info: 1
emergency: 1
alert: 1

Implementace ve "starém" PHP

<?php
$pattern = '/test\.(\w+)/';

// read and parse file
$log  = file_get_contents($argv[1]);
$rows = explode(PHP_EOL, $log);

// build stats
$stats = array();
foreach ($rows as $row) {
    // decorator: extract log level
    if (preg_match($pattern, $row, $matches)) {
        $level = strtolower($matches[1]);

        // filter: do not accept DEBUG
        if ($level != 'debug') {
            if (array_key_exists($level, $stats)) {
                $stats[$level]++;
            } else {
                $stats[$level] = 1;
            }
        }
    }
}

// show stats
arsort($stats);
foreach ($stats as $level => $count) {
    echo "$level: $count" . PHP_EOL;
}

About

Zadání a řešení úkolů pro workshop "Moderní PHP"

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages