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.

OpenStack.org: RackSpace Open Sources Their Cloud Services Platform, And Gets NASA On Board

Hosting company RackSpace is open sourcing the software behind its cloud storage and computing platforms on Monday, the company is saying. The company is also preparing to launch OpenStack, an open source cloud platform, and will donate the open source code to that project.

NASA is also incorporating technology from the NASA Nebula Cloud Platform into the OpenStack project, says RackSpace.

RackSpace says they want to drive interoperability in cloud services to avoid vendor lock-in, and help create industry standards. More than 25 companies have shown interest in the project, says RackSpace, or are actively working on the code. They include AMD, Citrix, Cloud.com, Cloudkick, CloudSwitch, Dell, enStratus, FathomDB, Limelight, Nicira, NTT DATA, Opscode, Peer 1, Puppet Labs, RightScale, Riptano, Scalr, Sonian, Spiceworks and Zuora.

The code is being released under the extremely flexible Apache 2 license, meaning third parties can redistribute the code, build proprietary software around the code, and distribute it with few restrictions.

Complete Doctrine 1.2x Integration with Zend Framework 1.10+

To achieve complete Doctrine 1 integration with Zend Framework some glue is required, Benjamin Eberlei has created a complete solution thats straight forward, easy to use and understand.

This project tries to offer a complete Integration of Doctrine 1 with Zend Framework. The following components belong to this Integration:

  • Zend_Application Resource
  • Zend Framework Modular Project Support
  • Zend_Tool Provider for Doctrine Model Generation, Migrations and Fixtures
  • Zend_Paginator Adapter for Doctrine Queries
  • Dynamic Zend_Form generation from Doctrine Models

This integration requires the latest Doctrine version 1.2.2 to work completely

Get it!

SVN Export or Externals

Github offers SVN Read support for a while now, you can either use svn export or svn:externals to include ZFDoctrine into your project or into your PHP Include Path.
svn checkout http://svn.github.com/beberlei/zf-doctrine.git

Git Clone

git clone git://github.com/beberlei/zf-doctrine.git
If you follow the tutorial and installation steps your will get this in ZFTool.

Zend Framework Command Line Console Tool v1.10.4
Actions supported by provider "Doctrine"
Doctrine
zf create-project doctrine dsn zend-project-style library-per-module single-library
zf build-project doctrine force load reload
zf create-database doctrine
zf drop-database doctrine force
zf create-tables doctrine
zf generate-sql doctrine
zf dql doctrine
zf load-data doctrine append
zf dump-data doctrine individual-files
zf generate-models-from-yaml doctrine
zf generate-yaml-from-models doctrine
zf generate-yaml-from-database doctrine
zf generate-migration doctrine class-name from-database from-models
zf excecute-migration doctrine to-version
zf show-migration doctrine
zf show doctrine

Read it ALL at beberlei’s zf-doctrine at master – GitHub.