Skip to content

Latest commit

 

History

History
530 lines (494 loc) · 18.8 KB

php-iii-jan-2021.md

File metadata and controls

530 lines (494 loc) · 18.8 KB

PHP-III Jan 2021

Homework

  • For Fri 22 Jan 2021
    • Stratigility Lab: Add a Middleware
    • Lab: Mezzio Skeleton Installation
  • For Mon 18 Jan 2021
    • Lab: Docker Image Build
    • Lab: New Image Creation
    • Lab: Full-build MySQL Container
    • Lab: Configuration Review and Pre-Built Service Execution
    • Lab: Configuration Review and Partial-Build Service Execution
  • For Fri 15 Jan 2021
    • Lab: CD Phing Build Tool Prerequisites
    • Lab: Lab: Phing Build Execution
    • Lab: Phing Deployment Update
PHP Deprecated: Array and string offset access syntax with curly braces is deprecated
// maybe Phing needs to be update?
docker run -d -p 8080:8080 -p 50000:50000 -v jenkins_home:/var/jenkins_home jenkins/jenkins:lts
  • Lab: Jenkins CI Freestyle Project Configuration
  • Lab: Jenkins CI Freestyle Project Build
    • NOTE: the URL mentioned on slides is this: https://github.com/datashuttle/PHP3OA
    • OPTIONAL: fork the repo from datashuttle into your own github account and use that as a target repo you can then make changes and test to see if the automated process kicks in
  • Lab: Load (Smoke) Testing
  • Notes on Labs: https://github.com/dbierer/php-iii-mar-2020
  • Or look here: https://github.com/dbierer/php-class-notes/blob/master/php-iii-may-2019.md
  • For Wed 13 Jan 2021
    • Lab: New Functions
      • Installing a Custom Extension
        • change to this directory: /home/vagrant/Zend/workspaces/DefaultWorkspace/php3/src/ModAdvancedTechniques/Extensions/TelemetryExtension
        • Modify Makefile:
          • Change this: INIT_DIR to /etc/php/7.4/cli/conf.d
          • Make sure all the directives, starting with all: are on their own line
          • Arguments should be on subsequent lines with at least a single tab indent
        • If you get this error: make: Nothing to be done for 'all'.
          • Make sure that all: is on its own line
          • Make sure arguments for all: are on the following line(s)
          • Arguments need to have at least a single tab indent
PHP Warning:  PHP Startup: Unable to load dynamic library 'telemetry.so' (tried: /usr/lib/php/20190902/telemetry.so (libphpcpp.so.2.0: cannot open shared object file: No such file or directory), /usr/lib/php/20190902/telemetry.so.so (libphpcpp.so.2.0: cannot open shared object file: No such file or directory)) in Unknown on line 0
  • Lab: Custom Compile PHP
  • For Wed 6 Jan 2021
    • Setup Apache JMeter
    • Setup Jenkins CI
      • Need to install Jenkins 1st!
      • The CheckStyle plug-in reached end-of-life. All functionality has been integrated into the Warnings Next Generation Plugin.
      • Same applies to warnings and pmd : integrated into Warnings NG
      • Here are some other suggestions for initial setup:
        • replace checkstyle with Warnings Next Generation
        • replace build-environment with Build Environment
        • replace phing with Phing
        • replace violations with Violations
        • replace htmlpublisher with Build-Publisher (???)
        • replace version number with Version Number

TODO

  • Make arrangements with Nicole to get updated version of PDF to Svante (screenshots missing for REST Service API section
  • Create an example of a heap where you create a branch (e.g. another list associated with "Space Suite Check" from slide example)
  • Document extension creation lab: note missing libs

Q & A

zlib

ZLib Support => enabled
Stream Wrapper => compress.zlib://
Stream Filter => zlib.inflate, zlib.deflate
Compiled Version => 1.2.11
Linked Version => 1.2.11

Resources

Class Notes

<?php
$function = function () { return 'Whatever'; };
echo $function();
var_dump($function);

function test(int $max)
{
        $num = range(0, 99);
        $count = 0;
        for ($x = 0; $x < $max; $x++) {
                yield $num[$x];
                $count += $num[$x];
        }
        return $count;
}

// NOTE: $x is *NOT* the value from line 14!
$x = test(10);
foreach ($x as $letter) echo $letter;
// This gives you the value from line 14
// NOTE: can only getReturn() *after* the iteration has completed
echo "\nReturn Value:" . $x->getReturn();
var_dump($x);
  • ArrayObject example demonstrating use of internal IteratorAggregate interface as well as ArrayAccess
<?php
$source = ['A' => 111, 'B' => 222, 'C' => 333];
$obj = new ArrayObject($source);

// this is possible due to ArrayAccess interface implementation
echo $obj['A'];
echo $obj->offsetGet('B');
echo "\n";

// this is possible because of IteratorAggregate
foreach ($obj as $key => $val) {
        echo $key . ':' . $val . "\n";
}
echo "\n";

$iter = $obj->getIterator();
foreach ($iter as $key => $val) {
        echo $key . ':' . $val . "\n";
}
echo "\n";
<?php
// If this is not declared, scalar data-typing acts as a type cast
declare(strict_types=1);
class Test
{
        public $name = 'Fred';
        public function setName(string $name)
        {
                $this->name = $name;
        }
        public function getName() : string
        {
                return $this->name;
        }
}

$test = new Test;
$test->setName('Wilma');
echo $test->getName();


$test->setName(123);
echo $test->getName();
var_dump($test);
// accepts either a string or float as an argument
public function setParam(string|float $param) {
        $this->param = $param;
}
  • Example of iterable data type
<?php
// If this is not declared, scalar data-typing acts as a type cast
//declare(strict_types=1);
class Test
{
        public $iter = NULL;
        public function setIter(iterable $iter)
        {
                $this->iter = $iter;
        }
        public function getIterAsString()
        {
                $output = '';
                foreach ($this->iter as $key => $val)
                        $output .= $key . ':' . $val . "\n";
                return $output;
        }
}
$arr = ['A' => 111, 'B' => 222, 'C' => 333];
$test = new Test();
$test->setIter($arr);
echo $test->getIterAsString();

$obj = new ArrayObject($arr);
$test->setIter($obj);
echo $test->getIterAsString();
  • callable data type
<?php
// If this is not declared, scalar data-typing acts as a type cast
//declare(strict_types=1);
class Test
{
        public $callback = NULL;
        public function setCallback(callable $callback)
        {
                $this->callback = $callback;
        }
        public function getCallback()
        {
                return $this->callback;
        }
}

$func = function ($a, $b) { return $a + $b; };
$anon = new class() {
        public function add($a, $b) {
                return $a + $b;
        }
};
$test = new Test();
// no error
$test->setCallback($func);
// classes that define __invoke are considered callable
// if not, you can  use array syntax as shown:
$test->setCallback([$anon, 'add']);
// no error on defined functions
$test->setCallback('strtolower');
echo $test->getCallback()('ABCDEF');
$id = $_POST['id'] ?? $_GET['id'] ?? $_SESSION['id'] ?? 0;
<?php
$path = '/home/vagrant/Zend/workspaces/DefaultWorkspace/php3';
$directory = new RecursiveDirectoryIterator($path);
$iterator = new RecursiveIteratorIterator($directory);

foreach ($iterator as $name => $obj) {
        echo $name . "\n";
        var_dump($obj);
}
sudo apt install -y pkg-config build-essential autoconf bison re2c libxml2-dev libsqlite3-dev
* After cloning from github.com:
cd /path/to/cloned/php
tar xvfz php-src-file
cd php-src-xxx
./buildconf
* Example `configure` options
./configure  \
    --sysconfdir=/etc \
    --localstatedir=/var \
    --datadir=/usr/share/php \
    --mandir=/usr/share/man \
    --enable-fpm \
    --with-fpm-user=apache \
    --with-fpm-group=apache \
    --with-config-file-path=/etc \
    --with-zlib \
    --enable-bcmath \
    --with-bz2 \
    --enable-calendar \
    --enable-dba=shared \
    --with-gdbm \
    --with-gmp \
    --enable-ftp \
    --with-gettext=/usr \
    --enable-mbstring \
    --enable-pcntl \
    --with-pspell \
    --with-readline \
    --with-snmp \
    --with-mysql-sock=/run/mysqld/mysqld.sock \
    --with-curl \
    --with-openssl \
    --with-openssl-dir=/usr \
    --with-mhash \
    --enable-intl \
    --with-libdir=/lib64 \
    --enable-sockets \
    --with-libxml \
    --enable-soap \
    --enable-gd \
    --with-jpeg \
    --with-freetype \
    --enable-exif \
    --with-xsl \
    --with-pgsql \
    --with-pdo-mysql=/usr \
    --with-pdo-pgsql \
    --with-mysqli \
    --with-pdo-dblib \
    --with-ldap \
    --with-ldap-sasl \
    --enable-shmop \
    --enable-sysvsem \
    --enable-sysvshm \
    --enable-sysvmsg \
    --with-tidy \
    --with-expat \
    --with-enchant \
    --with-imap=/usr/local/imap-2007f \
    --with-imap-ssl=/usr/include/openssl \
    --with-kerberos=/usr/include/krb5 \
    --with-sodium=/usr \
    --with-zip \
    --enable-opcache \
    --with-pear \
    --with-ffi
  • Using PHP directly as a shell script
#!/usr/bin/php
<?php
echo 'Hello World!';
php -S IP-or-DNS:port -t DOC_ROOT_DIR
// example
php -S localhost:9999 -t public
  • Prototype StreamWrapper class
    • Doesn't exist: can't extend it
    • Contains definitions of required methods
    • Only define the ones you need
    • At a minimum: stream_open()
    • See: https://www.php.net/StreamWrapper
  • Docker Notes
    • To open a shell onto a running image (container)
docker exec -it keen_edison /bin/bash
  • To get detailed information:
docker image|container|volume|network ls
docker image|container|volume|network inspect <ID>
  • System maintenance
docker system prune

Mezzio

  • Create Mezzio project:
composer create-project mezzio/mezzio-skeleton </path/to/project>
  • Command line tool: /vendor/bin/mezzio
  • Example Middleware
<?php
declare(strict_types=1);
namespace Demo\Middleware;

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;

class Log implements MiddlewareInterface
{
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler) : ResponseInterface
    {
        // $response = $handler->handle($request);
        $message = __METHOD__ . ':' . get_class($request);
        error_log($message);
        return $handler->handle($request);
    }
}

ERRATA

  • Mod 4: Use Case: Makefile
    • Note that in the VM you can also use this path for INIT_DIR: /etc/php/7.4/cli/conf.d
  • Docker Lab
  • Dockerfile in oabuild
DEBIAN_FRONTEND=noninte7ractive add-apt-repository -y
  • Package errors out: just comment this out: covered by another package
python-software-properties
  • Need to install apt-utils before its used