Modules

Kodoc_Class
extends Kohana_Kodoc_Class
extends Kodoc
extends Kohana_Kodoc

Class documentation generator.

package
Kohana/Userguide
category
Base
author
Kohana Team
copyright
© 2009-2012 Kohana Team
license
http://kohanaphp.com/license

Class declared in MODPATH/userguide/classes/kodoc/class.php on line 3.

Properties

public ReflectionClass $class

The ReflectionClass for this class

public array $constants

array of this classes constants

public string $description

description of the class from the comment

public string $modifiers

modifiers like abstract, final

public array $parents

Parent classes/interfaces of this class/interface

public static string $regex_class_member

PCRE fragment for matching 'Class', 'Class::method', 'Class::method()' or 'Class::$property'

string(33) "((\w++)(?:::(\$?\w++))?(?:\(\))?)"

public array $tags

array of tags, retrieved from the comment

Methods

public __construct( string $class ) (defined in Kohana_Kodoc_Class)

Loads a class and uses reflection to parse the class. Reads the class modifiers, constants and comment. Parses the comment to find the description and tags.

Parameters

  • string $class required - Class name

Return Values

  • void

Source Code

public function __construct($class)
{
	$this->class = new ReflectionClass($class);

	if ($modifiers = $this->class->getModifiers())
	{
		$this->modifiers = '<small>'.implode(' ', Reflection::getModifierNames($modifiers)).'</small> ';
	}

	if ($constants = $this->class->getConstants())
	{
		foreach ($constants as $name => $value)
		{
			$this->constants[$name] = Debug::vars($value);
		}
	}

	// If ReflectionClass::getParentClass() won't work if the class in 
	// question is an interface
	if ($this->class->isInterface())
	{
		$this->parents = $this->class->getInterfaces();
	}
	else
	{
		$parent = $this->class;

		while ($parent = $parent->getParentClass())
		{
			$this->parents[] = $parent;
		}
	}

	$parents = $this->parents;

	array_unshift($parents, $this->class);

	foreach ($parents as $parent)
	{
		if ($comment = $parent->getDocComment())
		{
			// Found a description for this class
			break;
		}
	}

	list($this->description, $this->tags) = Kodoc::parse($comment);
	
	// If this class extends Kodoc_Missing, add a warning about possible
	// incomplete documentation
	foreach ($parents as $parent)
	{
		if ($parent->name == 'Kodoc_Missing')
		{
			$warning = "[!!] **This class, or a class parent, could not be
			           found or loaded. This could be caused by a missing
					   module or other dependancy. The documentation for
					   class may not be complete!**";
			$this->description = Kodoc_Markdown::markdown($warning).$this->description;
		}
	}
}

public methods( ) (defined in Kohana_Kodoc_Class)

Gets a list of the class properties as Kodoc_Method objects.

Return Values

  • array

Source Code

public function methods()
{
	$methods = $this->class->getMethods();

	usort($methods, array($this,'_method_sort'));

	foreach ($methods as $key => $method)
	{
		$methods[$key] = new Kodoc_Method($this->class->name, $method->name);
	}

	return $methods;
}

public properties( ) (defined in Kohana_Kodoc_Class)

Gets a list of the class properties as Kodoc_Property objects.

Return Values

  • array

Source Code

public function properties()
{
	$props = $this->class->getProperties();

	usort($props, array($this,'_prop_sort'));

	foreach ($props as $key => $property)
	{
		// Create Kodoc Properties for each property
		$props[$key] = new Kodoc_Property($this->class->name, $property->name);
	}

	return $props;
}

public static class_methods( ) (defined in Kohana_Kodoc)

Get all classes and methods of files in a list.

I personally don't like this as it was used on the index page. Way too much stuff on one page. It has potential for a package index page though. For example: class_methods( Kohana::list_files('classes/sprig') ) could make a nice index page for the sprig package in the api browser ~bluehawk

Source Code

public static function class_methods(array $list = NULL)
{
	$list = Kodoc::classes($list);

	$classes = array();

	foreach ($list as $class)
	{
		$_class = new ReflectionClass($class);

		if (stripos($_class->name, 'Kohana_') === 0)
		{
			// Skip transparent extension classes
			continue;
		}

		$methods = array();

		foreach ($_class->getMethods() as $_method)
		{
			$declares = $_method->getDeclaringClass()->name;

			if (stripos($declares, 'Kohana_') === 0)
			{
				// Remove "Kohana_"
				$declares = substr($declares, 7);
			}

			if ($declares === $_class->name OR $declares === "Core")
			{
				$methods[] = $_method->name;
			}
		}

		sort($methods);

		$classes[$_class->name] = $methods;
	}

	return $classes;
}

public static classes( [ array $list = NULL ] ) (defined in Kohana_Kodoc)

Returns an array of all the classes available, built by listing all files in the classes folder and then trying to create that class.

This means any empty class files (as in complety empty) will cause an exception

Parameters

  • array $list = NULL - Array of files, obtained using Kohana::list_files

Return Values

  • array - An array of all the class names

Source Code

public static function classes(array $list = NULL)
{
	if ($list === NULL)
	{
		$list = Kohana::list_files('classes');
	}

	$classes = array();

	// This will be used a lot!
	$ext_length = strlen(EXT);

	foreach ($list as $name => $path)
	{
		if (is_array($path))
		{
			$classes += Kodoc::classes($path);
		}
		elseif (substr($name, -$ext_length) === EXT)
		{
			// Remove "classes/" and the extension
			$class = substr($name, 8, -$ext_length);

			// Convert slashes to underscores
			$class = str_replace(DIRECTORY_SEPARATOR, '_', strtolower($class));

			$classes[$class] = $class;
		}
	}

	return $classes;
}

public static factory( ) (defined in Kohana_Kodoc)

Source Code

public static function factory($class)
{
	return new Kodoc_Class($class);
}

Make a class#member API link using an array of matches from Kodoc::$regex_class_member

Parameters

  • array $matches required - Array( 1 => link text, 2 => class name, [3 => member name] )

Return Values

  • string

Source Code

public static function link_class_member($matches)
{
	$link = $matches[1];
	$class = $matches[2];
	$member = NULL;

	if (isset($matches[3]))
	{
		// If the first char is a $ it is a property, e.g. Kohana::$base_url
		if ($matches[3][0] === '$')
		{
			$member = '#property:'.substr($matches[3], 1);
		}
		else
		{
			$member = '#'.$matches[3];
		}
	}

	return HTML::anchor(Route::get('docs/api')->uri(array('class' => $class)).$member, $link, NULL, NULL, TRUE);
}

Creates an html list of all classes sorted by category (or package if no category)

Return Values

  • string - The html for the menu

Source Code

public static function menu()
{
	$classes = Kodoc::classes();

	foreach ($classes as $class)
	{
		if (isset($classes['kohana_'.$class]))
		{
			// Remove extended classes
			unset($classes['kohana_'.$class]);
		}
	}

	ksort($classes);

	$menu = array();

	$route = Route::get('docs/api');

	foreach ($classes as $class)
	{
		$class = Kodoc_Class::factory($class);

		// Test if we should show this class
		if ( ! Kodoc::show_class($class))
			continue;

		$link = HTML::anchor($route->uri(array('class' => $class->class->name)), $class->class->name);

		if (isset($class->tags['package']))
		{
			foreach ($class->tags['package'] as $package)
			{
				if (isset($class->tags['category']))
				{
					foreach ($class->tags['category'] as $category)
					{
						$menu[$package][$category][] = $link;
					}
				}
				else
				{
					$menu[$package]['Base'][] = $link;
				}
			}
		}
		else
		{
			$menu['[Unknown]']['Base'][] = $link;
		}
	}

	// Sort the packages
	ksort($menu);

	return View::factory('userguide/api/menu')
		->bind('menu', $menu);
}

public static parse( string $comment ) (defined in Kohana_Kodoc)

Parse a comment to extract the description and the tags

Parameters

  • string $comment required - The comment retreived using ReflectionClass->getDocComment()

Return Values

  • array - Array(string $description, array $tags)

Source Code

public static function parse($comment)
{
	// Normalize all new lines to \n
	$comment = str_replace(array("\r\n", "\n"), "\n", $comment);

	// Remove the phpdoc open/close tags and split
	$comment = array_slice(explode("\n", $comment), 1, -1);

	// Tag content
	$tags = array();

	foreach ($comment as $i => $line)
	{
		// Remove all leading whitespace
		$line = preg_replace('/^\s*\* ?/m', '', $line);

		// Search this line for a tag
		if (preg_match('/^@(\S+)(?:\s*(.+))?$/', $line, $matches))
		{
			// This is a tag line
			unset($comment[$i]);

			$name = $matches[1];
			$text = isset($matches[2]) ? $matches[2] : '';

			switch ($name)
			{
				case 'license':
					if (strpos($text, '://') !== FALSE)
					{
						// Convert the lincense into a link
						$text = HTML::anchor($text);
					}
				break;
				case 'link':
					$text = preg_split('/\s+/', $text, 2);
					$text = HTML::anchor($text[0], isset($text[1]) ? $text[1] : $text[0]);
				break;
				case 'copyright':
					if (strpos($text, '(c)') !== FALSE)
					{
						// Convert the copyright sign
						$text = str_replace('(c)', '&copy;', $text);
					}
				break;
				case 'throws':
					if (preg_match('/^(\w+)\W(.*)$/', $text, $matches))
					{
						$text = HTML::anchor(Route::get('docs/api')->uri(array('class' => $matches[1])), $matches[1]).' '.$matches[2];
					}
					else
					{
						$text = HTML::anchor(Route::get('docs/api')->uri(array('class' => $text)), $text);
					}
				break;
				case 'uses':
					if (preg_match('/^'.Kodoc::$regex_class_member.'$/i', $text, $matches))
					{
						$text = Kodoc::link_class_member($matches);
					}
				break;
				// Don't show @access lines, they are shown elsewhere
				case 'access':
					continue 2;
			}

			// Add the tag
			$tags[$name][] = $text;
		}
		else
		{
			// Overwrite the comment line
			$comment[$i] = (string) $line;
		}
	}

	// Concat the comment lines back to a block of text
	if ($comment = trim(implode("\n", $comment)))
	{
		// Parse the comment with Markdown
		$comment = Kodoc_Markdown::markdown($comment);
	}

	return array($comment, $tags);
}

public static show_class( Kodoc_Class $class ) (defined in Kohana_Kodoc)

Test whether a class should be shown, based on the api_packages config option

Parameters

  • Kodoc_Class $class required - The class to test

Return Values

  • bool - Whether this class should be shown

Source Code

public static function show_class(Kodoc_Class $class)
{
	$api_packages = Kohana::$config->load('userguide.api_packages');

	// If api_packages is true, all packages should be shown
	if ($api_packages === TRUE)
		return TRUE;

	// Get the package tags for this class (as an array)
	$packages = Arr::get($class->tags, 'package', array('None'));

	$show_this = FALSE;

	// Loop through each package tag
	foreach ($packages as $package)
	{
		// If this package is in the allowed packages, set show this to true
		if (in_array($package, explode(',', $api_packages)))
			$show_this = TRUE;
	}

	return $show_this;
}

public static source( string $file , int $start , int $end ) (defined in Kohana_Kodoc)

Get the source of a function

Parameters

  • string $file required - The filename
  • int $start required - Start line?
  • int $end required - End line?

Source Code

public static function source($file, $start, $end)
{
	if ( ! $file) return FALSE;

	$file = file($file, FILE_IGNORE_NEW_LINES);

	$file = array_slice($file, $start - 1, $end - $start + 1);

	if (preg_match('/^(\s+)/', $file[0], $matches))
	{
		$padding = strlen($matches[1]);

		foreach ($file as & $line)
		{
			$line = substr($line, $padding);
		}
	}

	return implode("\n", $file);
}

protected _method_sort( ) (defined in Kohana_Kodoc_Class)

Sort methods based on their visibility and declaring class based on: - methods will be sorted public, protected, then private. - methods that are declared by an ancestor will be after classes declared by the current class - lastly, they will be sorted alphabetically

Source Code

protected function _method_sort($a, $b)
{
	// If one method is public, and the other is not, it goes on top
	if ($a->isPublic() AND ( ! $b->isPublic()))
		return -1;
	if ($b->isPublic() AND ( ! $a->isPublic()))
		return 1;
	
	// If one method is protected and the other is private, it goes on top
	if ($a->isProtected() AND $b->isPrivate())
		return -1;
	if ($b->isProtected() AND $a->isPrivate())
		return 1;
	
	// The methods have the same visibility, so check the declaring class depth:
	
	
	/*
	echo Debug::vars('a is '.$a->class.'::'.$a->name,'b is '.$b->class.'::'.$b->name,
					   'are the classes the same?', $a->class == $b->class,'if they are, the result is:',strcmp($a->name, $b->name),
					   'is a this class?', $a->name == $this->class->name,-1,
					   'is b this class?', $b->name == $this->class->name,1,
					   'otherwise, the result is:',strcmp($a->class, $b->class)
					   );
	*/

	// If both methods are defined in the same class, just compare the method names
	if ($a->class == $b->class)
		return strcmp($a->name, $b->name);

	// If one of them was declared by this class, it needs to be on top
	if ($a->name == $this->class->name)
		return -1;
	if ($b->name == $this->class->name)
		return 1;

	// Otherwise, get the parents of each methods declaring class, then compare which function has more "ancestors"
	$adepth = 0;
	$bdepth = 0;

	$parent = $a->getDeclaringClass();
	do
	{
		$adepth++;
	}
	while ($parent = $parent->getParentClass());

	$parent = $b->getDeclaringClass();
	do
	{
		$bdepth++;
	}
	while ($parent = $parent->getParentClass());

	return $bdepth - $adepth;
}

protected _prop_sort( ) (defined in Kohana_Kodoc_Class)

Source Code

protected function _prop_sort($a, $b)
{
	// If one property is public, and the other is not, it goes on top
	if ($a->isPublic() AND ( ! $b->isPublic()))
		return -1;
	if ($b->isPublic() AND ( ! $a->isPublic()))
		return 1;
	
	// If one property is protected and the other is private, it goes on top
	if ($a->isProtected() AND $b->isPrivate())
		return -1;
	if ($b->isProtected() AND $a->isPrivate())
		return 1;
	
	// Otherwise just do alphabetical
	return strcmp($a->name, $b->name);
}