Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 2 additions & 81 deletions bin/phpdoc-md
Original file line number Diff line number Diff line change
@@ -1,85 +1,6 @@
#!/usr/bin/env php
<?php namespace Clean\PhpDocMd;

use ReflectionClass;
<?php

require 'vendor/autoload.php';
$config = require '.phpdoc-md';

function getDestinationDirectory(ReflectionClass $reflection, $rootNamespace, $rootDir)
{
return namespaceToPath(
rtrim(
sprintf(
'%s/%s',
$rootDir,
removeRootNamespace($reflection->getNamespaceName(), $rootNamespace)
),
'/'
)
);
}

function namespaceToPath($namespace)
{
return str_replace(
'\\',
DIRECTORY_SEPARATOR,
$namespace
);
}

function removeRootNamespace($namespace, $root)
{
$re = preg_replace(
sprintf("/^%s/", addslashes($root)),
'',
$namespace
);
return ltrim($re, '\\');
}

function error($message, $exception = null) {
echo sprintf("ERROR: '%s'", trim($message));
exit(1);
}

switch ($config->format) {
default:
$readme = new Markdown\GitHub\Readme($config->rootNamespace);
}

foreach ($config->classes as $class) {
try {
$reflection = new ReflectionClass($class);
} catch(\ReflectionException $e) {
error('Config error: ' . $e->getMessage(), $e);
}
$destDir = getDestinationDirectory($reflection, $config->rootNamespace, $config->destDirectory);
$destFile = sprintf('%s.md', $reflection->getShortName());
switch ($config->format) {
case 'stash':
$markDown = new Markdown\Stash\ClassInfo($reflection);
break;
case 'github':
$markDown = new Markdown\GitHub\ClassInfo($reflection);
break;
default:
throw new \RuntimeException('Not supported markdown format given: ' . $config->format);

}

file_exists($destDir) ?: mkdir($destDir, 0777, true);
file_put_contents($destDir . DIRECTORY_SEPARATOR . $destFile, $markDown);
$readme->addLink(
removeRootNamespace($reflection->getName(), $config->rootNamespace),
namespaceToPath(
removeRootNamespace(
$reflection->getName(),
$config->rootNamespace
)
) . '.md'
);
}

file_put_contents($config->destDirectory . '/README.md', $readme);
(new Clean\PhpDocMd\PhpDocMdCommand())->execute();
105 changes: 105 additions & 0 deletions src/PhpDocMdCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?php namespace Clean\PhpDocMd;

use ReflectionClass;

class PhpDocMdCommand
{

private $configFile = '.phpdoc-md';
private $config;


public function __construct()
{
$this->config = require $this->configFile;

if (is_array($this->config)) {
$this->config = (object) $this->config;
}
}

public function execute()
{
switch ($this->config->format) {
default:
$readme = new Markdown\GitHub\Readme($this->config->rootNamespace);
}

foreach ($this->config->classes as $class) {
try {
$reflection = new ReflectionClass($class);
} catch (\ReflectionException $e) {
$this->error('Config error: ' . $e->getMessage(), $e);
}
$destDir = $this->getDestinationDirectory(
$reflection,
$this->config->rootNamespace,
$this->config->destDirectory
);
$destFile = sprintf('%s.md', $reflection->getShortName());
switch ($this->config->format) {
case 'stash':
$markDown = new Markdown\Stash\ClassInfo($reflection);
break;
case 'github':
$markDown = new Markdown\GitHub\ClassInfo($reflection);
break;
default:
throw new \RuntimeException('Not supported markdown format given: ' . $this->config->format);
}

file_exists($destDir) ?: mkdir($destDir, 0777, true);
file_put_contents($destDir . DIRECTORY_SEPARATOR . $destFile, $markDown);
$readme->addLink(
$this->removeRootNamespace($reflection->getName(), $this->config->rootNamespace),
$this->namespaceToPath(
$this->removeRootNamespace(
$reflection->getName(),
$this->config->rootNamespace
)
) . '.md'
);
}

file_put_contents($this->config->destDirectory . '/README.md', $readme);
}

protected function getDestinationDirectory(ReflectionClass $reflection, $rootNamespace, $rootDir)
{
return $this->namespaceToPath(
rtrim(
sprintf(
'%s/%s',
$rootDir,
$this->removeRootNamespace($reflection->getNamespaceName(), $rootNamespace)
),
'/'
)
);
}

protected function namespaceToPath($namespace)
{
return str_replace(
'\\',
DIRECTORY_SEPARATOR,
$namespace
);
}

protected function removeRootNamespace($namespace, $root)
{
$re = preg_replace(
sprintf("/^%s/", addslashes($root)),
'',
$namespace
);
return ltrim($re, '\\');
}

protected function error($message, $exception = null)
{
echo sprintf("ERROR: '%s'", trim($message));
exit(1);
}
}