Class documentation generator.
Class declared in MODPATH/userguide/classes/Kodoc/Class.php on line 3.
ReflectionClass
$classlink to thisThe ReflectionClass for this class
NULL
array
$constantslink to thisarray of this classes constants
array(0)
string
$descriptionlink to thisdescription of the class from the comment
NULL
string
$modifierslink to thismodifiers like abstract, final
NULL
array
$parentslink to thisParent classes/interfaces of this class/interface
array(0)
string
$regex_class_memberlink to thisPCRE fragment for matching 'Class', 'Class::method', 'Class::method()' or 'Class::$property'
string(33) "((\w++)(?:::(\$?\w++))?(?:\(\))?)"
array
$tagslink to thisarray of tags, retrieved from the comment
array(0)
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.
string
$class
required - Class name void
public
function
__construct(
$class
)
{
$this
->
class
=
new
ReflectionClass(
$class
);
if
(
$modifiers
=
$this
->
class
->getModifiers()) {
$this
->modifiers =
'<small>'
. implode(
' '
, Reflection::getModifierNames(
$modifiers
)) .
'</small> '
;
}
$this
->constants =
$this
->
class
->getConstants();
// 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
;
}
}
if
(!
$comment
=
$this
->
class
->getDocComment()) {
foreach
(
$this
->parents
as
$parent
) {
if
(
$comment
=
$parent
->getDocComment()) {
// Found a description for this class
break
;
}
}
}
list(
$this
->description,
$this
->tags) = Kodoc::parse(
$comment
, false);
}
Gets the constants of this class as HTML.
array
public
function
constants()
{
$result
= [];
foreach
(
$this
->constants
as
$name
=>
$value
) {
$result
[
$name
] = Debug::vars(
$value
);
}
return
$result
;
}
Get the description of this class as HTML. Includes a warning when the class or one of its parents could not be found.
string
- HTML
public
function
description()
{
$result
=
$this
->description;
// If this class extends Kodoc_Missing, add a warning about possible
// incomplete documentation
foreach
(
$this
->parents
as
$parent
) {
if
(
$parent
->name ==
'Kodoc_Missing'
) {
$result
.= "[!!] **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!**";
}
}
return
Kodoc_Markdown::markdown(
$result
);
}
Gets a list of the class properties as Kodoc_Method objects.
array
public
function
methods()
{
$methods
=
$this
->
class
->getMethods();
usort(
$methods
, [
$this
,
'_method_sort'
]);
foreach
(
$methods
as
$key
=>
$method
) {
$methods
[
$key
] =
new
Kodoc_Method(
$this
->
class
->name,
$method
->name);
}
return
$methods
;
}
Gets a list of the class properties as Kodoc_Property objects.
array
public
function
properties()
{
$props
=
$this
->
class
->getProperties();
$defaults
=
$this
->
class
->getDefaultProperties();
usort(
$props
, [
$this
,
'_prop_sort'
]);
foreach
(
$props
as
$key
=>
$property
) {
// Create Kodoc Properties for each property
$props
[
$key
] =
new
Kodoc_Property(
$this
->
class
->name,
$property
->name, Arr::get(
$defaults
,
$property
->name));
}
return
$props
;
}
Get the tags of this class as HTML.
array
public
function
tags()
{
$result
= [];
foreach
(
$this
->tags
as
$name
=>
$set
) {
foreach
(
$set
as
$text
) {
$result
[
$name
][] = Kodoc::format_tag(
$name
,
$text
);
}
}
return
$result
;
}
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
public
static
function
class_methods(
array
$list
= null)
{
$list
= Kodoc::classes(
$list
);
$classes
= [];
foreach
(
$list
as
$class
) {
// Skip transparent extension classes
if
(Kodoc::is_transparent(
$class
))
continue
;
$_class
=
new
ReflectionClass(
$class
);
$methods
= [];
foreach
(
$_class
->getMethods()
as
$_method
) {
$declares
=
$_method
->getDeclaringClass()->name;
// Remove the transparent prefix from declaring classes
if
(
$child
= Kodoc::is_transparent(
$declares
)) {
$declares
=
$child
;
}
if
(
$declares
===
$_class
->name OR
$declares
===
"Core"
) {
$methods
[] =
$_method
->name;
}
}
sort(
$methods
);
$classes
[
$_class
->name] =
$methods
;
}
return
$classes
;
}
Returns an array of all the classes available, built by listing all files in the classes folder.
array
$list
= NULL - Array of files, obtained using Kohana::list_files array
- An array of all the class names
public
static
function
classes(
array
$list
= null)
{
if
(
$list
=== null) {
$list
= Kohana::list_files(
'classes'
);
}
$classes
= [];
// 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,
'_'
,
$class
);
$classes
[
$class
] =
$class
;
}
}
return
$classes
;
}
public
static
function
factory(
$class
)
{
return
new
Kodoc_Class(
$class
);
}
Generate HTML for the content of a tag.
string
$tag
required - Name of the tag without @ string
$text
required - Content of the tag string
- HTML
public
static
function
format_tag(
$tag
,
$text
)
{
if
(
$tag
===
'license'
) {
if
(
strpos
(
$text
,
'://'
) !== false)
return
HTML::anchor(
$text
);
}
elseif
(
$tag
===
'link'
) {
$split
= preg_split(
'/\s+/'
,
$text
, 2);
return
HTML::anchor(
$split
[0], isset(
$split
[1]) ?
$split
[1] :
$split
[0]
);
}
elseif
(
$tag
===
'copyright'
) {
// Convert the copyright symbol
return
str_replace
(
'(c)'
,
'©'
,
$text
);
}
elseif
(
$tag
===
'throws'
) {
$route
= Route::get(
'docs/api'
);
if
(preg_match(
'/^(\w+)\W(.*)$/D'
,
$text
,
$matches
)) {
return
HTML::anchor(
$route
->uri([
'class'
=>
$matches
[1]]),
$matches
[1]
) .
' '
.
$matches
[2];
}
return
HTML::anchor(
$route
->uri([
'class'
=>
$text
]),
$text
);
}
elseif
(
$tag
===
'see'
OR
$tag
===
'uses'
) {
if
(preg_match(
'/^'
. Kodoc::
$regex_class_member
.
'/'
,
$text
,
$matches
))
return
Kodoc::link_class_member(
$matches
);
}
return
$text
;
}
Checks whether a class is a transparent extension class or not.
This method takes an optional $classes parameter, a list of all defined class names. If provided, the method will return false unless the extension class exists. If not, the method will only check known transparent class prefixes.
Transparent prefixes are defined in the userguide.php config file:
'transparent_prefixes'
=> [
'Kohana'
=> true,
];
Module developers can therefore add their own transparent extension namespaces and exclude them from the userguide.
string
$class
required - The name of the class to check for transparency array
$classes
= NULL - An optional list of all defined classes false
- If this is not a transparent extension classstring
- The name of the class that extends this (in the case provided)
public
static
function
is_transparent(
$class
,
$classes
= null)
{
static
$transparent_prefixes
= null;
if
(!
$transparent_prefixes
) {
$transparent_prefixes
= Kohana::
$config
->load(
'userguide.transparent_prefixes'
);
}
// Split the class name at the first underscore
$segments
=
explode
(
'_'
,
$class
, 2);
if
((
count
(
$segments
) == 2) AND ( isset(
$transparent_prefixes
[
$segments
[0]]))) {
if
(
$segments
[1] ===
'Core'
) {
// Cater for Module extends Module_Core naming
$child_class
=
$segments
[0];
}
else
{
// Cater for Foo extends Module_Foo naming
$child_class
=
$segments
[1];
}
// It is only a transparent class if the unprefixed class also exists
if
(
$classes
AND ! isset(
$classes
[
$child_class
]))
return
false;
// Return the name of the child class
return
$child_class
;
}
else
{
// Not a transparent class
return
false;
}
}
Make a class#member API link using an array of matches from Kodoc::$regex_class_member
array
$matches
required - [1 => 'link text', 2 => 'class name', [3 => 'member name']] string
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);
}
elseif
(preg_match(
'/^[A-Z_\x7f-\xff][A-Z0-9_\x7f-\xff]*$/'
,
$matches
[3])) {
$member
=
'#constant:'
.
substr
(
$matches
[3], 0);
}
else
{
$member
=
'#'
.
$matches
[3];
}
}
return
HTML::anchor(Route::get(
'docs/api'
)->uri([
'class'
=>
$class
]) .
$member
,
$link
, null, null, true);
}
Creates an html list of all classes sorted by category (or package if no category)
string
- The html for the menu
public
static
function
menu()
{
$classes
= Kodoc::classes();
ksort(
$classes
);
$menu
= [];
$route
= Route::get(
'docs/api'
);
foreach
(
$classes
as
$class
) {
if
(Kodoc::is_transparent(
$class
,
$classes
))
continue
;
$class
= Kodoc_Class::factory(
$class
);
// Test if we should show this class
if
(!Kodoc::show_class(
$class
))
continue
;
$link
= HTML::anchor(
$route
->uri([
'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
);
}
Parse a comment to extract the description and the tags
Converting the output to HTML in this method is deprecated in 3.3
string
$comment
required - The DocBlock to parse boolean
$html
= bool TRUE - Whether or not to convert the return values
to HTML (deprecated) array
- [string $description, array $tags]
public
static
function
parse(
$comment
,
$html
= true)
{
// Normalize all new lines to \n
$comment
=
str_replace
([
"\r\n"
,
"\n"
],
"\n"
,
$comment
);
// Split into lines while capturing without leading whitespace
preg_match_all(
'/^\s*\* ?(.*)\n/m'
,
$comment
,
$lines
);
// Tag content
$tags
= [];
/**
* Process a tag and add it to $tags
*
* @param string $tag Name of the tag without @
* @param string $text Content of the tag
* @return void
*/
$add_tag
=
function
(
$tag
,
$text
)
use
(
$html
, &
$tags
) {
// Don't show @access lines, they are shown elsewhere
if
(
$tag
!==
'access'
) {
if
(
$html
) {
$text
= Kodoc::format_tag(
$tag
,
$text
);
}
// Add the tag
$tags
[
$tag
][] =
$text
;
}
};
$comment
=
$tag
= null;
$end
=
count
(
$lines
[1]) - 1;
foreach
(
$lines
[1]
as
$i
=>
$line
) {
// Search this line for a tag
if
(preg_match(
'/^@(\S+)\s*(.+)?$/'
,
$line
,
$matches
)) {
if
(
$tag
) {
// Previous tag is finished
$add_tag
(
$tag
,
$text
);
}
$tag
=
$matches
[1];
$text
= isset(
$matches
[2]) ?
$matches
[2] :
''
;
if
(
$i
===
$end
) {
// No more lines
$add_tag
(
$tag
,
$text
);
}
}
elseif
(
$tag
) {
// This is the continuation of the previous tag
$text
.=
"\n"
.
$line
;
if
(
$i
===
$end
) {
// No more lines
$add_tag
(
$tag
,
$text
);
}
}
else
{
$comment
.=
"\n"
.
$line
;
}
}
$comment
= trim(
$comment
,
"\n"
);
if
(
$comment
AND
$html
) {
// Parse the comment with Markdown
$comment
= Kodoc_Markdown::markdown(
$comment
);
}
return
[
$comment
,
$tags
];
}
Test whether a class should be shown, based on the api_packages config option
Kodoc_Class
$class
required - The class to test bool
- Whether this class should be shown
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'
, [
'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
;
}
Get the source of a function
string
$file
required - The filename int
$start
required - Start line? int
$end
required - End line?
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
);
}
Sort methods based on their visibility and declaring class based on:
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
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);
}