製作功能摘要表

edited 十月 2013 in CakePHP
製作的功能稍微多一點之後,腦子的記憶就不太靈光了,所以需要製作一些文件幫忙記著;但是手動剪貼每個功能是個很煩人的操作,所以能夠透過工具產生是最好不過的。雖然可以透過 phpDocumentor 產生漂亮的文件,但有時候並不想要那麼多資訊,所以就製作了下面一個簡單的程式,透過 Console 執行後就可以產生一個目前專案的功能清單,接著再進一步去填寫資訊即可。

這只是個小程式,沒有考慮的很完整,也許有些地方需要依據自己的環境調整,僅供參考 ;)
<?php

class DocShell extends Shell {

    function main() {
        $fh = fopen(TMP . 'list.csv', 'w');
        $viewPath = ROOT . 'View';
        foreach (glob(ROOT . 'Controller/*') AS $controllerFile) {
            include_once $controllerFile;
            $pos1 = strrpos($controllerFile, 'Controller.php');
            $pos2 = strrpos($controllerFile, '/');
            $controllerName = substr($controllerFile, $pos2 + 1, $pos1 - $pos2 - 1);
            if ($controllerName !== 'App') {
                $controllerUrl = Inflector::underscore($controllerName);
                $controllerMethods = $this->getDeclaredMethods($controllerName . 'Controller');
                foreach($controllerMethods AS $controllerMethod => $info) {
                    if('beforeFilter' === $controllerMethod) continue;
                    $viewFile = "{$viewPath}/{$controllerName}/{$controllerMethod}.ctp";
                    $methodUrl = '/';
                    $admin = false;
                    if(false !== strpos($controllerMethod, 'admin_')) {
                        $admin = true;
                        $controllerMethod = substr($controllerMethod, 6);
                    }
                    if($admin) {
                        $methodUrl .= 'admin/';
                    }
                    $methodUrl .= $controllerUrl . '/' . $controllerMethod;
                    if(file_exists($viewFile)) {
                        $viewFile = substr($viewFile, 31);
                    } else {
                        $viewFile = '';
                    }
                    fputcsv($fh, array(
                        'url' => $methodUrl,
                        'cFile' => substr($controllerFile, 31),
                        'lineStart' => $info['lineStart'],
                        'lineEnd' => $info['lineEnd'],
                        'parameters' => implode(',', $info['parameters']),
                        'viewFile' => $viewFile,
                        'tableName' => Inflector::tableize($controllerName),
                    ));
                }
            }
        }
        fclose($fh);
    }

    function getDeclaredMethods($className) {
        $reflector = new ReflectionClass($className);
        $methodNames = array();
        $lowerClassName = strtolower($className);
        foreach ($reflector->getMethods(ReflectionMethod::IS_PUBLIC) as $method) {
            if (strtolower($method->class) == $lowerClassName) {
                $parameters = $method->getParameters();
                $methodNames[$method->name] = array(
                    'lineStart' => $method->getStartLine(),
                    'lineEnd' =>  $method->getEndLine(),
                );
                $methodNames[$method->name]['parameters'] = array();
                if(!empty($parameters)) {
                    foreach($parameters AS $parameter) {
                        $methodNames[$method->name]['parameters'][] = $parameter->name;
                    }
                }
            }
        }
        return $methodNames;
    }

}

操作環境: CakePHP 2.3.10

原始討論: http://twpug.net/x/modules/newbb/viewtopic.php?topic_id=7690
Sign In or Register to comment.