Archiv für die Kategorie ‘PHP’

PHPUnit Manual – Sebastian Bergmann

Freitag, 30. Oktober 2009 von sägefisch

Hier der komplette PHPUnit Guide um entsprechende Tests zu seiner Applikation zu schreiben.

PHPUnit Manual

Professionelle Softwareentwicklung mit PHP 5

Freitag, 30. Oktober 2009 von sägefisch

Der ultimative Guide für Objektorientierung, Entwurfsmuster und Modellierung sowie fortgeschrittene Datenbankprogrammierung von Sebastian Bergmann

Professionelle Softwareentwicklung mit PHP 5

der Kohana Script Collector Helper

Sonntag, 25. Oktober 2009 von sägefisch

Um modular und agil in Kohana zu entwickeln, wurde ein Skript Kollektor notwendig, der aus allen Controllern (Template- oder Standard-Controllern) Skripte (CSS, Javascript) sammeln kann.

Diese Scripte werden dann auf den jeweiligen Mastertemplates wieder an den richtigen Stellen eingebunden.
Dazu habe ich einen neuen Helper unter application/helpers/collector.php eingerichtet.


class Collector_Core
{
    /**
     * Arrays containing URL's to scripts/styles (fill with standards)
     * @var string
     */
    static protected $scripts       = array();
    static protected $styles        = array();

    /**
     * Adds a url to store
     * @param string $file the local path to file
     * @return void
     */
    static public function addJs($file)
    {
        self::$scripts[] = $file;
    }

    /**
     * Adds a url to store
     * @param string $file the local path to file
     * @return void
     */
    static public function addCss($file)
    {
        self::$styles[] = $file;
    }

    /**
     * Generates/renders collectors items
     * @param boolean      $print whether to echo the output or just return rendered string
     * @return string      the rendered output
     */
    static public function renderJs($print = false)
    {
        $scripts    = array_unique(self::$scripts);
        $output     = html::script($scripts);
        if ($print)
        {
            echo $output;
        }
        else
        {
            return $output;
        }
    }

    /**
     * Generates/renders collectors items
     * @param boolean      $print whether to echo the output or just return rendered string
     * @param string|array $media type for this style (all, screen, print, media)
     * @return string      the rendered output
     */
    static public function renderCss($print = false, $media = 'all')
    {
        $styles = array_unique(self::$styles);
        $output = html::stylesheet($styles, $media);
        if ($print)
        {
            echo $output;
        }
        else
        {
            return $output;
        }
    }
} // end of Collector_Core

Dieser Helper kann nun aus allen Controllern heraus befüllt werden.


class Welcome_Controller extends Template_Controller
{
    /**
     * set master template
     */
    public $template = 'master_default.tpl';

    /**
     * default constructor
     * @param void
     * @return void
     */
    public function __construct()
    {
        // load parent constructor
        parent::__construct();

        // collect scripts and styles
        collector::addCss('/css/fancybox');
        collector::addJs('/js/jquery.1.3.2');
        collector::addJs('/js/jquery.fancybox');
    }
    /** more code here */
} // end of Welcome_Controller

Nachdem nun alle relevanten Skripte eingesammelt wurden, kann man diese auf dem Template wieder ausgeben lassen.


<?php collector::renderCss(true, 'all'); ?>

<!-- html code here -->

<?php collector::renderJs(true); ?>

Der Kollektor sorgt dafür das keine doppelten Skripte geladen werden.

SOAP Service mit Kohana und Zend AutoDiscover

Sonntag, 25. Oktober 2009 von sägefisch

Eine einfache all-in-one Lösung für Standard SOAP Services kann man mit dem Kohana Framework und der Zend Bibliothek realisieren.

Dazu bedarf es lediglich eines Kohana Frontcontrollers über den wir den Service und die WSDL ansprechen können.

Zur automatischen Generierung der WSDL, bedienen wir uns hier der Zend AutoDiscover Klasse. Dieser Klasse übergibt man lediglich die ServiceModel-Klasse mit enthaltenen Annotationen, die die Servicefunktionen enthält.

Aus den Annotationen generiert Zend AutoDiscover eine passende WSDL.


/**
 * include libs and models
 *
 */
include('Zend/Soap/AutoDiscover.php');
include(APPPATH . 'models/service.php');

/**
 * this class represents a controller
 * application/controllers/soap.php
 *
 * @package     SOAPService
 * @subpackage  ...
 * @author      saegefisch (xxx@xxx.xx)
 * @copyright   (c) 2009 xxx
 */
class Soap_Controller extends Controller
{
    /**
     * default constructor
     *
     * @param void
     * @return void
     */
    public function __construct()
    {
        // load parent constructor
        parent::__construct();
    }

    /**
     * service to call
     *
     * @param void
     * @return void
     */
    public function service()
    {
        // disable wsdl cache
        ini_set('soap.wsdl_cache_enabled', '0');

        // set auth settings if needed
        $settings   = array(
                          'login'              => 'user',
                          'password'        => 'password',
                          'authentication' => SOAP_AUTHENTICATION_BASIC,
                          'soap_version'   => SOAP_1_2,
                          'encoding'         => 'UTF-8',
                          'cache_wsdl'     => WSDL_CACHE_NONE
                          );

        // include user:password if needed
        $wsdl = 'http://user:password@' . $_SERVER['HTTP_HOST'] . '/soap/wsdl';
        $server = new SoapServer($wsdl, $settings);
        $server->setClass('Service_Model');
        $server->handle();
    }

    /**
     * wsdl to call
     *
     * @param void
     * @return void
     */
    public function wsdl()
    {
        // disable wsdl cache
        ini_set('soap.wsdl_cache_enabled', '0');

        $wsdl = new Zend_Soap_AutoDiscover();
        $wsdl->setUri('http://' . $_SERVER['HTTP_HOST'] . '/soap/service');
        $wsdl->setClass('Service_Model');
        $wsdl->handle();
    }
}

Hier stellen wir das Standard Model für unseren SOAP Service zusammen.


/**
 * this class represents a model
 * application/models/service.php
 *
 * @package     SOAPService
 * @subpackage  ...
 * @author      saegefisch (xxx@xxx.xx)
 * @copyright   (c) 2009
 */
class Service_Model extends Model
{
    /**
     * default constructor
     *
     * @param   void
     * @return  void
     */
    public function __construct()
    {
        // load database library into $this->db (can be omitted if not required)
        parent::__construct();
    }

    /**
     * dummy function
     *
     * @param   int $int
     * @param   string $string
     * @param   array $arr
     * @param   object $obj
     * @param   bool $bool
     * @return  array
     */
    public function get_dummy_array($int, $string, $arr, $obj, $bool)
    {
        return array();
    }

    /**
     * dummy function
     *
     * @param   int $int
     * @param   string $string
     * @param   array $arr
     * @param   object $obj
     * @param   bool $bool
     * @return  bool
     */
    public function get_dummy_boolean($int, $string, $arr, $obj, $bool)
    {
        return true;
    }

    /**
     * dummy function
     *
     * @param   int $int
     * @param   string $string
     * @param   array $arr
     * @param   object $obj
     * @param   bool $bool
     * @return  string
     */
    public function get_dummy_string($int, $string, $arr, $obj, $bool)
    {
        return 'foo=bar';
    }
}

der Criteria Builder für Propel ORM Criterias

Sonntag, 25. Oktober 2009 von sägefisch

propel_criteria_builder

Die Propel ORM für PHP bedient sich einer eigenen Syntax (Criterias) um Queries zusammenzustellen.

Hier gibt es einen Criteria Builder, den man mit Standard SQL füttern kann.

Der Criteria Builder konvertiert sodann das eingegebene SQL Statement in ein Propel Criteria.

So wird aus diesem kleinen SQL Statement:


SELECT user.*
FROM user
WHERE user.state = 100
AND (user.name = 'user' OR user.email = 'user@domain.tld')

diese Propel Criteria:


$c = new Criteria();
$crit0 = $c->getNewCriterion(UserPeer::STATE, 100);
$crit1 = $c->getNewCriterion(UserPeer::NAME, 'user');
$crit2 = $c->getNewCriterion(UserPeer::EMAIL, 'user@domain.tld');

// Perform OR at level 1 ($crit1 $crit2 )
$crit1->addOr($crit2);

// Perform AND at level 0 ($crit0 $crit1 )
$crit0->addAnd($crit1);

// Remember to change the peer class here for the correct one in your model
$c->add($crit0);
$result = TablePeer::doSelect($c);

// This loop will of course need to be edited to work
foreach ($result as $obj)
{
	//$val = $obj->getValue();
}

Einen Versuch ist es Wert.

Hier gehts zum Propel ORM Criteria Builder