Module.php #1

  • //
  • guest/
  • matt_attaway/
  • Swarm_Demo_Extensions/
  • CloneWorld/
  • Module.php
  • View
  • Commits
  • Open Download .zip Download (3 KB)
<?php
/**
 * @copyright   2013 Perforce Software. All rights reserved
 * @license     Please see LICENSE in top-level folder of this distribution.
 */

namespace CloneWorld;

use Projects\Model\Project;
use Zend\Mvc\MvcEvent;
use Activity\Model\Activity;

class Module
{
    /**
     * If my nemesis Andrew checks in, republish Matt's last change.
     *
     * This module demonstrates firiing off a new event based on the content of the event that is passed in.
     * It also shows how we can run our own Perforce commands.
     *
     * @param   Event   $event  the bootstrap event
     * @return  void
     */
    public function onBootstrap(MvcEvent $event)
    {
        $application = $event->getApplication();
        $services    = $application->getServiceManager();
        $manager     = $services->get('queue');
        $events      = $manager->getEventManager();

        $events->attach(
            'task.change',   // only fire on task.change events
            function ($event) use ($services, $events) { // we need events and services for this module

                // sanity check to make sure this is a properly formed Activity event
                $model = $event->getParam('activity');
                if (!$model instanceof Activity) {
                    return;
                }

		        // if Andrew checks in, remind the world of my last very important change 
                if ($model->get('user') == 'alau') {
                    $p4 = $services->get('p4');   // there is a pre-built p4 connection waiting for us
                    $changes = $p4->run('changes', array('-m1', '-s', 'submitted', '-u', 'matt')); // we run commands with flags just like from the command line
                    foreach ($changes->getData() as $change) {
                        $params = array('id' => $change['change']);
                        $events->trigger('task.change', null, $params); // here we trigger a new change event using my last change number
                    }
                }

                return;
            },
            // set our priority, -70 puts us before the other sample modules
            -70
        );
    }

    // loads up our config file. all modules should have this method.
    public function getConfig()
    {
        return include __DIR__ . '/config/module.config.php';
    }

    // makes sure our module is automatically loaded. otherwise the user would
    // need to explicitly install our module in the application
    public function getAutoloaderConfig()
    {
        return array(
            'Zend\Loader\StandardAutoloader' => array(
                'namespaces' => array(
                    __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
                ),
            ),
        );
    }
}
# Change User Description Committed
#1 8403 Matt Attaway Sample Swarm project to generate events

This module demonstrates firiing off a new event based on the content of
the event that is passed in. It also shows how we can run our own Perforce
commands and work with the results.

To install, copy the 'CloneWorld' directory into the 'modules' directory
in your Swarm installation and restart Apache