Sami: an API documentation generator

Related tags

Documentation Sami
Overview

Sami: an API documentation generator

WARNING: Sami is not supported nor maintained anymore. Feel free to fork.

Curious about what Sami generates? Have a look at the Symfony API.

Installation

Caution!

Sami requires PHP 7.

Get Sami as a phar file:

$ curl -O http://get.sensiolabs.org/sami.phar

Check that everything worked as expected by executing the sami.phar file without any arguments:

$ php sami.phar

Note

Installing Sami as a regular Composer dependency is NOT supported. Sami is a tool, not a library. As such, it should be installed as a standalone package, so that Sami's dependencies do not interfere with your project's dependencies.

Configuration

Before generating documentation, you must create a configuration file. Here is the simplest possible one:



return new Sami\Sami('/path/to/symfony/src');

The configuration file must return an instance of Sami\Sami and the first argument of the constructor is the path to the code you want to generate documentation for.

Actually, instead of a directory, you can use any valid PHP iterator (and for that matter any instance of the Symfony Finder class):



use Sami\Sami;
use Symfony\Component\Finder\Finder;

$iterator = Finder::create()
    ->files()
    ->name('*.php')
    ->exclude('Resources')
    ->exclude('Tests')
    ->in('/path/to/symfony/src')
;

return new Sami($iterator);

The Sami constructor optionally takes an array of options as a second argument:

return new Sami($iterator, array(
    'theme'                => 'symfony',
    'title'                => 'Symfony2 API',
    'build_dir'            => __DIR__.'/build',
    'cache_dir'            => __DIR__.'/cache',
    'remote_repository'    => new GitHubRemoteRepository('username/repository', '/path/to/repository'),
    'default_opened_level' => 2,
));

And here is how you can configure different versions:



use Sami\Sami;
use Sami\RemoteRepository\GitHubRemoteRepository;
use Sami\Version\GitVersionCollection;
use Symfony\Component\Finder\Finder;

$iterator = Finder::create()
    ->files()
    ->name('*.php')
    ->exclude('Resources')
    ->exclude('Tests')
    ->in($dir = '/path/to/symfony/src')
;

// generate documentation for all v2.0.* tags, the 2.0 branch, and the master one
$versions = GitVersionCollection::create($dir)
    ->addFromTags('v2.0.*')
    ->add('2.0', '2.0 branch')
    ->add('master', 'master branch')
;

return new Sami($iterator, array(
    'theme'                => 'symfony',
    'versions'             => $versions,
    'title'                => 'Symfony2 API',
    'build_dir'            => __DIR__.'/../build/sf2/%version%',
    'cache_dir'            => __DIR__.'/../cache/sf2/%version%',
    'remote_repository'    => new GitHubRemoteRepository('symfony/symfony', dirname($dir)),
    'default_opened_level' => 2,
));

To generate documentation for a PHP 5.2 project, simply set the simulate_namespaces option to true.

You can find more configuration examples under the examples/ directory of the source code.

Sami only documents the public API (public properties and methods); override the default configured filter to change this behavior:



use Sami\Parser\Filter\TrueFilter;

$sami = new Sami(...);
// document all methods and properties
$sami['filter'] = function () {
    return new TrueFilter();
};

Rendering

Now that we have a configuration file, let's generate the API documentation:

$ php sami.phar update /path/to/config.php

The generated documentation can be found under the configured build/ directory (note that the client side search engine does not work on Chrome due to JavaScript execution restriction, unless Chrome is started with the "--allow-file-access-from-files" option -- it works fine in Firefox).

By default, Sami is configured to run in "incremental" mode. It means that when running the update command, Sami only re-generates the files that needs to be updated based on what has changed in your code since the last execution.

Sami also detects problems in your phpdoc and can tell you what you need to fix if you add the -v option:

$ php sami.phar update /path/to/config.php -v

Creating a Theme

If the default themes do not suit your needs, you can very easily create a new one, or just override an existing one.

A theme is just a directory with a manifest.yml file that describes the theme (this is a YAML file):

name:   symfony
parent: default

The above configuration creates a new symfony theme based on the default built-in theme. To override a template, just create a file with the same name as the original one. For instance, here is how you can extend the default class template to prefix the class name with "Class " in the class page title:

{# pages/class.twig #}

{% extends 'default/pages/class.twig' %}

{% block title %}Class {{ parent() }}{% endblock %}

If you are familiar with Twig, you will be able to very easily tweak every aspect of the templates as everything has been well isolated in named Twig blocks.

A theme can also add more templates and static files. Here is the manifest for the default theme:

name: default

static:
    'css/sami.css': 'css/sami.css'
    'css/bootstrap.min.css': 'css/bootstrap.min.css'
    'css/bootstrap-theme.min.css': 'css/bootstrap-theme.min.css'
    'fonts/glyphicons-halflings-regular.eot': 'fonts/glyphicons-halflings-regular.eot'
    'fonts/glyphicons-halflings-regular.svg': 'fonts/glyphicons-halflings-regular.svg'
    'fonts/glyphicons-halflings-regular.ttf': 'fonts/glyphicons-halflings-regular.ttf'
    'fonts/glyphicons-halflings-regular.woff': 'fonts/glyphicons-halflings-regular.woff'
    'js/bootstrap.min.js': 'js/bootstrap.min.js'
    'js/jquery-1.11.1.min.js': 'js/jquery-1.11.1.min.js'
    'js/handlebars.min.js': 'js/handlebars.min.js'
    'js/typeahead.min.js': 'js/typeahead.min.js'

global:
    'index.twig':      'index.html'
    'doc-index.twig':  'doc-index.html'
    'namespaces.twig': 'namespaces.html'
    'classes.twig':    'classes.html'
    'interfaces.twig': 'interfaces.html'
    'traits.twig':     'traits.html'
    'opensearch.twig': 'opensearch.xml'
    'search.twig':     'search.html'
    'sami.js.twig':    'sami.js'

namespace:
    'namespace.twig': '%s.html'

class:
    'class.twig': '%s.html'

Files are contained into sections, depending on how Sami needs to treat them:

  • static: Files are copied as is (for assets like images, stylesheets, or JavaScript files);
  • global: Templates that do not depend on the current class context;
  • namespace: Templates that should be generated for every namespace;
  • class: Templates that should be generated for every class.

Search Index

The autocomplete and search functionality of Sami is provided through a search index that is generated based on the classes, namespaces, interfaces, and traits of a project. You can customize the search index by overriding the search_index_extra block of sami.js.twig.

The search_index_extra allows you to extend the default theme and add more entries to the index. For example, some projects implement magic methods that are dynamically generated at runtime. You might wish to document these methods while generating API documentation and add them to the search index.

Each entry in the search index is a JavaScript object that contains the following keys:

type
The type associated with the entry. Built-in types are "Class", "Namespace", "Interface", "Trait". You can add additional types specific to an application, and the type information will appear next to the search result.
name
The name of the entry. This is the element in the index that is searchable (e.g., class name, namespace name, etc).
fromName
The parent of the element (if any). This can be used to provide context for the entry. For example, the fromName of a class would be the namespace of the class.
fromLink
The link to the parent of the entry (if any). This is used to link a child to a parent. For example, this would be a link from a class to the class namespace.
doc
A short text description of the entry.

One such example of when overriding the index is useful could be documenting dynamically generated API operations of a web service client. Here's a simple example that adds dynamically generated API operations for a web service client to the search index:

{% extends "default/sami.js.twig" %}

{% block search_index_extra %}
    {% for operation in operations -%}
        {"type": "Operation", "link": "{{ operation.path }}", "name": "{{ operation.name }}", "doc": "{{ operation.doc }}"},
    {%- endfor %}
{% endblock %}

This example assumes that the template has a variable operations available which contains an array of operations.

Note

Always include a trailing comma for each entry you add to the index. Sami will take care of ensuring that trailing commas are handled properly.

Comments
  • Adding a theme based on twitter bootstrap not using frames

    Adding a theme based on twitter bootstrap not using frames

    This commit adds a bootstrap 3 powered theme to Sami that does not use frames.

    Closes #119, #122, #47, #13, #123.

    This pull request adds a new theme that is completely rewritten to be simpler, not use frames, and is now responsive (i.e., looks good on a phone). I've re-implemented the left-navigation using a much simpler approach, created a new search implementation that allows users to hook in custom search result types by extending the theme, and added breadcrumbs. I tried to keep a relatively similar look and feel to the existing Sami theme, but with a bit of twitter bootstrap on top.

    Because this provides the same features of the default theme and includes additional features, I would recommend making this the default theme and removing the "enhanced" theme. If you'd like me to do this, then let me know and I'll rebase with those changes.

    Here are some screenshots and descriptions of what they do.

    Namespaces

    This is the namespaces page. It just lists each namespace. This is also the homepage if a project has classes.

    screen shot 2014-08-27 at 9 05 35 pm

    Here is is at a smaller width:

    screen shot 2014-08-27 at 9 17 16 pm

    Note: the button at the top provides a drop down containing the menu.

    Classes

    Here is the "classes" page, full-screen.

    screen shot 2014-08-27 at 9 08 02 pm

    And again, small screen:

    screen shot 2014-08-27 at 9 08 19 pm

    Note: Traits, and interfaces look that same as this page.

    Index page (doc-index)

    screen shot 2014-08-27 at 9 09 12 pm

    This looks good on a phone too:

    screen shot 2014-08-27 at 9 09 53 pm

    Interface

    Here's an interface page. Just like with class and namespace pages, the left-nav is scrolled down to show the currently selected node.

    screen shot 2014-08-27 at 9 10 35 pm

    Here are the start of the methods:

    screen shot 2014-08-27 at 9 11 36 pm

    Namespace page

    Here's a namespace page. It lists the sub-namespaces, the classes, traits, interfaces, etc.

    screen shot 2014-08-27 at 9 12 02 pm

    Search page

    When you submit a search query, it takes you to this page.

    screen shot 2014-08-27 at 9 12 57 pm

    The search looks through the names of each thing in the search index. The search index contains each namespace, class, interface, trait, and methods from each of these.

    opened by mtdowling 41
  • Fix issue #158

    Fix issue #158

    When building the documentation of a project defining a ConsoleExceptionListener (like described in http://symfony.com/doc/current/cookbook/console/logging.html#enabling-automatic-exceptions-logging) we got this issue:

    Version master
      Rendering ############################################# 91%     
                Class PTC\CoreBundle\Listener\ConsoleExceptionListener
    PHP Fatal error:  Class 'Symfony\Component\EventDispatcher\Event' not found in phar:///.../sami.phar/vendor/symfony/console/Event/ConsoleEvent.php on line 25
    

    By adding a dependency to "symfony/event-dispatcher", there is no more issue. Maybe not the best way to fix the issue as not directly needed by Sami but it does the trick as a tempoary solution? See https://github.com/FriendsOfPHP/Sami/issues/158 for more details.

    opened by raziel057 33
  • Adding

    Adding "View Source" link to the class page

    This is an implementation of #63 task based on original commit made in #64.

    _Usage_ By default Sami doesn't know on which hosting Git repository is located and doesn't add View Source links on class detail pages. However if you specify remote_repository option with instance of AbstractRemoteRepository class sub-class, then the View Source link would be added:

    sami_viewsourcelink

    For example:

    array(
    ...
    'remote_repository' => new GitHubRemoteRepository('username/repository', '/path/to/repository'),
    ...
    )
    

    The '/path/to/repository' is required, because Sami doesn't know base folder on GitHub (or any other service), where original files (for which documentation is being generated) live.

    Thanks to @anlutro, which commit (squashed) was used as a base for my changes.

    Closes #63

    opened by aik099 13
  • Link to source - sample implementation

    Link to source - sample implementation

    Hopefully at least some of this code is useful. See #63

    By passing source_url and source_dir to the Sami config, with this PR you can get links to view source for Github. For example, if your source files are in myproject/src, you'd pass myproject (omitting src) as the source_dir and github.com/myuser/myproject as source_url.

    For github it'll use tree/ for the url - this probably isn't very flexible but not sure how it can be improved quite yet.

    opened by anlutro 10
  • FIX parsing of @property tags

    FIX parsing of @property tags

    Closes #114.

    Property Tags are now parsed in much the same way that @param tags on methods are parsed. This way, they can be properly linked and translated to FQCN in the generated documentation.

    opened by Christoph-Harms 9
  • Don't update Composer on each buld

    Don't update Composer on each buld

    Currently at least 1 of builds in build matrix fails because of composer self-update command in the .travis.yml. See https://travis-ci.org/fabpot/Sami/jobs/30440724

    I've removed it since I haven't witnessed a case, that even month old composer.phar has created problems during a build.

    opened by aik099 9
  • Make Sami skip anonymous classes (fixes #294 and #252)

    Make Sami skip anonymous classes (fixes #294 and #252)

    As discussed on issues #294 and #252, there's a bug in Sami that prevents it generating documentations when it reaches anonymous classes nodes - a feature from PHP 7.x.

    As it was never supported before and I'm not sure if it's reasonable to have support for that I'm just proposing this PR to skip those anonymous classes and let Sami works flawlessly as before.

    opened by paulofreitas 7
  • Now printing class name on method error

    Now printing class name on method error

    fixes #147

    When a method error was printed, the MethodReflection did not have a ClassReflection associated with it. Now, the classname is printed as part of the error, because the class is set on the MethodReflection

    Signed-off-by: Martin Schenck [email protected]

    opened by schemar 7
  • Optionally allow nikic/php-parser ~2.0 as dependency

    Optionally allow nikic/php-parser ~2.0 as dependency

    This is related to #217. I encountered conflicts in the attempt to install Sami with a fresh Laravel 5.3 setup.

    I wasn't sure what to do with the composer.lock in this repo so I left it untouched.

    opened by mzur 6
  • Showing namespaces on each namespace page.

    Showing namespaces on each namespace page.

    This commit builds on top of PR https://github.com/fabpot/Sami/pull/13, but it has been updated for recent changes to Sami. In addition to some minor tweaks, this PR shows child namespaces as a comma separated list of namespaces rather than in a table to take up less space. This allows users to see classes and interfaces on a page without having to scroll down past the fold (providing a better user experience IMO).

    Closes #13. Closes #119.

    opened by mtdowling 6
  • build cache broke source link between versions

    build cache broke source link between versions

    When using remote_repository conf to provide links to gitlab or github source, a class that didn't change from previous version has the same link (that point to previous version).

    So, cache should be disabled in this case.

    opened by gza 5
Owner
null
Daux.io is an documentation generator that uses a simple folder structure and Markdown files to create custom documentation on the fly. It helps you create great looking documentation in a developer friendly way.

Daux.io - Deprecation Notice This repository is deprecated! Daux.io has been moved to an organization, to guarantee future development and support. So

Justin Walsh 4.6k Dec 16, 2022
Daux.io is an documentation generator that uses a simple folder structure and Markdown files to create custom documentation on the fly. It helps you create great looking documentation in a developer friendly way.

Daux.io Daux.io is a documentation generator that uses a simple folder structure and Markdown files to create custom documentation on the fly. It help

Daux.io 719 Jan 1, 2023
An API documentation generator

Sami: an API documentation generator WARNING: Sami is not supported nor maintained anymore. Feel free to fork. Curious about what Sami generates? Have

null 2k Dec 17, 2022
Documentation Generator for PHP

phpDocumentor What is phpDocumentor? phpDocumentor is an application that is capable of analyzing your PHP source code and DocBlock comments to genera

phpDocumentor 3.7k Dec 28, 2022
Documentation generator for PHP Code using standard technology (SRC, DOCBLOCK, XML and XSLT)

phpDox phpDox is a documentation generator for PHP projects. This includes, but is not limited to, API documentation. The main focus is on enriching t

Arne Blankerts 588 Dec 22, 2022
Documentation Generator for WordPress.

Pronamic WordPress Documentor is a tool to automatically extract data about the actions and filters of your WordPress theme or plugin.

Pronamic 36 Dec 8, 2022
Generates documentation for your REST API from annotations

NelmioApiDocBundle The NelmioApiDocBundle bundle allows you to generate a decent documentation for your APIs. Migrate from 3.x to 4.0 To migrate from

Nelmio 2.1k Jan 6, 2023
Generate interactive OpenAPI documentation for your RESTful API using doctrine annotations

Generate interactive OpenAPI documentation for your RESTful API using doctrine annotations

Robert Allen 4.6k Jan 6, 2023
phpDocumentor is an application that is capable of analyzing your PHP source code and DocBlock comments to generate a complete set of API Documentation

phpDocumentor What is phpDocumentor? phpDocumentor is an application that is capable of analyzing your PHP source code and DocBlock comments to genera

phpDocumentor 3.7k Jan 3, 2023
PHP 7.1 ready Smart and Simple Documentation for your PHP project

Smart and Readable Documentation for your PHP project ApiGen is the simplest, the easiest to use and the most modern api doc generator. It is all PHP

ApiGen 2.1k Dec 22, 2022
PHP 7.1 ready Smart and Simple Documentation for your PHP project

Smart and Readable Documentation for your PHP project ApiGen is the simplest, the easiest to use and the most modern api doc generator. It is all PHP

ApiGen 2.1k Apr 20, 2021
Generate Sphinx/ReStructured documentation from PHPDoc

PHP-phpdoc-to-rst Forked from Francesco "Abbadon1334" Danti by AeonDigital. In this fork the changes has only visual and superficial effects [ just be

Rianna Cantarelli 0 Nov 16, 2021
Simple and effective multi-format Web API Server to host your PHP API as Pragmatic REST and / or RESTful API

Luracast Restler ![Gitter](https://badges.gitter.im/Join Chat.svg) Version 3.0 Release Candidate 5 Restler is a simple and effective multi-format Web

Luracast 1.4k Jan 2, 2023
A PHP framework foucs on API fast development.接口,从简单开始!PhalApi简称π框架,一个轻量级PHP开源接口框架,专注于接口服务开发。

PhalApi开源接口框架 / PhalApi API Framework 读音:派框架 Stargazers over time 开发文档 / Documents 专为PHPer准备的优雅而详细的开发文档,请看:PhalApi 2.x 开发文档。 PhalApi 2.x English Docs.

dogstar 1.5k Dec 30, 2022
Yii2 Annotations Generate API Document Extension

Generate online api document by code annotation for yii2 easily

Trevor 16 Dec 15, 2021
Sami: an API documentation generator

Sami: an API documentation generator WARNING: Sami is not supported nor maintained anymore. Feel free to fork. Curious about what Sami generates? Have

null 2k Dec 17, 2022
Daux.io is an documentation generator that uses a simple folder structure and Markdown files to create custom documentation on the fly. It helps you create great looking documentation in a developer friendly way.

Daux.io - Deprecation Notice This repository is deprecated! Daux.io has been moved to an organization, to guarantee future development and support. So

Justin Walsh 4.6k Dec 16, 2022
Daux.io is an documentation generator that uses a simple folder structure and Markdown files to create custom documentation on the fly. It helps you create great looking documentation in a developer friendly way.

Daux.io Daux.io is a documentation generator that uses a simple folder structure and Markdown files to create custom documentation on the fly. It help

Daux.io 719 Jan 1, 2023
An API documentation generator

Sami: an API documentation generator WARNING: Sami is not supported nor maintained anymore. Feel free to fork. Curious about what Sami generates? Have

null 2k Dec 17, 2022
Laravel API Documentation Generator

Laravel API Documentation Generator Automatically generate your API documentation from your existing Laravel/Lumen/Dingo routes. php artisan apidoc:ge

Marcel Pociot 3.3k Dec 21, 2022