Key
This line was removed.
This word was removed. This word was added.
This line was added.

Changes (37)

View Page History
Zend_Commons is a collection of object-oriented strategies wrapping most common algorithms and operations. The biggest problem with existing PHP functions like {{fopen()}}, {{file_get_contents()}}, {{shell_exec()}}, etc. is that they are not object-oriented and are a huge legacy from PHP4. Zend_Commons resolves this problem by means of an extendable collection of classes designed in a Strategy pattern.

Moreover, this approach is very helpful during unit testing, when sensitive I/O, system, HTTP, etc. operations shall be properly isolated and mocked. When every operation is presented as an object retrieved from a factory - we can control their behavior to the very last detail.

I can't imagine right now all possible algorithms we may wrap in Zend_Commons. The idea is to create a factory of reusable components, which can be extended later by other contributors.
{zone-data}
** Zend_Commons_Exception
** Zend_Commons_Abstract
** Zend_Commons_Io_Abstract Zend_Commons_Listener
** Zend_Commons_Event
*** Zend_Commons_Io_ReadFile
*** Zend_Commons_Io_WriteFile
*** Zend_Commons_Io_Delete
*** Zend_Commons_Io_Mkdir
** Zend_Commons_Exec
** Zend_Commons_Email
** Zend_Commons_Ftp Zend_Commons_Http
* more later...
{zone-data}
{card:label=UC-01: Zend_Commons}
{code}
// We have
$content = Zend_Commons::factory('io_ReadFile')
// We want to make a directory and all necessary parent directories
// on top of it. We would like this operation to be properly
// logged. If the directory is actually created we would like to
// get a notification. In case of any problems we would like
// an exception to be thrown. Here it is:
Zend_Commons::factory('io_Mkdir')
->setFileName('some-file.txt') ->setDirName($directory)
->setCreateMissedParents(true)
->setVerbose(true)
->addLog($logger)
->addListener($listener)
->execute();

// The majority of configuration options can be configured
// in the factory class:
Zend_Commons::addLog($logger);
Zend_Commons::setVerbose(true);
Zend_Commons::addListener($listener);

// In unit tests we can easily replace every particular operation
// with our own implementation, or just disable it. For example:
Zend_Commons::factory('io_Write')
->setId('writing-config-file') // it's unique in application
->setFileName('/etc/passwd') // we don't want to touch it in tests
->setContent($content)
->execute();
// Later in unit test we mock the operation and override its
// parameters:
Zend_Commons::mock('io_Write', 'writing-config-file')
->setFileName('/tmp/passwd.txt');
{code}
{card}
// Instead of using file_get_contents() and validate its response
// for possible NULL value we may use this object approach. First, we
// instantiate an object of class Zend_Commons_Io_ReadFile. Then we
// configure the object via fluent interface and start the
// operation by means of execute() method. If something
// happens inside, the strategy throughs an exception.
$content = Zend_Commons::factory('io_ReadFile')
->setFileName('some-file.txt')
->execute(); // exception here if any
// to the strategy, which will tailor its behavior to our
// particular needs, for example:
$content = Zend_Commons::factory('io_ReadFile')
->setFileName('some-file.txt')
->setBufferLimit(50 * 1024) // limit memory usage up to 50K
// is typically solved by custom functions. With Zend_Commons
// strategy object we can do it as simple as this:
Zend_Commons::factory('delete') Zend_Commons::factory('io_Delete')
->setName('/home/user/some/directory')
->setIgnoreIfAbsent(true) // don't panic if it's absent

/**
* Shall our operations be verbose?
* @param boolean Shall they?
* @return void
*/
public static function setVerbose($verbose);

/**
* Add logger to be used in all operations.
* @param Zend_Log The logger to use

/**
* Add new listener.
* @param Zend_Log The logger to use
* @param Zend_Commons_Listener The listener
* @return void
*/
public static function addLog(Zend_Log $log); addListener(Zend_Commons_Listener $lstnr);

/**
* Remove logger previously added. listener added before.
* @param Zend_Log Zend_Commons_Listener The logger listener to remove
* @return void
*/
public static function removeLog(Zend_Log $log); removeLog(Zend_Commons_Listener $lstnr);

/**
* We can mock any other operation.
* @param string Class name
* @param string Unique ID of the operation
* @return Zend_Commons_Abstract
*/
public static function mock($cls, $id);
}

{
/**
* Shall we protocol this particular operation to Execute the operation.
* the log file? @return mixed
* @param boolean Shall we?
* @return $this
*/
public abstract function execute();

/**
* Shall the operation be verbose?
* @param boolean Shall it?
* @return Zend_Commons_Abstract
*/
public function setVerbose($verbose);

/**
* Add logger to be used in all operations.
* @param Zend_Log The logger to use
* @return Zend_Commons_Abstract
*/
public function addLog(Zend_Log $log);

/**
* Remove logger previously added.
* @param Zend_Log The logger to remove
* @return Zend_Commons_Abstract
*/
public function removeLog(Zend_Log $log);

/**
* Add new listener.
* @param Zend_Commons_Listener The listener
* @return Zend_Commons_Abstract
*/
public function addListener(Zend_Commons_Listener $lstnr);

/**
* Remove listener added before.
* @param Zend_Commons_Listener The listener to remove
* @return Zend_Commons_Abstract
*/
public function removeLog(Zend_Commons_Listener $lstnr);
}
interface Zend_Commons_Listener
{
/**
* Listen to the event passed from operation.
* @return void
*/
function listen(Zend_Commons_Event $event);
}
class Zend_Commons_Event
{
}
{code}
{card}
{card:label=Zend_Commons_Io}
{code}
/**
* Read file content to a PHP variable.
*/
class Zend_Commons_Io_Read extends Zend_Commons_Abstract
{
/**
* Set the name of file to read.
* @param string The name
* @return $this
*/
public function setFileName($name);

/**
* Read the file and return its content.
* @return string
*/
public function execute();
}

/**
* Write content to a file.
*/
class Zend_Commons_Io_Write extends Zend_Commons_Abstract
{
/**
* Set the name of file to write.
* @param string The name
* @return $this
*/
public function setFileName($name);

/**
* Set content to write.
* @param string The content
* @return $this
*/
public function setContent($content);

/**
* Write the file.
* @return string
*/
public function execute();
}

/**
* Delete file or directory.
*/
class Zend_Commons_Io_Delete extends Zend_Commons_Io_Abstract
{
*/
public function setName($name);

/**
* Execute the operation, delete the file or directory.
* @return void
*/
public function delete(); execute();
}

/**
* Make directory.
*/
class Zend_Commons_Io_Mkdir extends Zend_Commons_Io_Abstract
{
/**
*/
public function setDirectory($dir);

/**
* Execute the operation, create the directory.
* @return void
*/
public function mkdir(); execute();
}
{code}