Skip to content

Commit

Permalink
Code cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
gauthierm committed May 4, 2011
1 parent c281ca7 commit 3c62c2f
Showing 1 changed file with 149 additions and 85 deletions.
234 changes: 149 additions & 85 deletions src/PEAR2/SimpleChannelFrontend/Main.php
Original file line number Diff line number Diff line change
@@ -1,58 +1,96 @@
<?php

/**
* PEAR2\SimpleChannelFrontend\Main
* \PEAR2\SimpleChannelFrontend\Main
*
* PHP version 5
*
* @category Yourcategory
* @category PEAR2
* @package PEAR2_SimpleChannelFrontend
* @author Your Name <handle@php.net>
* @copyright 2009 Your Name
* @author Brett Bieber <saltybeagle@php.net>
* @author Michael Gauthier <mike@silverorange.com>
* @copyright 2009 Brett Biever, 2011 Michael Gauthier
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License
* @version SVN: $Id$
* @link http://svn.php.net/repository/pear2/PEAR2_SimpleChannelFrontend
* @link http://pear2.php.net/PEAR2_SimpleChannelFrontend
*/

/**
* Main class for PEAR2_SimpleChannelFrontend
* Main class for \PEAR2\SimpleChannelFrontend
*
* @category Yourcategory
* @category PEAR2
* @package PEAR2_SimpleChannelFrontend
* @author Your Name <handle@php.net>
* @copyright 2009 Your Name
* @author Brett Bieber <saltybeagle@php.net>
* @author Michael Gauthier <mike@silverorange.com>
* @copyright 2009 Brett Biever, 2011 Michael Gauthier
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License
* @link http://svn.php.net/repository/pear2/PEAR2_SimpleChannelFrontend
* @link http://pear2.php.net/PEAR2_SimpleChannelFrontend
*/
namespace PEAR2\SimpleChannelFrontend;
class Main
{
/**
* The channel object
*
* @var Channel
*/
public static $channel;

static public $channel_path;


/**
* The channel path
*
* @var string
*/
public static $channel_path;

/**
* The title of the current page
*
* @var string
*/
public $page_title = '{page_title}';


/**
* The content of the current page
*
* @var string
*/
public $page_content;

public $options = array('view' => 'news',
'format' => 'html');

protected static $view_map = array('news' => 'PEAR2\SimpleChannelFrontend\News',
'packages' => 'PEAR2\SimpleChannelFrontend\PackageList',
'package' => 'PEAR2\SimpleChannelFrontend\Package',
'release' => 'PEAR2\SimpleChannelFrontend\PackageRelease',
'latest' => 'PEAR2\SimpleChannelFrontend\LatestReleases',
'categories' => 'PEAR2\SimpleChannelFrontend\Categories',
'category' => 'PEAR2\SimpleChannelFrontend\Category',
'support' => 'PEAR2\SimpleChannelFrontend\Support',
'search' => 'PEAR2\SimpleChannelFrontend\Search',
'filebrowser' => 'PEAR2\SimpleChannelFrontend\ReleaseFileBrowser',

/**
* Options passed to the view
*
* @var array
*/
public $options = array(
'view' => 'news',
'format' => 'html',
);

/**
* Map of view routes to view classes
*
* @var array
*
* @see \PEAR2\SimpleChannelFrontendMail::registerView()
*/
protected static $view_map = array(
'news' => 'PEAR2\SimpleChannelFrontend\News',
'packages' => 'PEAR2\SimpleChannelFrontend\PackageList',
'package' => 'PEAR2\SimpleChannelFrontend\Package',
'release' => 'PEAR2\SimpleChannelFrontend\PackageRelease',
'latest' => 'PEAR2\SimpleChannelFrontend\LatestReleases',
'categories' => 'PEAR2\SimpleChannelFrontend\Categories',
'category' => 'PEAR2\SimpleChannelFrontend\Category',
'support' => 'PEAR2\SimpleChannelFrontend\Support',
'search' => 'PEAR2\SimpleChannelFrontend\Search',
'filebrowser' => 'PEAR2\SimpleChannelFrontend\ReleaseFileBrowser',
);

/**
* Application URL
*
* @var string
*/
public static $url = '';

/**
Expand All @@ -63,13 +101,15 @@ class Main
public static $title = 'Simple Channel Frontend';

/**
* Constructor
* Creates a new simple channel frontend
*
* @param \PEAR2\Pyrus\ChannelFile $channel The channel object
* @param array $options Associative array of options
* @param \PEAR2\Pyrus\ChannelFile $channel the channel object.
* @param array $options an associative array of options.
*/
function __construct(\PEAR2\Pyrus\ChannelFileInterface $channel, $options = array())
{
public function __construct(
\PEAR2\Pyrus\ChannelFileInterface $channel,
$options = array()
) {
static::setChannel($channel);
$this->options = array_merge($this->options, $options);
$this->preRun();
Expand All @@ -80,7 +120,12 @@ function __construct(\PEAR2\Pyrus\ChannelFileInterface $channel, $options = arra
}
}

function preRun()
/**
* Sets appropriate HTTP headers before the page is rendered
*
* @return void
*/
protected function preRun()
{
switch ($this->options['format']) {
case 'rss':
Expand All @@ -94,86 +139,104 @@ function preRun()
}

/**
* Set the channel file for this frontend.
*
* @param \PEAR2\Pyrus\IChanelFile $channel The channel object
*
* Sets the channel file for simple channel frontend
*
* @param \PEAR2\Pyrus\IChanelFile $channel The channel object.
*
* @return void
*/
public static function setChannel(\PEAR2\Pyrus\ChannelFileInterface $channel)
{
public static function setChannel(
\PEAR2\Pyrus\ChannelFileInterface $channel
) {
\PEAR2\Pyrus\Main::$downloadClass = __NAMESPACE__ . '\\Internet';
\PEAR2\Pyrus\Config::current()->cache_dir = '/tmp';

static::$channel = \PEAR2\Pyrus\Config::current()->channelregistry['pear2.php.net'];

$config = \PEAR2\Pyrus\Config::current();

static::$channel = $config->channelregistry['pear2.php.net'];
static::$channel_path = dirname($channel->path);

$rest = str_replace('http://' . $channel->name,
'',
$channel->protocols->rest['REST1.0']->baseurl);

Internet::addDirectory(static::$channel_path . '/get',
'http://' . $channel->name . '/get/');
Internet::addDirectory(static::$channel_path . $rest,
$channel->protocols->rest['REST1.0']->baseurl);

$rest = str_replace(
'http://' . $channel->name,
'',
$channel->protocols->rest['REST1.0']->baseurl
);

Internet::addDirectory(
static::$channel_path . '/get',
'http://' . $channel->name . '/get/'
);

Internet::addDirectory(
static::$channel_path . $rest,
$channel->protocols->rest['REST1.0']->baseurl
);

static::$channel->fromArray($channel->getArray());
}

/**
* Determine which view to instantiate and set as the page content
*
* @return mixed
* Determines and instantiates the page class
*
* The instantiated page class is set as the page content.
*
* @return void
*/
function run()
protected function run()
{
if (!array_key_exists($this->options['view'], static::$view_map)) {
throw new UnregisteredViewException('No view, or incorrect view specified.');
throw new UnregisteredViewException(
'No view, or incorrect view specified.'
);
}

$class = static::$view_map[$this->options['view']];
$options = array_merge($this->options, array('frontend'=>$this));
$options = array_merge($this->options, array('frontend' => $this));
$this->page_content = new $class($options);
}

/**
* Register a new view for the channel.
*
* @param string $route The route used to identify this model and view
* @param string $class Class to instantiate when this view is requested.
*
* @return Main
* Registers a new view for the channel
*
* @param string $route the route used to identify this model and view.
* @param string $class the Class to instantiate when the specified view is
* requested.
*
* @return Main the current class for fluent interface.
*/
function registerView($route, $classname)
public function registerView($route, $classname)
{
static::$view_map[$route] = $classname;
return $this;
}

/**
* Get the URL to a specific view
*
* @param mixed $class What class to return a route for
*
* @return string The url
* Gets the URL for a specific view
*
* @param mixed $class optional. The class for which to return a route.
*
* @return string the URL.
*/
public static function getURL($class = null)
{
static $default_view;

if (empty($default_view)) {
$main = new \ReflectionClass(__CLASS__);
$properties = $main->getDefaultProperties();
$main = new \ReflectionClass(__CLASS__);
$properties = $main->getDefaultProperties();
$default_view = $properties['options']['view'];
}

$url = static::$url;
if ($class) {
if (is_object($class)) {
$class = get_class($class);
}
$route = array_keys(static::$view_map, $class);
if (!count($route)) {
throw new UnregisteredViewException('The view for that object is not registered');
throw new UnregisteredViewException(
'The view for that object is not registered.'
);
}

if ($route[0] != $default_view) {
Expand All @@ -183,19 +246,20 @@ public static function getURL($class = null)
}
return $url;
}

/**
* Called after the page is rendered to perform any necessary replacements.
* Performs necessary replacements after the page is rendered
*
* @param string $html The rendered template.
*
* @return string Filtered html
* @return string Filtered html.
*/
public function postRender($html)
{
$html = str_replace('{page_title}',
$this->page_title,
$html);
return $html;
return str_replace(
'{page_title}',
$this->page_title,
$html
);
}
}

0 comments on commit 3c62c2f

Please sign in to comment.