Zend_Server Class

e_schrade wrote a neat way of doing things in the service layer; Let’s take a quick look at something that’s kind of neat in Zend Framework. I’ve been doing some work with Adobe on some articles and one of them was on working with mobile clients with Flash. Well, me being the masochist I did more. What I did was write an example that worked as a full website, an Ajax website, a Flash service and an XML-RPC service.

Looks like a lot, right?  Actually there’s not much there.  Here’s the logic flow.

  • Is it an XMLHTTP Request and is it a POST? Create the Json server
  • Is it an AMF request? Create the AMF server
  • Is it an XmlRpc request? Create the XmlRpc server
  • Is it an XMLHTTP Request and is it a GET? Create the Service Map (for JSON-RPC 2.0)
  • If a service handler has been created add all of the application’s mappers, attach the service handler to the request and redirect to the service action.

And with that you have an application that can serve content for multiple different types of service with almost no effort on your part.  At least.. if you copy and paste this code.

Have a good Friday!!!

via Zend_Server – zf ffh zend_server on e_schrade – zend php.

Finding syntax errors in your PHP Project files

Till posted this little snippet;
It’s so useful I just had to share it 🙂

find . \( -name "*.php" -o -name "*.phtml" \) -exec php -l {} \;

Just go to your project directory and fire it off, it will help you find those pesky unmatched {}

Search each class for function names that match except for the underscore prefix

Bill Karwin posts a useful little snippet that will list and search each class for function names that match except for the underscore prefix, private / protected functions.

< ?php /** * Find methods that differ only by the underscore prefix. * by Bill Karwin August 2010 * * I release this code under the terms of the New BSD License: * http://framework.zend.com/license/new-bsd */ // Change this to suit your environment define("LIBRARY_DIR", "/Users/bill/Library/PHP/ZF/library"); // Pre-load some files to satisfy the autoloader. // We could also add the local PEAR library dir to the autoloader. require_once("PHPUnit/Framework/SelfDescribing.php"); require_once("PHPUnit/Framework/AssertionFailedError.php"); require_once("PHPUnit/Framework/Assert.php"); require_once("PHPUnit/Framework/Test.php"); require_once("PHPUnit/Extensions/Database/DataSet/ITable.php"); require_once("PHPUnit/Extensions/Database/DataSet/AbstractTable.php"); require_once("PHPUnit/Extensions/Database/DataSet/IDataSet.php"); require_once("PHPUnit/Extensions/Database/DataSet/AbstractDataSet.php"); require_once("PHPUnit/Extensions/Database/ITester.php"); require_once("PHPUnit/Extensions/Database/AbstractTester.php"); require_once(LIBRARY_DIR . "/Zend/Loader/Autoloader.php"); Zend_Loader_Autoloader::getInstance(); // Find every PHP file under the library dir and slurp them in. // Yes that's a lot of files. Deal with it. $Directory = new RecursiveDirectoryIterator(LIBRARY_DIR); $Iterator = new RecursiveIteratorIterator($Directory); $Regex = new RegexIterator($Iterator, '/^.+\.php$/i'); foreach ($Regex as $filename) { require_once($filename); } // Loop over each class now in the PHP runtime. // Filter by classes named Zend*. $classes = get_declared_classes(); $zendclasses = new RegexIterator(new ArrayIterator($classes), '/ ^Zend/'); foreach ($zendclasses as $classname) { // Search each class for function names that match except for the underscore prefix // Note this includes duplicates and magic methods, so you have to do some sorting // on the output. Hint: `sort -u`. $class = new ReflectionClass($classname); $methods = $class->getMethods();
foreach ($methods as $method) {
if (preg_match("/^__*(.*)/", $method->name, $matches)) {
$underscore = $method;
if ($class->hasMethod($matches[1])) {
$nonunderscore = $class->getMethod($matches[1]);
echo $underscore->getDeclaringClass()->name
. "::" . $underscore->name . "()" . " => ";
if ($underscore->getDeclaringClass() != $nonunderscore->getDeclaringClass()) {
echo $nonunderscore->getDeclaringClass()->name;
}
echo "::" . $nonunderscore->name . "()" . "\n";
}
}
}

}

Testing Zend Framework controllers in isolation

What I do is I have my controllers fetch all their dependencies from the bootstrap and/or front controller. The most common example is to pull the db resource from the bootstrap:

// in controller
$db = $this->getInvokeArg('bootstrap')->getResource('db');

But I also take it a step further. For example, if I’m using data mappers, I have the action controller check the front controller for the data mapper I need:

// in controller
$postsMapper = $this->getInvokeArg('posts_mapper');

I then update your unit test to inject the posts mapper with a stub:

// in unit test
$this->frontController->setParam('posts_mapper', $stubPostsMapper);

However, that invoke arg won’t exist in production, so I wrap that call in an if statement a la “lazy loading” style:

if (null === ($postsMapper = $this->getInvokeArg('posts_mapper'))) {
$postsMapper = new Default_Model_Mapper_Posts();
}

What this does is it allows me to stub in my stub posts mapper in my unit tests while letting the controller lazy-load the real one in production.

An alternative is to use Zend_Registry, but I find this to be a bit cleaner without the static calls.


Hector Virgen

Zend Framework 2.0 (2.0.0dev1)

Yesterday, the Zend Framework team tagged the first development milestone of Zend Framework 2.0 (2.0.0dev1). It is immediately downloadable from the Zend Framework servers:

* Zip package:

http://framework.zend.com/releases/ZendFramework-2.0.0dev1/ZendFramework-2.0.0dev1.zip

* tar.gz package:

http://framework.zend.com/releases/ZendFramework-2.0.0dev1/ZendFramework-2.0.0dev1.tar.gz

NOTE! This release is not considered of production quality, and is released solely to provide a development snapshot for purposes of testing and research. Use at your own risk.

This release is the culmination of several months of work, and incorporates the following features:

* Removal of all require_once statements.

* Migration to namespaces.

* Refactoring of the test suite, including:

* Removal of all “AllTests.php” files.

* Removal of unreferenced test classes.

* Limited refactoring to move helper classes into their own files.

* Refactoring of conditional tests.

* Rewrite of Zend\Session from the ground up. This required creation of a new component, Zend\SignalSlot, for handling observers and creating filter chains.

* Addition of a new Zend\Stdlib namespace for interfaces and utility classes; in particular, we added extensions to SplQueue, SplStack, and  SplPriorityQueue to create serializable versions of these classes.

We have done some “real-world” testing of the release by building the Quick Start application, as well as migrating an existing demo application to ZF2. We were able to achieve both goals, demonstrating that while the release is certainly pre-alpha, it is definitely functional.

There is much work yet to be done. Today, we published a rough roadmap of milestones we will be working towards (1). This roadmap only addresses components with cross-cutting concerns, but serves as a guide for development in the coming months. If you are interested in contributing, be sure to sign our Contributors License Agreement (CLA), and read the “README-DEV.txt” file in the release. We also suggest you join the zf-contributors mailing list (2), and join in discussions on the #zftalk.dev IRC channel on Freenode.

[1] http://framework.zend.com/wiki/display/ZFDEV2/Zend+Framework+2.0+Milestones

[2] http://zend-framework-community.634137.n4.nabble.com/ZF-Contributor-f680267.html

Matthew Weier O’Phinney

Tutorial: Getting Started with Zend_Auth

Rob Allen writes; After too many months of neglect, I have completely rewritten my Zend_Auth tutorial so that it is compatible with Zend Framework 1.10!

As an experiment, I have written it directly in HTML, rather than PDF as before and cover the login form along with the login controller code required to authenticate a user using a database table. For good measure, I’ve included logging out and a view helper to show how to access the logged in user’s details.

The full source code is also available, if you don’t want to type it in 🙂

I hope you find it useful.

Tutorial: Getting Started with Zend_Auth – Rob Allen’s DevNotes.