1. Digging Through the Guts of Enterprise PHP

    A Case Study

    Shawn Lauriat

    IBM Rational Build Forge

  2. Introductions

    Me
    Advisory Engineer, Lead UI/G11N Developer @ IBM Rational Build Forge
    Author, Advanced Ajax: Architectures and Best Practices
    http://frozen-o.com/blog/
    Build Forge
    It automates and accelerates build and release processes through server pooling and fault tolerance for Agile development and streamlined software delivery.
    a.k.a. process automation tool
  3. Disclaimer

    The views expressed in this commentary are the author's own and do not necessarily reflect those of IBM.

  4. Overview

    I will not:

    I will:

  5. Interfaces

    class -> Instance Definition

    interface -> Instance Requirements Definition

    interface + type hinting

    = more stable code with less effort

  6. BuildForge_Services_Serializable

    
    /** Used for object transportation */
    interface BuildForge_Services_Serializable {
    
        /** @param array|null $data */
        public function fromArray(array $data = null);
    
        /** @return array */
        public function toArray();
    
    }
  7. BuildForge_Services_DBO_Snapshottable

    
    /** Create snapshot metadata */
    $snapshot = BuildForge_Services_DBO_Snapshot::createSnapshot(
        $services,
        "My New Snapshot",
        "A Comment about My New Snapshot"
    );
    /** Get a new snapshot of the $originalOne */
    return $originalOne->snapshot(
        $snapshot->getUuid(),
        new BuildForge_Services_DBO_SnapshotRequest(true),
        false
    );
  8. Inheritance

    BuildForge_Services_DBO_Project

  9. Iterators

    
    interface Iterator extends Traversable {
        function rewind();
        function current();
        function key();
        function next();
        function valid();
    }
  10. Using Iterators for scalability

    The Class Tree

    Iterator
    OuterIterator
    BuildForge_Services_Iterator
    BuildForge_Services_ProjectIterator
    Iterator
    BuildForge_Services_ValueIterator
  11. Using Iterators for Scalability

    The Code

    class BuildForge_Services_DBO_ProjectIterator
            extends BuildForge_Services_Iterator {
        public function current() {
            $current = parent::current();
            if (empty($current)) return $current;
            $project = new BuildForge_Services_DBO_Project($this->conn);
            $project->fromArray($current);
            return $project;
        }
    }
    // Elsewhere in the app...
    $projects = BuildForge_Services_DBO_Project::iterateAll($services);
    foreach ($projects as $project) {
        // Do stuff
    }
  12. Design Patterns

    An entity relationship diagram of the Model, View, and Controller aspects of Project::iterateFiltered usage.
  13. Model-View-Controller Code

    class Projects {
        public function render() {
            $projects = BuildForge_Services_DBO_Project
                    ::iterateFiltered(
                $this->services,
                $filter
            );
            $this->view->register_object('listing', $projects);
        }
    }
    <tbody id="listing">
        <?php try {
            foreach ($_['listing'] as $project_instance) {
                print '<tr>';
                // etc.
                print '</tr>';
            }
        } catch (Exception $e) { /* do something */ } ?>
    </tbody>
  14. Observer to Track State

    class BuildForge_ServicesConnection
            implements BuildForge_Strings_Localizer {
    
        protected function createResponse() {
            $this->mode = BUILDFORGE_SERVICES_MODE_READ;
            $response = new BuildForge_Services_JSONResponse($this);
            $response->attach($this);
            return $response;
        }
    
        public function update(SplSubject $subject) {
            $this->mode = BUILDFORGE_SERVICES_MODE_IDLE;
        }
    }
  15. Observer to Track State

    class BuildForge_Services_Response
            extends BuildForge_Utilities_Observable {
    
        public function __destruct() {
            $this->readRemaining();
            $this->notify();
        }
    }
  16. Other PHP-Specific Additions

    __toString() for all DBOs

    print $project;
    
    /* prints:
    Project[projectId=5bd405d70c3f1000d081238b009625ac, level=6, name=Project One, class=58BB01F0-836A-11DD-A3D4-8B1B7F9F254D, selectorId=5bd403da0c3f100086e8238b0043b5da, tag=BUILD_$B, envId=, active=1, snotify=0, pnotify=0, fnotify=0, maxThread=0, runLimit=0, tagSync=, sticky=, passChainId=, failChainId=, geoId=, isDefaultSnapshot=1, snapshotUuid=!Snapshot, snapshotSetUuid=5bd405d30c3f1000d080238b009625ac, parentUuid=, steps= <Step[stepUuid=5bd405de0c3f1000d082238b009625ac, projectId=5bd405d70c3f1000d081238b009625ac, stepId=1, envId=, filterId=, inlineChainId=, description=Step one, stepType=REGULAR, dir=/, passNotify=0, failNotify=0, onFail=HALT, selectorId=, threaded=NO, absolute=, timeout=300, maxIterations=100, level=0, broadcast=, commandText=echo "hi", elseCommandText=, conditionText=, passChainId=, failChainId=, elseInlineId=, active=1, passWait=, failWait=]>]
    */

    __clone() for clone keyword support

    $projectTwo = clone $projectOne;
    $projectTwo->setName('Look, a new Project!');
    $projectTwo->create();
  17. These make my life easier

  18. What I can't wait to use next

  19. Questions?