Run test and coverage report with ant build in Windows
So, you want to run test and coverage report commands in Windows environment. For example, you want to run kahlan
command for test, then make coverage report by istanbul
and istanbul-merge
.
Based on the ant
documentation on exec part, it noted as follow:
The task delegates to Runtime.exec which in turn apparently calls ::CreateProcess. It is the latter Win32 function that defines the exact semantics of the call. In particular, if you do not put a file extension on the executable, only .EXE files are looked for, not .COM, .CMD or other file types listed in the environment variable PATHEXT. That is only used by the shell.
Note that .bat files cannot in general by executed directly. One normally needs to execute the command shell executable cmd using the /c switch.
If you are familiar with ant command, usually, you can create a build.xml
file as default build configuration. You can create another build file that you can mention it when run the ant
command, eg: build-windows.xml
:
$ ant -buildfile build-windows.xml
Your build-windows.xml
then can use “cmd” as executable
property with /c /path/to/executable-file
as argument. The file will like below:
<?xml version="1.0" encoding="UTF-8"?> <project name="Your Application name" default="build"> <property name="toolsdir" value="${basedir}/vendor/bin/"/> <property name="moduledir" value="${basedir}/module/"/> <target name="build" depends="kahlan,coverage-report" description=""/> <target name="kahlan" description="Run kahlan"> <!-- Application --> <exec executable="cmd" failonerror="true" taskname="kahlan"> <arg line="/c ${toolsdir}kahlan.bat --spec=${moduledir}Application/spec/ --src=${moduledir}Application/src --istanbul=coverage/coverage-application.json "/> </exec> <!-- Application --> <!-- other modules to be tested here --> </target> <target name="coverage-report" description="Run coverage report generation"> <!-- merge coverages for many modules test --> <exec executable="cmd" failonerror="true" taskname="istanbul merge"> <arg line="/c istanbul-merge --out coverage.json coverage/*.json"/> </exec> <!-- make report --> <exec executable="cmd" failonerror="true" taskname="istanbul report"> <arg line="/c istanbul report"/> </exec> </target> </project>
With above, when you run the command, if it succeed, it will likely as follow:
$ ant -buildfile build-windows.xml Buildfile: D:\app\build-windows.xml kahlan: [kahlan] _ _ [kahlan] /\ /\__ _| |__ | | __ _ _ __ [kahlan] / //_/ _` | '_ \| |/ _` | '_ \ [kahlan] / __ \ (_| | | | | | (_| | | | | [kahlan] \/ \/\__,_|_| |_|_|\__,_|_| | | [kahlan] [kahlan] The PHP Test Framework for Freedom, Truth and Justice. [kahlan] [kahlan] src directory : D:\app\module\Application\src [kahlan] spec directory : D:\app\module\Application\spec [kahlan] [kahlan] ...................................................... 11 / 11 (100%) [kahlan] [kahlan] [kahlan] [kahlan] Expectations : 11 Executed [kahlan] Specifications : 0 Pending, 0 Excluded, 0 Skipped [kahlan] [kahlan] Passed 11 of 11 PASS in 1.831 seconds (using 8Mo) [kahlan] [kahlan] Coverage Summary [kahlan] ---------------- [kahlan] [kahlan] Total: 100.00% (25/25) [kahlan] [kahlan] Coverage collected in 0.035 seconds coverage-report: [istanbul report] Done build: BUILD SUCCESSFUL Total time: 4 seconds
That’s it!
References:
Tinify : Resize your image without reduce quality
I tried some client library services for my project to resize images. They resized images correctly, however, they made the images quality slightly reduced. Until my client suggested me to try Tinify API service, that the API key I can just use, and it works pretty well.
The Tinify provide free 500 compresions per-month if you need to try. You can upgrade to paid account if you need more. To register, you can go to https://tinypng.com/developers and got the following page and fill your name and email and click “Get your API key”:
You can then check your email to get the link and be redirected to the dashboard page with information of your API key and how many compressions left for current month:
There are libraries for different languages, for example, if you are using PHP, you can use tinify-php that you can require via composer in your project by run:
$ composer require tinify/tinify
Once required, you can do :
include 'vendor/autoload.php'; \Tinify\setKey('YOUR_API_KEY'); // fill with your API key $source = \Tinify\fromFile('/path/to/file.png'); // source file $resized = $source->resize(array( 'method' => 'scale', 'width' => 500, )); $resized->toFile('target-thumbnail.png'); // target file
For complete reference, you can look at https://tinypng.com/developers/reference/php . That’s it!
Create API Service in CodeIgniter 4 with Request Filtering
In CodeIgniter 4, there is a trait that specialized to be used for API, named CodeIgniter\API\ResponseTrait
for that, that consist of collection of functionality to build a response. How about request filtering ? We will need a filter class that implements CodeIgniter\Filters\FilterInterface
interface.
For example, we are going to create a /api/ping
api service, which will returns time value, we can create controller as follow:
<?php // application/Controllers/Api/Ping.php declare(strict_types=1); namespace App\Controllers\Api; use function time; use CodeIgniter\API\ResponseTrait; use CodeIgniter\Controller; use CodeIgniter\HTTP\ResponseInterface; final class Ping extends Controller { use ResponseTrait; public function index(): ResponseInterface { return $this->respond(['ack' => time()]); } }
Without filtering, we can call url and got result like the following based on Accept header, for example, for application/xml
, it will get:
➜ ~ curl -i -H "Accept: application/xml" http://localhost:8080/api/ping HTTP/1.1 200 OK Host: localhost:8080 Date: Sat, 22 Sep 2018 01:11:30 -0500 Connection: close X-Powered-By: PHP/7.2.9 Cache-control: no-store, max-age=0, no-cache Content-Type: application/xml; charset=UTF-8 Debugbar-Time: 1537596690 Debugbar-Link: http://localhost:8080/index.php/?debugbar_time=1537596690 <?xml version="1.0"?> <response><ack>1537596690</ack></response>
and when it changed to application/json
, it will get:
➜ ~ curl -i -H "Accept: application/json" http://localhost:8080/api/ping HTTP/1.1 200 OK Host: localhost:8080 Date: Sat, 22 Sep 2018 01:11:53 -0500 Connection: close X-Powered-By: PHP/7.2.9 Cache-control: no-store, max-age=0, no-cache Content-Type: application/json; charset=UTF-8 Debugbar-Time: 1537596713 Debugbar-Link: http://localhost:8080/index.php/?debugbar_time=1537596713 { "ack": 1537596713 }
Let’s try create filter it to only allow the “POST” request! We can create filter as follow:
<?php // application/Filters/PostRequestOnly.php declare(strict_types=1); namespace App\Filters; use CodeIgniter\Filters\FilterInterface; use CodeIgniter\HTTP\RequestInterface; use CodeIgniter\HTTP\ResponseInterface; use Config\Services; final class PostRequestOnly implements FilterInterface { public function before(RequestInterface $request) { if ($request->getMethod() === 'post') { return; } return Services::response() ->setStatusCode(ResponseInterface::HTTP_METHOD_NOT_ALLOWED); } public function after(RequestInterface $request, ResponseInterface $response) { } }
In above PostRequestOnly
filter, we are allowing request only for “POST”, other request methods will get response with “Method Not Allowed” (405). To make it work, we need register it into Config\Filters
::$aliases
class under application
directory, and ensure it applied into Config\Filters
::$filters
to register specific uri for the filter, as follow:
<?php // application/Config/Filters.php // ... use App\Filters\PostRequestOnly; class Filters extends BaseConfig { public $aliases = [ // ... 'postRequestOnly' => PostRequestOnly::class, ]; // ... public $filters = [ // ... 'postRequestOnly' => [ 'before' => [ 'api/ping', ], ], ]; }
That’s it! Now, when we try to see different result with GET
and POST
, it will get the following response:
GET: Method Not Allowed 405
➜ ~ curl -i -H "Accept: application/json" -X GET http://localhost:8080/api/ping HTTP/1.1 405 Method Not Allowed Host: localhost:8080 Date: Sat, 22 Sep 2018 13:12:22 +0700 Connection: close X-Powered-By: PHP/7.2.9 Cache-control: no-store, max-age=0, no-cache Content-Type: text/html; charset=UTF-8
POST: Got Response OK 200
➜ ~ curl -i -H "Accept: application/json" -X POST http://localhost:8080/api/ping HTTP/1.1 200 OK Host: localhost:8080 Date: Sat, 22 Sep 2018 01:12:54 -0500 Connection: close X-Powered-By: PHP/7.2.9 Cache-control: no-store, max-age=0, no-cache Content-Type: application/json; charset=UTF-8 Debugbar-Time: 1537596774 Debugbar-Link: http://localhost:8080/index.php/?debugbar_time=1537596774 { "ack": 1537596774 }
Create Middleware for File Post/Redirect/Get in Expressive 3
Previously, I wrote a post about Post/Redirect/Get in Expressive 3 which specifically handle POST parsed body. How about File upload? Let’s do it!
In this post, I will do with Zend\Form
component.
Let’s start by install a fresh skeleton:
$ composer create-project \ zendframework/zend-expressive-skeleton \ expressive-fileprg-tutorial
To save temporary the form data and its invalid input error messages during redirect, we can use session, so, we can require session components:
$ cd expressive-fileprg-tutorial $ composer require \ zendframework/zend-expressive-session:^1.0.0 \ zendframework/zend-expressive-session-ext:^1.1.1
After above components installed, ensure that your config/config.php injected with ConfigProvider like below:
<?php // config/config.php $aggregator = new ConfigAggregator([ // session requirements \Zend\Expressive\Session\Ext\ConfigProvider::class, \Zend\Expressive\Session\ConfigProvider::class, // ... ]);
The session middleware from zend-expressive-session
component need to be registered in our config/pipeline.php
before RouteMiddleware
:
// config/pipeline.php // ... use Zend\Expressive\Session\SessionMiddleware; // ... $app->pipe(SessionMiddleware::class); $app->pipe(RouteMiddleware::class);
Next, we are going to require a form dependencies for it with a command:
$ composer require \ zendframework/zend-form:^2.11 \ zendframework/zend-i18n:^2.7
After above components installed, ensure that your config/config.php have sessions and form ConfigProviders:
<?php // config/config.php $aggregator = new ConfigAggregator([ // session requirements \Zend\Expressive\Session\Ext\ConfigProvider::class, \Zend\Expressive\Session\ConfigProvider::class, // ... // form requirements \Zend\I18n\ConfigProvider::class, \Zend\Form\ConfigProvider::class, \Zend\InputFilter\ConfigProvider::class, \Zend\Filter\ConfigProvider::class, \Zend\Hydrator\ConfigProvider::class, ]);
The New Middleware
We are going to create a middleware for our application, for example, we name it FilePrgMiddleware
, placed at src/App/Middleware. I will explain part by part.
Unlike the normal PRG
middleware in previous post, the FilePrgMiddleware
need to bring the Zend\Form\Form
object to be filtered and validated, so, it will be applied in the routes after the Page handler registration.
First, we check whether the request method is POST and has the media type is “multipart/form-data”, then applied POST and FILES data into the form instance. We can save form post and file data, a form filtered/validated data (“form->getData()”), and its form error messages into session. Next, we redirect to current page with status code = 303.
As I explored, the easiest way to work with zend-form
for post and files data is by using zend-psr7bridge
for it like below:
use Zend\Diactoros\Response\RedirectResponse; use Zend\Expressive\Session\SessionMiddleware; use Zend\Psr7Bridge\Psr7ServerRequest; $session = $request->getAttribute(SessionMiddleware::SESSION_ATTRIBUTE); $zendRequest = Psr7ServerRequest::toZend($request); $zendRequestHeaders = $zendRequest->getHeaders(); $isMultiPart = $zendRequestHeaders->has('Content-type') ? $zendRequestHeaders->get('Content-type')->getMediaType() === 'multipart/form-data' : false; if ($request->getMethod() === 'POST' && $isMultiPart === true) { $postAndFileData = \array_merge_recursive( $zendRequest->getPost()->toArray(), $zendRequest->getFiles()->toArray() ); $session->set('post_and_file_data', $postAndFileData); $form->setData($postAndFileData); if ($form->isValid()) { $session->set('form_data', $form->getData()); } if ($messages = $form->getMessages()) { $session->set('form_errors', $messages); } return new RedirectResponse($request->getUri(), 303); }
On next flow, we can check if the session has “post_and_file_data” key, set form error messages when session has “form_errors” key, and return Response.
if ($session->has('post_and_file_data')) { $form->setData($session->get('post_and_file_data')); $session->unset('post_and_file_data'); if ($session->has('form_errors')) { $form->setMessages($session->get('form_errors')); $session->unset('form_errors'); } return new Response(); }
Above, we didn’t use the “form_data” session as the form data may be used in the page handler, so, we can handle it with keep the “form_data” as “form->getData()” result once by return Response above, and remove it when it already hit in next refresh, so next flow can be:
if ($session->has('form_data')) { $session->unset('form_data'); } return new Response();
The complete middleware class can be as follow:
<?php // src/App/Middleware/FilePrgMiddleware.php declare(strict_types=1); namespace App\Middleware; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Server\MiddlewareInterface; use Psr\Http\Server\RequestHandlerInterface; use Zend\Diactoros\Response; use Zend\Diactoros\Response\RedirectResponse; use Zend\Expressive\Session\SessionMiddleware; use Zend\Psr7Bridge\Psr7ServerRequest; class FilePrgMiddleware implements MiddlewareInterface { public function process( ServerRequestInterface $request, RequestHandlerInterface $handler ) : ResponseInterface { $session = $request->getAttribute(SessionMiddleware::SESSION_ATTRIBUTE); $zendRequest = Psr7ServerRequest::toZend($request); $zendRequestHeaders = $zendRequest->getHeaders(); $isMultiPart = $zendRequestHeaders->has('Content-type') ? $zendRequestHeaders->get('Content-type')->getMediaType() === 'multipart/form-data' : false; $form = $request->getAttribute('form'); if ($request->getMethod() === 'POST' && $isMultiPart === true) { $postAndFileData = \array_merge_recursive( $zendRequest->getPost()->toArray(), $zendRequest->getFiles()->toArray() ); $session->set('post_and_file_data', $postAndFileData); $form->setData($postAndFileData); if ($form->isValid()) { $session->set('form_data', $form->getData()); } if ($messages = $form->getMessages()) { $session->set('form_errors', $messages); } return new RedirectResponse($request->getUri(), 303); } if ($session->has('post_and_file_data')) { $form->setData($session->get('post_and_file_data')); $session->unset('post_and_file_data'); if ($session->has('form_errors')) { $form->setMessages($session->get('form_errors')); $session->unset('form_errors'); } return new Response(); } if ($session->has('form_data')) { $session->unset('form_data'); } return new Response(); } }
FilePrgMiddleware Service Registration
We can register the FilePrgMiddleware at src/App/ConfigProvider under getDependencies() function:
// src/App/ConfigProvider.php use Zend\ServiceManager\Factory\InvokableFactory; // ... public function getDependencies() : array { return [ 'factories' => [ // ... Middleware\FilePrgMiddleware::class => InvokableFactory::class, ], ]; }
The Upload Form and Its Page
Time for usage. First, we can create an upload form, for example like below:
<?php namespace App\Form; use Zend\Form\Element\File; use Zend\Form\Element\Submit; use Zend\Form\Form; use Zend\InputFilter\InputFilterProviderInterface; use Zend\Validator\File\MimeType; use Zend\Validator\File\Size; use Zend\Filter\File\RenameUpload; class UploadForm extends Form implements InputFilterProviderInterface { public function __construct() { parent::__construct('upload-form'); $this->init(); } public function init() { $this->add([ 'type' => File::class, 'name' => 'filename', 'options' => [ 'label' => 'File upload', ], ]); $this->add([ 'name' => 'submit', 'type' => Submit::class, 'attributes' => [ 'value' => 'Submit', ], ]); } public function getInputFilterSpecification() { return [ [ 'name' => 'filename', 'required' => true, 'filters' => [ [ 'name' => RenameUpload::class, 'options' => [ 'target' => \getcwd() . '/public/uploads', 'use_upload_extension' => true, 'use_upload_name' => true, 'overwrite' => true, 'randomize' => false, ], ], ], 'validators' => [ [ 'name' => Size::class, 'options' => [ 'max' => '10MB', ], ], [ 'name' => MimeType::class, 'options' => [ 'mimeType' => [ 'image/jpg', 'image/jpeg', 'image/png', ], ] ] ], ], ]; } }
To ensure the file upload correctly, I applied filter RenameUpload
to move it to public/uploads
directory. We can create an uploads directory by run command:
$ mkdir -p public/uploads && chmod 755 public/uploads
Next, we can create an Upload Page Handler :
<?php declare(strict_types=1); namespace App\Handler; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Server\MiddlewareInterface; use Psr\Http\Server\RequestHandlerInterface; use Zend\Diactoros\Response\HtmlResponse; use Zend\Expressive\Template; class UploadPageHandler implements MiddlewareInterface { private $template; public function __construct(Template\TemplateRendererInterface $template) { $this->template = $template; } public function process( ServerRequestInterface $request, RequestHandlerInterface $handler ) : ResponseInterface { $data = []; return new HtmlResponse($this->template->render('app::upload-page', $data)); } }
We need template in templates/app/upload-page.phtml
for it. For view, we can create a file:
$ touch templates/app/upload-page.phtml
and write with the form helper:
<?php // templates/app/upload-page.phtml echo $this->form($form);
As above UploadPageHandler require an TemplateRendererInterface
service, we need factory for it, as follow:
<?php declare(strict_types=1); namespace App\Handler; use Psr\Container\ContainerInterface; use Psr\Http\Server\MiddlewareInterface; use Zend\Expressive\Template\TemplateRendererInterface; class UploadPageHandlerFactory { public function __invoke(ContainerInterface $container) : MiddlewareInterface { $template = $container->get(TemplateRendererInterface::class); return new UploadPageHandler($template); } }
We can register the UploadPageHandler at src/App/ConfigProvider under getDependencies() function:
// src/App/ConfigProvider.php use Zend\ServiceManager\Factory\InvokableFactory; // ... public function getDependencies() : array { return [ 'factories' => [ // ... Handler\UploadPageHandler::class => Handler\UploadPageHandlerFactory::class, // ... ], ]; }
To make it accessible, we need to register its routing, for example, at config/routes.php
:
return function (Application $app, MiddlewareFactory $factory, ContainerInterface $container) : void { // ... $app->route( '/upload', [ App\Handler\UploadPageHandler::class, App\Middleware\FilePrgMiddleware::class, ], [ 'GET', 'POST' ], 'upload' ); // ... };
So, we will get the following display when access “/upload” page:
Using The FilePrg in UploadPageHandler
We can set the Zend\Form\Form
instance into request attribute and call it handler, on form is valid, we can apply after its response check not a RedirectResponse
.
use App\Form\UploadForm; use Zend\Expressive\Session\SessionMiddleware; // ... public function process( ServerRequestInterface $request, RequestHandlerInterface $handler ) : ResponseInterface { $form = new UploadForm(); $request = $request->withAttribute('form', $form); $response = $handler->handle($request); if ($response instanceof RedirectResponse) { return $response; } $session = $request->getAttribute(SessionMiddleware::SESSION_ATTRIBUTE); if ($session->has('form_data')) { $formData = $session->get('form_data'); // we can returns RedirectResponse to "success upload" page, // process form data, // set view variable to allow display that the upload success, // etc } $data = ['form' => $form]; return new HtmlResponse($this->template->render('app::upload-page', $data)); } // ...
When we get invalid input, we will get the following form page like below:
When we refresh it, it will normal refresh and won’t call a duplicate form submission.
That’s it ;).
Introduce IsDeprecated: PHP7+ Helper for E_USER_DEPRECATED and E_DEPRECATED Detection
Before I continue, allow me to give you my suggestions:
Live with deprecated function is bad, you may can’t upgrade to newer PHP version or newer library as the function that was deprecated already removed in next major/minor version.
Use same environment or at least same major and minor version between local dev and production environment when possible, your life will be easier.
When reality is not always what you want
You may found a a reality when you work at different version of PHP version or library that rely on specific function which already deprecated in some environment, the IsDeprecated may be solution at that time that can verify E_USER_DEPRECATED
or E_DEPRECATED
trigger error. It utilize jeremeamia/FunctionParser for user defined function check, and zendframework/zend-stdlib’s ErrorHandler for E_DEPRECATED
function check.
When you found the passed function is deprecated, you can use alternative function.
Installation
This helper can be installed via composer:
composer require samsonasik/is-deprecated
This helper have features:
1. Detect E_USER_DEPRECATED
- At independent function
- At function inside class
You can use IsDeprecated\isDeprecatedUser
function with signature:
/** * @param string|array $function the "functionName" or ["ClassName" or object, "functionName"] or "ClassName::functionName" * @throws InvalidArgumentException when trigger_error found but the error is not E_USER_DEPRECATED * @throws InvalidArgumentException when trigger_error and E_USER_DEPRECATED found but misplaced * @return bool */ function isDeprecatedUser($function): bool
Note: when trigger_error E_USER_DEPRECATED inside condition, you need to use
actual
call with signature:
/** * @param callable $function callable function * @return bool */ function isDeprecatedWithActualCall(callable $function)
1a. Independent Function
The usage is like the following:
use function IsDeprecated\isDeprecatedUser; function foo() { trigger_error('this method has been deprecated.', E_USER_DEPRECATED); } if (isDeprecatedUser('foo')) { // apply alternative/new function to call... } else { foo(); }
1.b Function Inside Class
The usage is like the following:
use function IsDeprecated\isDeprecatedUser; class AClass { public function foo() { trigger_error('this method has been deprecated.', E_USER_DEPRECATED); } // check inside with $this public function execute() { if (isDeprecatedUser([$this, 'foo'])) { // apply alternative/new function to call... return; } $this->foo(); } } // you may call after instantiation $object = new \AClass(); if (isDeprecatedUser([$object, 'foo'])) { // apply alternative/new function to call... } else { $object->foo(); } // or if (isDeprecatedUser(['AClass', 'foo'])) { // apply alternative/new function to call... } else { (new \AClass())->foo(); }
2. Detect E_DEPRECATED
E_DEPRECATED can be triggered on Core PHP function call.
You can use IsDeprecated\isDeprecatedCore
function with signature:
/** * @param callable $function callable function * @return bool */ function isDeprecatedCore(callable $function): bool
The usage is like the following:
use function IsDeprecated\isDeprecatedCore; $function = function () { mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC); }; if (isDeprecatedCore($function)) { // alternative function, eg: openssl ... } else { $function(); }
Limitation
For Core PHP Functions or user function with condition (T_IF or T_SWITCH token), the function passed actually need to be called. It ensure that we don’t get error during call deprecated function, and we can use alternative function if the isDeprecatedCore()
returns true with call of isDeprecatedWithActualCall
.
You want to use it? You can check my repository https://github.com/samsonasik/IsDeprecated
That’s it ;).
ErrorHeroModule : a Hero for Your Zend Mvc and Expressive Application
After > 1 year work with 52 releases, I think it is time to show off. Even you have 100% test coverage, error may can still happen, that’s why ErrorHeroModule was born. ErrorHeroModule is a Hero for your Zend Mvc, and zend-expressive application to trap php errors and exception with configureable options.
The logging storage is mainly a database, then can continue “log” to your email when you want it.
Features
1. Save to DB with Db Writer Adapter
We can choose using Zend\Db
or Doctrine
via DoctrineORMModule
. The error log is recorded like below:
2. Log Exception (dispatch.error and render.error) and PHP Errors in all events process
This handle all Exceptions and Errors with support PHP 7 Error during MVC process or middleware flow.
3. Support excludes PHP E_* Error (eg: exclude E_USER_DEPRECATED) in config settings
This can be used when you have a functionality which has collection of E_* errors, and you need to keep the functionality to run.
4. Support excludes PHP Exception (eg: Exception class or classes that extends it) in config settings
This can be used when you have exceptions that you want to have special treatment.
5. Handle only once log error for same error per configured time range
This can be used when on some environment, eg: in production, we don’t want to get same error repeatly reported in some periodic time while we are fixing it.
6. Set default page (web access) or default message (console access) for error if configured ‘display_errors’ = 0
This can be used to set a “nice” page on web environment:
or content on console access:
7. Set default content when request is XMLHttpRequest via ‘ajax’ configuration
This can be used to set a default content when request is an XMLHttpRequest.
8. Provide request information ( http method, raw data, query data, files data, and cookie data )
This can be used to help reproduce the error.
9. Send Mail
This has options:
– many receivers to listed configured email
– with include $_FILES into attachments on upload error.
This can be used to help reproduce the error, with include uploaded data when error happen when we just submitted a form with upload process.
Support
This module support zend-mvc:^2.5 and zend-expressive:^1.1|^2.0 with php version ^5.6|^7.0. My plan is to drop php ^5.6 in version 2.
Limitations
There are some limitations right now and I want it to be implemented in next releases:
General functionality:
- Allow custom formatter when log to email, currently, it send Json format to email.
Current Json Formatter is really stable with the following format sample data:
{ "timestamp": "2017-12-20T15:23:00+07:00", "priority": 3, "priorityName": "ERR", "message": "a sample error preview", "extra": { "url": "http://app.dev/error-preview", "file": "/var/www/app/vendor/samsonasik/error-hero-module/src/Controller/ErrorPreviewController.php", "line": 11, "error_type": "Exception", "trace": "#0 /var/www/app/vendor/zendframework/zend-mvc/src/Controller/AbstractActionController.php(78): ErrorHeroModule\\Controller\\ErrorPreviewController->exceptionAction() #1 /var/www/app/vendor/zendframework/zend-eventmanager/src/EventManager.php(322): Zend\\Mvc\\Controller\\AbstractActionController->onDispatch(Object(Zend\\Mvc\\MvcEvent)) #2 /var/www/app/vendor/zendframework/zend-eventmanager/src/EventManager.php(179): Zend\\EventManager\\EventManager->triggerListeners(Object(Zend\\Mvc\\MvcEvent), Object(Closure)) #3 /var/www/app/vendor/zendframework/zend-mvc/src/Controller/AbstractController.php(106): Zend\\EventManager\\EventManager->triggerEventUntil(Object(Closure), Object(Zend\\Mvc\\MvcEvent)) #4 /var/www/app/vendor/zendframework/zend-mvc/src/DispatchListener.php(138): Zend\\Mvc\\Controller\\AbstractController->dispatch(Object(Zend\\Http\\PhpEnvironment\\Request), Object(Zend\\Http\\PhpEnvironment\\Response)) #5 /var/www/app/vendor/zendframework/zend-eventmanager/src/EventManager.php(322): Zend\\Mvc\\DispatchListener->onDispatch(Object(Zend\\Mvc\\MvcEvent)) #6 /var/www/app/vendor/zendframework/zend-eventmanager/src/EventManager.php(179): Zend\\EventManager\\EventManager->triggerListeners(Object(Zend\\Mvc\\MvcEvent), Object(Closure)) #7 /var/www/app/vendor/zendframework/zend-mvc/src/Application.php(332): Zend\\EventManager\\EventManager->triggerEventUntil(Object(Closure), Object(Zend\\Mvc\\MvcEvent)) #8 /var/www/app/public/index.php(53): Zend\\Mvc\\Application->run() #9 {main}", "request_data": { "query": [], "request_method": "GET", "body_data": [], "raw_data": "", "files_data": [], "cookie_data": { "ZS6SESSID": "pbihc9ts004oq4b5alg4tg91b6", "PHPSESSID": "bkd7jaj22z936vstc9l0xuc9sr2dqp4g", } } } }
The drawback with allow custom formatter is you maintain/keep an eye yourself for the formatter you provide!
Zend Mvc application:
- Trap exception and error when they happen at
Module::init()
.
Zend Expressive application:
Make support for non zend-servicemanager for container.(support zend-servicemanager, symfony, aura, auryn, and pimple for zend-expressive application at version 2.9.0)Make support for non zend-view for custom page template engine when error happen.(supported at version 2.1.0)
That’s it for now. If you see something can be improved, please contribute! Thank you for all users that using it.
Testing Hard Dependency with AspectMock
This is another testing legacy application post. Don’t tell your client/other people to refactor, if your job is only to make tests for it, as at some situations, there are reasons to not refactor. You may have situation to test hard dependency that impossible to be mocked and stubbed. There is a library named AspectMock for it, that you can use in PHPUnit, for example.
So, to have it, you can require via composer:
composer require "codeception/aspect-mock:^0.5.5" --dev
For example, you have the following class:
namespace App; class MyController { public function save() { $user = new User(); if (! $user->save()) { echo 'not saved'; return; } echo 'saved'; } }
That bad, huh! Ok, let’s deal to tests it even you don’t really like it. First, setup your phpunit.xml to have ‘backupGlobals=”false”‘ config:
<?xml version="1.0" encoding="UTF-8"?> <phpunit colors="true" backupGlobals="false" bootstrap="bootstrap.php"> <testsuites> <testsuite name="AppTest"> <directory suffix=".php">./test</directory> </testsuite> </testsuites> <filter> <whitelist addUncoveredFilesFromWhitelist="true"> <directory suffix=".php">./src</directory> </whitelist> </filter> </phpunit>
Now, based on config above, you can create bootstrap.php
:
include 'vendor/autoload.php'; use AspectMock\Kernel; $kernel = Kernel::getInstance(); $kernel->init([ 'debug' => true, 'cacheDir' => __DIR__ . '/data/cache', 'includePaths' => [__DIR__.'/src'], ]);
Assumption: You have ‘./data/cache’ for saving cache and ‘src/’ for your source code directory, if you use your own autoloader, you can add:
// ... $kernel->loadFile('YourAutoloader.php');
as the AspectMock documentation mentioned.
Now, time to write the tests:
- Preparation
namespace AppTest; use PHPUnit_Framework_TestCase; use App\MyController; use AspectMock\Test as test; class MyControllerTest extends PHPUnit_Framework_TestCase { private $myController; protected function setUp() { $this->myController = new MyController; } protected function tearDown() { test::clean(); // remove all registered test doubles } }
-
write the test cases
class MyControllerTest extends PHPUnit_Framework_TestCase { // ... public function provideSave() { return [ [true, 'saved'], [false, 'not saved'], ]; } /** * @dataProvider provideSave */ public function testSave($succeed, $echoed) { // mock $userMock = test::double('App\User', ['save' => $succeed]); ob_start(); $this->myController->save(); $content = ob_get_clean(); $this->assertEquals($echoed, $content); // stub $userMock->verifyInvoked('save'); } // ... }
Done 😉
references:
– https://github.com/Codeception/AspectMock
– https://twitter.com/grmpyprogrammer/status/642847787713884160
– https://littlehart.net/atthekeyboard/2014/12/14/stop-telling-me-to-refactor/
PHPUnit: Testing Closure passed to Collaborator
Yes! Closure is callable, so you can just call __invoke()
to the closure returned when test it! This is happen when we, for example, have a class and function that have closure inside it like the following:
class Awesome { public function call($foo) { return function() use ($foo) { return $foo; }; } }
This can be tested with:
use Awesome; use PHPUnit_Framework_TestCase; class AwesomeTest extends PHPUnit_Framework_TestCase { protected function setUp() { $this->awesome = new Awesome(); } public function testCall() { $foo = 'foo'; $call = $this->awesome->call($foo); $this->assertTrue(is_callable($call)); $invoked = $call->__invoke(); $this->assertEquals($foo, $invoked); } }
We need an __invoke()
call, as the closure never executed before it invoked. So, we need to call that.
On Collaborator Case
The problem is when we have a collaborator, and closure is processed inside the collaborator:
class Awesome { private $awesomeDependency; public function __construct(AwesomeDependency $awesomeDependency) { $this->awesomeDependency = $awesomeDependency; } public function call($foo) { $closure = function() use ($foo) { return $foo; }; return $this->awesomeDependency->call($closure); } }
and the closure inside call only executed in the AwesomeDependency
class:
class AwesomeDependency { public function call($call) { return $call(); } }
Our test can be like the following:
use Awesome; use AwesomeDependency; use PHPUnit_Framework_TestCase; class AwesomeTest extends PHPUnit_Framework_TestCase { protected function setUp() { $this->awesomeDependency = $this->prophesize(AwesomeDependency::class); $this->awesome = new Awesome($this->awesomeDependency->reveal()); } public function testCall() { $foo = 'foo'; $closure = function() use ($foo) { return $foo; }; $this->awesomeDependency ->call($closure) ->will(function() use ($closure) { return $closure->__invoke(); }) ->shouldBeCalled(); $call = $this->awesome->call($foo); } }
As we can see, the $this->awesomeDependency
is act as a mock, and calling __invoke()
in will()
is represent a $closure
that already passed to the mock, not the original $closure
one, and we will get partial coverage:
We know now, it won’t coverable as completed! What we can do? A refactor! But wait, it may be a legacy code, an aggressive refactor may cause problem, so a little but works patch may work for it.
- Make a
$closure
as class property, and add mutator and accessor for it.
class Awesome { private $closure; // ... public function setClosure($closure) { $this->closure = $closure; } public function getClosure() { return $this->closure; } // ... }
- Set
$closure
property when callcall()
function:
class Awesome { // ... public function call($foo) { $this->setClosure(function() use ($foo) { return $foo; }); return $this->awesomeDependency->call($this->getClosure()); } }
- And in tests, we can now has:
class AwesomeTest extends PHPUnit_Framework_TestCase { // ... public function testCall() { $foo = 'foo'; $closure = function() use ($foo) { return $foo; }; $awesome = $this->awesome; $this->awesomeDependency ->call($closure) ->will(function() use ($awesome) { return $awesome->getClosure()->__invoke(); }) ->shouldBeCalled(); $call = $this->awesome->call($foo); $this->assertEquals($foo, $call); } }
Need a better way? We can replace a closure with an array callback, so, we add additional function that called via call_user_func_array()
:
class Awesome { public function call($foo) { return $this->awesomeDependency->call(call_user_func_array( [$this, 'onFoo'], [$foo] )); } public function onFoo($foo) { return function() use ($foo) { return $foo; }; } }
And in our tests, we can do:
public function testCall() { $foo = 'foo'; $closure = function() use ($foo) { return $foo; }; $awesome = $this->awesome; $this->awesomeDependency->call($closure) ->will(function() use ($awesome, $foo) { return $awesome->onFoo($foo)->__invoke(); }) ->shouldBeCalled(); $call = $this->awesome->call($foo); $this->assertEquals($foo, $call); }
And, we now have a fully coverage too:
Re-fill selectize js value
It’s been a while since I didn’t write a post about non-framework category. Ok, this time, I will show you how to use selectize js on re-fill functionality. Selectize js is a jQuery plugin that useful for tagging and autocomplete. I used it in several projects. Once it’s installed and selectize()
called, your form can be like the following :
In images demo above, I want to re-set the “district” based on the “province” changes by javascript. To make selectize still applied to “district”, you need to do :
- re-set Html option values
- re-set selectize value options
Ok, let’s do a demo application for this.
1. Preparation
1.a make bower.json
for dependency requirements definition
{ "name":"Selectize Demo", "dependencies": { "jquery": "1.11.1", "selectize":"0.11.2" } }
1.b make .bowerrc
for specification
{ "directory": "js", "json": "bower.json" }
1.c install dependencies
bower install
2. Initialize selectize
We can initialize selectize js by include it in the header ( js and css ) like this :
<link href="./js/selectize/dist/css/selectize.default.css" media="screen" rel="stylesheet" type="text/css"> <script type="text/javascript" src="./js/jquery/dist/jquery.min.js"></script> <script type="text/javascript" src="./js/selectize/dist/js/standalone/selectize.min.js"></script>
and then, we create the elements which we want to selectize :
<form method="post"> <select name="province_id" id="province_id"> <option value="0">--Select Province--</option> <option value="1">Jawa Barat</option> <option value="2">Jawa Tengah</option> </select> <select name="district" id="district"> <option value="0">--Select District--</option> </select> </form>
Now, time to execute :
$(document).ready(function() { //initialize selectize for both fields $("#province_id").selectize(); $("#district").selectize(); });
3. Do the awesome
Ok, now what ? We need to re-fill the “district” data on change of “province”, In this case, I wrote a case when using Ajax request and catched by PHP script. So, create a “change-data.php” file :
<?php if (isset($_POST['province_id'])) { $data = []; if ($_POST['province_id'] == 1) { $data = [ 0 => [ 'id' => 1, 'name' => 'Bandung', ], 1 => [ 'id' => 2, 'name' => 'Cimahi', ] ]; } if ($_POST['province_id'] == 2) { $data = [ 0 => [ 'id' => 3, 'name' => 'Kudus', ], 1 => [ 'id' => 4, 'name' => 'Cirebon', ] ]; } echo json_encode($data); }
Basically, the selectize can be filled by json object that have “text” and “value” key, like the following :
[ {text: "Bandung", value: 1 }, {text: "Cimahi", value: 2 } ]
So, we need to get the data, and convert to json object, we can do with eval
:
new_value_options = eval('(' + new_value_options + ')');
Ok, now, let’s do this :
$(document).ready(function() { //initialize selectize for both fields $("#province_id").selectize(); $("#district").selectize(); // onchange $("#province_id").change(function() { $.post('./change-data', { 'province_id' : $(this).val() } , function(jsondata) { var htmldata = ''; var new_value_options = '['; for (var key in jsondata) { htmldata += '<option value="'+jsondata[key].id+'">'+jsondata[key].name+'</option>'; var keyPlus = parseInt(key) + 1; if (keyPlus == jsondata.length) { new_value_options += '{text: "'+jsondata[key].name+'", value: '+jsondata[key].id+'}'; } else { new_value_options += '{text: "'+jsondata[key].name+'", value: '+jsondata[key].id+'},'; } } new_value_options += ']'; //convert to json object new_value_options = eval('(' + new_value_options + ')'); if (new_value_options[0] != undefined) { // re-fill html select option field $("#district").html(htmldata); // re-fill/set the selectize values var selectize = $("#district")[0].selectize; selectize.clear(); selectize.clearOptions(); selectize.renderCache['option'] = {}; selectize.renderCache['item'] = {}; selectize.addOption(new_value_options); selectize.setValue(new_value_options[0].value); } }, 'json'); }); });
That’s it, hope it helpful. Want to grab the code ? grab it from https://github.com/samsonasik/selectize-demo
Zend Framework 2 : Reset HeadTitle Position from View
Sometime, we need to reset position of title that default we can look at ZendSkeletonApplication like “My Album – ZF2 Skeleton Application” to something like “ZF2 Skeleton Application – My Album”.
We CAN NOT do this from view :
$this->headTitle($title, 'PREPEND'); //OR $this->headTitle($title, 'APPEND');
because of the layout rendered after view rendered, so we need to pass a value from view to layout via placeholder. So after set a title at view, we need to pass a value via placeholder view helper.
// module/Album/view/album/album/index.phtml: $title = 'My albums'; $this->headTitle($title); $this->placeholder('titleType')->set('PREPEND');
so we can set the title type at layout like the following :
// module/Application/view/layout/layout.phtml: echo $this->headTitle('ZF2 Skeleton Application', $this->placeholder('titleType', 'APPEND')) ->setSeparator(' - ')->setAutoEscape(false);
$this->placeholder(‘titleType’, ‘APPEND’) that passed at 2nd parameter of headTitle() means that if titleType already set, so get from the already data, if no, set to ‘APPEND’ as default value.
When situation need to remove layout title ( parameter is ‘SET’), and use the view title, we need to make a conditional like this :
// module/Application/view/layout/layout.phtml: if ($this->placeholder('titleType', 'APPEND') == 'SET') { echo $this->headTitle()->setAutoEscape(false); } else { echo $this->headTitle('ZF2 Skeleton Application', $this->placeholder('titleType', 'APPEND')) ->setSeparator(' - ')->setAutoEscape(false); }
That’s it 😉
references:
1. http://stackoverflow.com/questions/13949809/zend-framework-2-make-content-page-variable-accessable-in-layout-phtml
2. http://zf2.readthedocs.org/en/latest/modules/zend.view.helpers.placeholder.html#zend-view-helpers-initial-placeholder
Zend Framework 2 : Generate Doctrine Entities from Existing Database using DoctrineModule and DoctrineORMModule
If we are working with Doctrine , we usually create entities first, and generate the database tables. How if the situation is the database tables and data is already ready, and we have to create an application based on doctrine and Zend Framework 2? We need generate entities! Don’t create them manually!
For example, i have two tables : album and track (i’m using PostgreSQL ) like the following :
-- Table: album CREATE TABLE album ( id bigserial NOT NULL, artist character varying(255), title character varying(255), CONSTRAINT pk_album PRIMARY KEY (id ) ) WITH ( OIDS=FALSE ); ALTER TABLE album OWNER TO developer; -- Table: track CREATE TABLE track ( track_id bigserial NOT NULL, track_title character varying(255), album_id bigint, CONSTRAINT track_pkey PRIMARY KEY (track_id ), CONSTRAINT fk_track_album FOREIGN KEY (album_id) REFERENCES album (id) MATCH SIMPLE ON UPDATE CASCADE ON DELETE CASCADE ) WITH ( OIDS=FALSE ); ALTER TABLE track OWNER TO developer;
Ok, let’s create an application based on ZF2 step by step :
1. install ZF2 SkeletonApplication
git clone git://github.com/zendframework/ZendSkeletonApplication.git zftutordoctrine
2. add “doctrine/doctrine-orm-module” to your zftutordoctrine/composer.json
{ "name": "zendframework/skeleton-application", "description": "Skeleton Application for ZF2", "license": "BSD-3-Clause", "keywords": [ "framework", "zf2" ], "homepage": "http://framework.zend.com/", "require": { "php": ">=5.3.3", "zendframework/zendframework": ">2.1.3", "doctrine/doctrine-orm-module": "0.*" } }
3. Install it
php composer.phar self-update && php composer.phar install
4. Create an Album module with structure entity like the following :
5. Configure doctrine connection
You can define it at one file, but separate it with two file(local and global) can make security happy 😀
a. config/autoload/doctrine.global.php
//config/autoload/doctrine.global.php return array( 'doctrine' => array( 'connection' => array( 'orm_default' => array( 'driverClass' => 'Doctrine\DBAL\Driver\PDOMySql\Driver', 'params' => array( 'host' => 'localhost', 'port' => '5432', 'dbname' => 'zftutordoctrine', ), ), ) ));
b. config/autoload/doctrine.local.php
//config/autoload/doctrine.local.php return array( 'doctrine' => array( 'connection' => array( 'orm_default' => array( 'driverClass' => 'Doctrine\DBAL\Driver\PDOPgSql\Driver', 'params' => array( 'user' => 'developer', 'password' => '123456', ), ), ) ));
6. register Album\Entity into doctrine driver in module/Album/config/module.config.php
//module/Album/config/module.config.php return array( 'doctrine' => array( 'driver' => array( 'Album_driver' => array( 'class' => 'Doctrine\ORM\Mapping\Driver\AnnotationDriver', 'cache' => 'array', 'paths' => array(__DIR__ . '/../src/Album/Entity') ), 'orm_default' => array( 'drivers' => array( 'Album\Entity' => 'Album_driver' ), ), ), ), );
7. Register modules into config/application.config.php
//config/application.config.php return array( 'modules' => array( 'Application', 'DoctrineModule', 'DoctrineORMModule', 'Album' ), // These are various options for the listeners attached to the ModuleManager 'module_listener_options' => array( 'module_paths' => array( './module', './vendor', ), 'config_glob_paths' => array( 'config/autoload/{,*.}{global,local}.php', ), ), );
8. Generate Time !
a. convert-mapping
./vendor/doctrine/doctrine-module/bin/doctrine-module orm:convert-mapping --namespace="Album\\Entity\\" --force --from-database annotation ./module/Album/src/
it will export “annotation” mapping information into ./module/Album/src/
b. generate-entities
./vendor/doctrine/doctrine-module/bin/doctrine-module orm:generate-entities ./module/Album/src/ --generate-annotations=true
it will add setter/getter into entities.
and you will get the following entities AUTOMATICALLY :
Album\Entity\Album
<?php namespace Album\Entity; use Doctrine\ORM\Mapping as ORM; /** * Album * * @ORM\Table(name="album") * @ORM\Entity */ class Album { /** * @var integer * * @ORM\Column(name="id", type="bigint", nullable=false) * @ORM\Id * @ORM\GeneratedValue(strategy="SEQUENCE") * @ORM\SequenceGenerator(sequenceName="album_id_seq", allocationSize=1, initialValue=1) */ private $id; /** * @var string * * @ORM\Column(name="artist", type="string", length=255, nullable=true) */ private $artist; /** * @var string * * @ORM\Column(name="title", type="string", length=255, nullable=true) */ private $title; /** * Get id * * @return integer */ public function getId() { return $this->id; } /** * Set artist * * @param string $artist * @return Album */ public function setArtist($artist) { $this->artist = $artist; return $this; } /** * Get artist * * @return string */ public function getArtist() { return $this->artist; } /** * Set title * * @param string $title * @return Album */ public function setTitle($title) { $this->title = $title; return $this; } /** * Get title * * @return string */ public function getTitle() { return $this->title; } }
Album\Entity\Track
<?php namespace Album\Entity; use Doctrine\ORM\Mapping as ORM; /** * Track * * @ORM\Table(name="track") * @ORM\Entity */ class Track { /** * @var integer * * @ORM\Column(name="track_id", type="bigint", nullable=false) * @ORM\Id * @ORM\GeneratedValue(strategy="SEQUENCE") * @ORM\SequenceGenerator(sequenceName="track_track_id_seq", allocationSize=1, initialValue=1) */ private $trackId; /** * @var string * * @ORM\Column(name="track_title", type="string", length=255, nullable=true) */ private $trackTitle; /** * @var \Album\Entity\Album * * @ORM\ManyToOne(targetEntity="Album\Entity\Album") * @ORM\JoinColumns({ * @ORM\JoinColumn(name="album_id", referencedColumnName="id") * }) */ private $album; /** * Get trackId * * @return integer */ public function getTrackId() { return $this->trackId; } /** * Set trackTitle * * @param string $trackTitle * @return Track */ public function setTrackTitle($trackTitle) { $this->trackTitle = $trackTitle; return $this; } /** * Get trackTitle * * @return string */ public function getTrackTitle() { return $this->trackTitle; } /** * Set album * * @param \Album\Entity\Album $album * @return Track */ public function setAlbum(\Album\Entity\Album $album = null) { $this->album = $album; return $this; } /** * Get album * * @return \Album\Entity\Album */ public function getAlbum() { return $this->album; } }
JUST IMAGINE that YOU SHOULD WRITE THEM MANUALLY :p
9. Fill database tables rows, and Let’s call from whatever controller for testing :
public function indexAction() { $em = $this->getServiceLocator() ->get('doctrine.entitymanager.orm_default'); $data = $em->getRepository('Album\Entity\Track')->findAll(); foreach($data as $key=>$row) { echo $row->getAlbum()->getArtist().' :: '.$row->getTrackTitle(); echo '<br />'; } }
References :
1. Conversation with Anass Ans
1. http://docs.doctrine-project.org/en/2.0.x/reference/tools.html
2. http://wildlyinaccurate.com/useful-doctrine-2-console-commands
Practical GIT (3) : Remove Specific commits
If you’are working with pilot project that many feature are experiment conditions, you need this feature, You can remove commits that you don’t want to exist in your revisions log. This feature named rebase. You can find the whole commits lists on range by typing :
git rebase -i HEAD~5
that command will show you last 5 commit and show them in the editor like the following :
pick 5c22314 third commit pick 8de60b5 fourth commit pick 9d3556f fifth commit pick 434ddc3 sixth commit pick d9b917g seventh commit # Rebase d9b917f..8de60b5 onto d9b917f # # Commands: # p, pick = use commit # r, reword = use commit, but edit the commit message # e, edit = use commit, but stop for amending # s, squash = use commit, but meld into previous commit # f, fixup = like "squash", but discard this commit's log message # x, exec = run command (the rest of the line) using shell # # If you remove a line here THAT COMMIT WILL BE LOST. # However, if you remove everything, the rebase will be aborted. # ~ ~
If you want to remove fifth and sixth commit, just remove these two lines and save the editor ( if you’re working with vim, type ‘dd’ in the line you want to delete, and type ‘:wq!’. After do that, check by typing command :
git log
and you will get the result like the following ( fifth and sixth commit deleted ! )
commit d9b917g9d3556cfb0d0bbcd8ecd952cf4f358eea Author: Abdul Malik Ikhsan <samsonasik@gmail.com> Date: Wed Feb 20 15:47:13 2013 +0700 seventh commit commit 8de60b59d3556cd8eeafb0d0bbcd8ec952cf4f35 Author: Abdul Malik Ikhsan <samsonasik@gmail.com> Date: Wed Feb 20 15:47:13 2013 +0700 fourth commit commit 5c223149d3556cd952cf4f358eeafb0d0bbcd8ec Author: Abdul Malik Ikhsan <samsonasik@gmail.com> Date: Wed Feb 20 15:47:13 2013 +0700 third commit commit 5c2231434ddc3bac911ab38a22d2b47f5736f7d4 Author: Abdul Malik Ikhsan <samsonasik@gmail.com> Date: Wed Feb 20 15:46:50 2013 +0700 second commit commit d9b917f6a48eb9c0c415772f70b7c66adf3de0b5 Author: Abdul Malik Ikhsan <samsonasik@gmail.com> Date: Wed Feb 20 15:45:26 2013 +0700 first commit
With rebase, you can combine commits to/from other branch or squash commits with commands list while you get the list of commit via rebase -i command.
references :
1. http://www.kernel.org/pub/software/scm/git/docs/git-rebase.html
Rsync : Synchronize local with shared hosting
As web developer, We already realize that we are lazy. Our client is often only have shared hosting, not dedicated server that we can install version control which simplify push and pull. We often forgot to upload some file we changed/added in local to shared hosting, and it make web is not run properly in shared hosting. The solution is to use rsync in local PC. Rsync is a file transfer program for Unix systems. rsync uses the ‘rsync algorithm’ which provides a very fast method for bringing remote files into sync. An important feature of rsync not found in most similar programs/protocols is that the mirroring takes place with only one transmission in each direction. Rsync can copy or display directory contents and copy files, optionally using compression and recursion.
So, you need to know the step to do this :
1. Go to your Cpanel
2. Click Ftp Account
3. Click Configure FTP Client in your desired user
And see the SFTP Port. Use the port to in rsync port target transfer.
4. Start syncing…
cd ~/www && rsync -timeout=50000 -av --exclude ".git" --exclude ".gitignore" --exclude "wp-config.php" wordpress_local_dev/ -e "ssh -p 2223" yourremoteserveruser@yourdomain.com:/home/yourremoteserveruser/public_html/
The option –exclude will exclude unneeded to sync.
The option -timeout is what second to be connection to timeout.
And you will see the following ( just updated / added file will be uploaded ) :
5. Done !
References :
Image : http://www.haykranen.nl/wp-content/uploads/2008/05/timemachine.jpg
Source :
1. http://mike-hostetler.com/blog/2007/12/08/rsync-non-standard-ssh-port/
2. http://en.wikipedia.org/wiki/Rsync
Zend Framework 2 ‘Cheat Sheet’ : Service Manager
The Service Locator design pattern is implemented by the ServiceManager. The Service Locator is a service/object locator, tasked with retrieving other objects.
Get the SM :
1. Inside Controller
$serviceLocator = $this->getServiceLocator();
2. Inside Module.php
namespace YourModule\Service; use Zend\Mvc\MvcEvent; class Module { public function onBootstrap(MvcEvent $e) { $sm = $e->getApplication()->getServiceManager(); } }
3. Inside Controller Plugin
$serviceLocator = $this->getController()->getServiceLocator();
Types of Services
The registered name of service is not case sensitive. There are the type of services :
a. invokables : an array of service name/class name pairs. The class name should be class that may be directly instantiated without any constructor arguments
for ex :
//YourModule/config/module.config.php return array( 'controllers'=>array( 'invokables' => array( 'SanUser\Controller\User' => 'SanUser\Controller\UserController' ), ), );
b. abstract_factories : Unknown Services ( The “Limbo” if ServiceManager failed to search in registered services)
//YourModule/src/YourModule/Service/CommonControlAppAbstractFactory.php namespace YourModule\Service; use Zend\ServiceManager\AbstractFactoryInterface; use Zend\ServiceManager\ServiceLocatorInterface; class CommonControlAppAbstractFactory implements AbstractFactoryInterface { public function canCreateServiceWithName(ServiceLocatorInterface $locator, $name, $requestedName) { if (class_exists($requestedName.'Controller')){ return true; } return false; } public function createServiceWithName(ServiceLocatorInterface $locator, $name, $requestedName) { $class = $requestedName.'Controller'; return new $class; } }
Then, register in SM :
//YourModule/config/module.config.php return array( 'controllers'=> array( 'abstract_factories' => array( 'YourModule\Service\CommonControlAppAbstractFactory', ), ); ), );
In this case, if SM could not find controllers in invokables, the SM will turn to it whenever canCreateServiceWithName return true; ( controllers is service that called automatically by mvc stack )
What if you want other service ? This is it :
namespace YourModule\Service; use Zend\ServiceManager\AbstractFactoryInterface; use Zend\ServiceManager\ServiceLocatorInterface; class CommonModelTableAbstractFactory implements AbstractFactoryInterface { public function canCreateServiceWithName(ServiceLocatorInterface $locator, $name, $requestedName) { return (substr($requestedName, -5) === 'Table'); } public function createServiceWithName(ServiceLocatorInterface $locator, $name, $requestedName) { $db = $locator->get('Zend\Db\Adapter\Adapter'); $tablemodel = new $requestedName; $tablemodel->setDbAdapter($db); return $tablemodel; } }
You want if you call un-registered table model, you automatically create service with this abstract factory whenever last 5 chars of the service called = ‘Table’. Register this abstract factory under service_manager key :
//YourModule/config/module.config.php return array( 'service_manager'=> array( 'abstract_factories' => array( 'YourModule\Service\CommonModelTableAbstractFactory', ), ); ), );
Note for abstract_factories : Being explicit is more secure and reliable. You should not forgot to register service you write into ServiceManager.
c. factories : an array of service name/factory class name pairs.
c.1. If you are using PHP configuration files, you may provide any PHP callable as the factory.
//YourModule/config/module.config.php return array( 'service_manager'=>array( 'factories' => array( 'MyTable' => function ($sm) { $db = $sm->get('Zend\Db\Adapter\DbAdapter'); $table = new \YourModule\Model\MyTableModel(); $table->setDbAdapter($db); }, ), ), );
c.2. by implementing Zend\ServiceManager\FactoryInterface by create a factory first :
namespace YourModule\Service; use Zend\ServiceManager\FactoryInterface; use Zend\ServiceManager\ServiceLocatorInterface; class MyTableFactory implements FactoryInterface { public function createService(ServiceLocatorInterface $serviceLocator) { $db = $serviceLocator->get('Zend\Db\Adapter\DbAdapter'); $table = new \YourModule\Model\MyTableModel(); $table->setDbAdapter($db); return $table; } }
And the factories registered just like the following :
//YourModule/config/module.config.php return array( 'service_manager'=>array( 'factories' => array( 'MyTable' => 'YourModule\Service\MyTableFactory' ), ), );
d. aliases : which should be an associative array of alias name/target name pairs (where the target name may also be an alias).
//YourModule/config/module.config.php return array( 'service_manager'=>array( 'factories' => array( 'MyTable' => 'YourModule\Service\MyTableFactory' ), 'aliases' => array( 'YourModule\Model\MyTable' => 'MyTable', ), ), );
e. shared :an array of service name/boolean pairs, indicating whether or not a service should be shared. By default, the ServiceManager assumes all services are shared, but you may specify a boolean false value here to indicate a new instance should be returned.
//YourModule/config/module.config.php return array( 'service_manager'=>array( 'factories' => array( 'MyTable' => 'YourModule\Service\MyTableFactory' ), 'shared' => array( // Usually, you'll only indicate services that should _NOT_ be // shared -- i.e., ones where you want a different instance // every time. 'MyTable' => false, ), ), );
f. services : an array of service name/object pairs. Clearly, this will only work with PHP configuration.
//YourModule/config/module.config.php return array( 'service_manager'=>array( 'services' => array( // Keys are the service names // Values are objects 'Auth' => new YourModule\Authentication\AuthenticationService(), ), ), );
g. initializers
It initialize the service whenever service created. It can reduce the redundance the injections to services.
//YourModule/config/module.config.php return array( 'service_manager'=>array( 'initializers' => array( function ($instance, $sm) { if ($instance instanceof \Zend\Db\Adapter\AdapterAwareInterface) { $instance->setDbAdapter($sm->get('Zend\Db\Adapter\Adapter')); } } ), ), );
And you should not to inject Adapter manually in the Table Class :
namespace YourModule\Model; use Zend\Db\TableGateway\AbstractTableGateway; use Zend\Db\Adapter\AdapterAwareInterface; use Zend\Db\Adapter\Adapter; class UserTable extends AbstractTableGateway implements AdapterAwareInterface { protected $table = 'zf2_users'; public function setDbAdapter(Adapter $adapter) { $this->adapter = $adapter; $this->initialize(); } }
Just invoke at service_manager :
//YourModule/config/module.config.php return array( 'service_manager'=>array( 'initializers' => array( function ($instance, $sm) { if ($instance instanceof \Zend\Db\Adapter\AdapterAwareInterface) { $instance->setDbAdapter($sm->get('Zend\Db\Adapter\Adapter')); } } ), 'invokables' => array( 'YourModule\Model\UserTable' => 'YourModule\Model\UserTable' ) ), );
h. allow_override
Override your existing Services.
//YourModule/config/module.config.php return array( 'service_manager'=>array( 'factories' => array( 'MyService' => 'YourModule\Service\MyServiceFactory' ), 'allow_override' => array( 'MyService' => true, ), ), );
The top level configuration keys
Manager | Key name in configuration array | Method name in Module.php |
---|---|---|
ServiceManager | service_manager | getServiceConfig() |
ViewHelperManager | view_helpers | getViewHelperConfig() |
ControllerPluginManager | controller_plugins | getControllerPluginConfig() |
ControllerLoader | controllers | getControllerConfig() |
Sample Code in Module.php :
class Module { //for 'service_manager' public function getServiceConfig() { return array( 'invokables' => array( /* see Types of Services */ ), 'factories' => array( /* see Types of Services */ ), 'abstract_factories' => array( /* see Types of Services */ ), 'aliases' => array( /* see Types of Services */ ), 'services' => array( /* see Types of Services */ ), 'initializers' => array( /* see Types of Services */ ), 'shared' => array(/* see Types of Services */), ); } //for 'controllers' -> it automatically composed by mvc stack //no need to call by your hand ( get('...') ); public function getControllerConfig() { return array( /* looks like above code */ ); } //for 'controller_plugins' public function getControllerPluginConfig() { return array( /* looks like above code */ ); } //for 'view_helpers' call in view by $this->nameViewHelperRegistered()->dosomething() public function getViewHelperConfig() { return array( /* looks like above code */ ); } }
For ZF 2.1 ( still in dev branch, the keys added with FormElementManager )
Manager | Key name in configuration array | Method name in Module.php |
---|---|---|
FormElementManager | form_elements | getFormElementConfig() |
For what have to do to create a Controller pLugin , see : empirio’s post about creating Controller pLugin in ZF2. For what have to do to create a View Helper , see : my post about creating view helper or EvanDotPro’s post.
Hope this post helpful ;). Happy new Year!
References :
1. http://zf2.readthedocs.org/en/latest/modules/zend.service-manager.intro.html
2. http://akrabat.com/zend-framework-2/zendservicemanager-configuration-keys/
3. http://juriansluiman.nl/en/article/120/using-zend-framework-service-managers-in-your-application
4. http://blog.evan.pro/creating-a-simple-view-helper-in-zend-framework-2
5. http://lab.empirio.no/custom-controller-plugin-in-zf2.html
Practical GIT (2) : Update remote file after push with configure post-receive hooks
GIT version control got one feature that I really like, named hooks. With hooks, we can configure what GIT do after metadata pushed to the remote server. It can ease our development because we don’t need to upload file manually, just say : git push remoteserver, so the file in our online web will be replaced.
leave a comment