diff --git a/bin/phpdoc-md b/bin/phpdoc-md index 1a733e8..e6fe830 100755 --- a/bin/phpdoc-md +++ b/bin/phpdoc-md @@ -1,85 +1,6 @@ #!/usr/bin/env php -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(); diff --git a/src/PhpDocMdCommand.php b/src/PhpDocMdCommand.php new file mode 100755 index 0000000..35b103a --- /dev/null +++ b/src/PhpDocMdCommand.php @@ -0,0 +1,105 @@ +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); + } +}