Welcome to Abdul Malik Ikhsan's Blog

Rsync : Synchronize local with shared hosting

Posted in Teknologi, tips and tricks by samsonasik on January 26, 2013

rsyncAs 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
Screen Shot 2013-01-26 at 4.36.40 PM
3. Click Configure FTP Client in your desired user
Screen Shot 2013-01-26 at 4.39.14 PM

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 : ZFTool – Zend Framework 2 Module to Ease Development

Posted in Tutorial PHP, Zend Framework 2 by samsonasik on January 16, 2013

zf2-zendframework2ZFTool is an utility module for maintaining modular Zend Framework 2 applications. It runs from the command line and can be installed as ZF2 module or as PHAR.
It currently can show installed modules, create a new module, or create a project.
1. Installation
Open your composer.json in your skeleton application :

{
    "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.*",
        "zendframework/zftool": "dev-master"
    }
}

and add “zendframework/zftool”: “dev-master” in require, and run :

php composer.phar self-update && php composer.phar update 

OR, you can remove above step by run the following :

composer require zendframework/zftool:dev-master

and You will see process like this :
zftool_img1
and your downloaded ZFTool show in your vendor folder.
Last step for installation, edit your config/application.config.php and add ZFTool to modules array.

2. Using ZFTool
a. Getting started : Show list commands
Type :

php public/index.php

and you will see the following list :
zftool_img2

b. show modules
Type :

php public/index.php modules

and you will see like the following :
zftool_img3

c. create module
Type :

//testmodule is name of module we want to create
php public/index.php create module testmodule

and if success, you will see like the following :
zftool_img4

d. create a project
Type :

//new_prj is name of project we want to create
php public/index.php create project ../new_prj

and if success, you will see like the following :
zftool_img5

References :
https://github.com/zendframework/ZFTool

Zend Framework 2 ‘Cheat Sheet’ : Zend\Db

Posted in Tutorial PHP, Zend Framework 2 by samsonasik on January 15, 2013

zf2-zendframework2Zend Framework is a Full stack and component based framework. It can be used by standalone without full of components. At this time, I will post about Zend\Db. I hope this post will help You to try this component with or without full framework.
Continue reading

Zend Framework 2 ‘Cheat Sheet’ : Service Manager

Posted in Teknologi, Tutorial PHP, Zend Framework 2 by samsonasik on January 2, 2013

zf2-zendframework2The 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