Build.class.php 16.7 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2014 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
namespace Think;

/**
 * 用于ThinkPHP的自动生成
 */
class Build
{

    /**
     * 控制层模板
     */
    protected static $controller = '<?php
/**
 * [CONTROLLER][CNAME].class.php
 * $author$
 */

namespace [MODULE]\[CNAME_CPATH];

class [CONTROLLER][CNAME] extends [BASE_CONTROLLER]
{

    public function [DEFAULT_ACTION]()
    {

        $this->show(\'[[CONTROLLER][CNAME]->[DEFAULT_ACTION]]\');
        $this->_output("[TPL_PATH]");
    }
}
';

    /**
     * Abstract 的方法
     */
    protected static $abstract_controller = '<?php
/**
 * [CONTROLLER][CNAME].class.php
 * $author$
 */

namespace [MODULE]\[CNAME_CPATH];

abstract class [CONTROLLER][CNAME] extends [BASE_CONTROLLER]
{

    public function before_action($action = \'\')
    {

        return parent::before_action($action);
    }

    public function after_action($action = \'\')
    {

        return parent::after_action($action);
    }
}
';

    /**
     * Model 层模板
     */
    protected static $model = '<?php
/**
 * [MODEL]Model.class.php
 * $author$
 */

namespace [MODULE]\Model;

class [MODEL]Model extends AbstractModel
{

    /**
     * 构造方法
     */
    public function __construct()
    {

        parent::__construct();
    }
}
';

    /**
     * Model 层基类模板
     */
    protected static $abstract_model = '<?php
/**
 * [MODEL]Model.class.php
 * $author$
 */

namespace [MODULE]\Model;

abstract class [MODEL]Model extends \Common\Model\AbstractModel
{

    /**
     * 构造方法
     */
    public function __construct()
    {

        parent::__construct();
    }
}
';

    /**
     * Service 层模板
     */
    protected static $service = '<?php
/**
 * [MODEL]Service.class.php
 * $author$
 */

namespace [MODULE]\Service;

class [MODEL]Service extends AbstractService
{

    /**
     * 构造方法
     */
    public function __construct()
    {

        parent::__construct();
        $this->_d = D("[MODULE]/[MODEL]");
    }
}
';

    /**
     * Service 层基类模板
     */
    protected static $abstract_service = '<?php
/**
 * [MODEL]Service.class.php
 * $author$
 */

namespace [MODULE]\Service;

abstract class [MODEL]Service extends \Common\Service\AbstractService
{

    /**
     * 构造方法
     */
    public function __construct()
    {

        parent::__construct();
    }
}
';

    /**
     * 操作层方法模板
     */
    protected static $action = '

    /**
     * [ACTION_NAME]
     */
    public function [ACTION_NAME]()
    {

        $this->_output("[TPL_PATH]");
    }
';

    /**
     * 操作层方法模板
     */
    protected static $action_api = '

    /**
     * [ACTION_NAME]
     */
    public function [ACTION_NAME]()
    {

        return true;
    }
';

    /**
     * View 层模板
     */
    protected static $template = '<include file="Header" />
body
<include file="Footer" />
';

    /**
     * View 层错误模板
     */
    protected static $error_tpl = '<include file="Common@Frontend/Error" />
';

    /**
     * 检测应用目录是否需要自动创建
     *
     * @param string $module
     */
    static public function checkDir($module)
    {

        if (!is_dir(APP_PATH . $module)) {
            // 创建模块的目录结构
            self::buildAppDir($module);
        } elseif (!is_dir(LOG_PATH)) {
            // 检查缓存目录
            self::buildRuntime();
        }
    }

    /**
     * 创建应用和模块的目录结构
     *
     * @param string $module
     */
    static public function buildAppDir($module)
    {

        // 没有创建的话自动创建
        if (!is_dir(APP_PATH)) {
            mkdir(APP_PATH, 0755, true);
        }
        if (is_writeable(APP_PATH)) {
            $c_layer = ucfirst(C('DEFAULT_C_LAYER'));
            $dirs = array(
                COMMON_PATH,
                COMMON_PATH . 'Common/',
                CONF_PATH,
                APP_PATH . $module . '/',
                APP_PATH . $module . '/Behavior/',
                APP_PATH . $module . '/Common/',
                APP_PATH . $module . '/' . $c_layer . '/',
                APP_PATH . $module . '/' . $c_layer . '/Frontend/',
                APP_PATH . $module . '/' . $c_layer . '/Admincp/',
                APP_PATH . $module . '/' . $c_layer . '/Rpc/',
                APP_PATH . $module . '/' . $c_layer . '/Api/',
                APP_PATH . $module . '/' . $c_layer . '/Apicp/',
                APP_PATH . $module . '/Service/',
                APP_PATH . $module . '/Sql/',
                APP_PATH . $module . '/Model/',
                APP_PATH . $module . '/Conf.bak/',
                APP_PATH . $module . '/Lang/',
                APP_PATH . $module . '/View/',
                APP_PATH . $module . '/View/Frontend/',
                APP_PATH . $module . '/View/Admincp/',
                RUNTIME_PATH,
                CACHE_PATH,
                CACHE_PATH . $module . '/',
                LOG_PATH,
                LOG_PATH . $module . '/',
                TEMP_PATH,
                DATA_PATH
            );
            foreach ($dirs as $dir) {
                if (!is_dir($dir)) {
                    mkdir($dir, 0755, true);
                }
            }
            // 写入目录安全文件
            self::buildDirSecure($dirs);
            // 写入应用配置文件
            if (!is_file(CONF_PATH . 'config' . CONF_EXT)) {
                file_put_contents(CONF_PATH . 'config' . CONF_EXT, '.php' == CONF_EXT ? "<?php\nreturn [\n    //'配置项'=>'配置值'\n];" : '');
            }
            // 写入模块配置文件
            if (!is_file(APP_PATH . $module . '/Conf.bak/config' . CONF_EXT)) {
                file_put_contents(APP_PATH . $module . '/Conf.bak/config' . CONF_EXT, '.php' == CONF_EXT ? "<?php\nreturn [\n    //'配置项'=>'配置值'\n];" : '');
            }
            // by zhuxun, begin
            // 表数据
            if (!is_file(APP_PATH . $module . '/Sql/data.php')) {
                file_put_contents(APP_PATH . $module . '/Sql/data.php', "<?php\n/**\n * 应用安装时的初始数据文件\n * data.php\n * \$Author\$\n */\n\nreturn \"\";\n");
            }
            // 表结构
            if (!is_file(APP_PATH . $module . '/Sql/structure.php')) {
                file_put_contents(APP_PATH . $module . '/Sql/structure.php', "<?php\n/**\n * 应用的数据表结构文件\n * structure.php\n * \$Author\$\n */\n\nreturn \"\";\n");
            }
            // 生成错误模板
            self::build_tpl(APP_PATH . $module . '/View/Frontend/Error.tpl', self::$error_tpl);
            // end.
            // 生成模块的测试控制器
            if (defined('BUILD_CONTROLLER_LIST')) {
                // 自动生成的控制器列表(注意大小写)
                $list = explode(',', BUILD_CONTROLLER_LIST);
                foreach ($list as $controller) {
                    self::buildController($module, $controller);
                }
            } else {
                // 生成默认的控制器
                self::buildController($module);
            }
            // 生成模块的模型
            if (defined('BUILD_MODEL_LIST')) {
                // 自动生成的控制器列表(注意大小写)
                $list = explode(',', BUILD_MODEL_LIST);
                foreach ($list as $model) {
                    self::buildModel($module, $model);
                }
            }
        } else {
            header('Content-Type:text/html; charset=utf-8');
            exit('应用目录[' . APP_PATH . ']不可写,目录无法自动生成!<BR>请手动生成项目目录~');
        }
    }

    /**
     * 检查缓存目录(Runtime) 如果不存在则自动创建
     *
     * @return boolean
     */
    static public function buildRuntime()
    {

        if (!is_dir(RUNTIME_PATH)) {
            mkdir(RUNTIME_PATH);
        } elseif (!is_writeable(RUNTIME_PATH)) {
            header('Content-Type:text/html; charset=utf-8');
            exit('目录 [ ' . RUNTIME_PATH . ' ] 不可写!');
        }
        mkdir(CACHE_PATH); // 模板缓存目录
        if (!is_dir(LOG_PATH)) {
            mkdir(LOG_PATH); // 日志目录
        }
        if (!is_dir(TEMP_PATH)) {
            mkdir(TEMP_PATH); // 数据缓存目录
        }
        if (!is_dir(DATA_PATH)) {
            mkdir(DATA_PATH); // 数据文件目录
        }

        return true;
    }

    /**
     * 从文件路径解析所有目录, 检查是否有安全文件
     *
     * @param string $file 文件路径
     * @return bool
     */
    static public function chk_dir_secure($file)
    {

        // 先剔除 app 目录
        $path = str_replace(APP_PATH, '', $file);
        // 切割路径
        $paths = explode('/', $path);
        $dirs = array();
        // 原始目录为 app 目录
        $dir = APP_PATH;
        // 遍历所有路径
        foreach ($paths as $_path) {
            // 路径为空时, 忽略
            if (empty($_path)) {
                continue;
            }
            $dir = $dir . $_path . '/';
            $dirs[] = $dir;
        }
        // 生成安全文件
        self::buildDirSecure($dirs);

        return true;
    }

    /**
     * 创建控制器类
     *
     * @param string $module
     * @param string $controller
     */
    static public function buildController($module, $controller = '')
    {

        // 重写生成方法 by zhuxun37
        $c_layer = ucfirst(C('DEFAULT_C_LAYER'));
        // 如果控制器为空, 则取默认
        if (empty($controller)) {
            $controller = cfg('DEFAULT_CONTROLLER');
        }
        // 控制层文件名
        $file = APP_PATH . $module . '/' . $c_layer . '/' . $controller . $c_layer . EXT;
        $controls = explode('/', $controller);
        $act = array_pop($controls);
        // 如果 $controls 为空
        if (empty($controls)) {
            $cname_cpath = $c_layer;
            $tpl_path = $act;
        } else {
            $cname_cpath = $c_layer . '\\' . str_replace('/', '\\', implode('/', $controls));
            $tpl_path = implode('/', $controls) . '/' . $act;
        }
        // 如果定义了 ACTION_NAME
        if (defined(ACTION_NAME)) {
            $tpl_path .= '/' . ACTION_NAME;
            $acname = ACTION_NAME;
        } else {
            $tpl_path .= '/' . C('DEFAULT_ACTION');
            $acname = C('DEFAULT_ACTION');
        }
        // 判断控制层是否属于 Common
        $controller_tpl = self::$controller;
        if ('Common' == $module) {
            $base_controller = '\Think\Controller';
        } else {
            if ('Abstract' == $act) {
                $controller_tpl = self::$abstract_controller;
                $base_controller = '\Common\\' . $cname_cpath . '\Abstract' . $c_layer;
            } else {
                $base_controller = 'Abstract' . $c_layer;
                // 重建基类文件
                self::buildController($module, empty($controls) ? 'Abstract' : implode('/', $controls) . '/Abstract');
            }
        }
        if (!is_file($file)) {
            $content = str_replace(array(
                '[MODULE]',
                '[CONTROLLER]',
                '[CNAME_CPATH]',
                '[CNAME]',
                '[DEFAULT_ACTION]',
                '[TPL_PATH]',
                '[BASE_CONTROLLER]'
            ), array(
                $module,
                $act,
                $cname_cpath,
                $c_layer,
                $acname,
                $tpl_path,
                $base_controller
            ), $controller_tpl);
            if (!C('APP_USE_NAMESPACE')) {
                $content = preg_replace('/namespace\s(.*?);/', '', $content, 1);
            }
            $dir = dirname($file);
            if (!is_dir($dir)) {
                mkdir($dir, 0755, true);
            }
            self::chk_dir_secure($file);
            file_put_contents($file, $content);
        }
        // end
    }

    /**
     * 创建模型类
     *
     * @param string $module
     * @param string $model
     * @return boolean
     */
    static public function buildModel($module, $model)
    {

        $mname = 'Model';
        $tpl = self::$model;
        // 如果为 Service 层
        if ('Service' == substr($model, -7)) {
            $mname = 'Service';
            $model = substr($model, 0, -7);
            $tpl = 'Abstract' == $model ? self::$abstract_service : self::$service;
        } elseif ('Model' == substr($model, -5)) { // 如果为 Model 层
            $model = substr($model, 0, -5);
            $tpl = 'Abstract' == $model ? self::$abstract_model : self::$model;
        }
        // 文件名
        $file = APP_PATH . $module . '/' . $mname . '/' . $model . $mname . EXT;
        if (is_file($file)) { // 如果文件存在
            return true;
        }
        $content = str_replace(array(
            '[MODULE]',
            '[MODEL]'
        ), array(
            $module,
            $model
        ), $tpl);
        // 如果启用了命名空间
        if (!C('APP_USE_NAMESPACE')) {
            $content = preg_replace('/namespace\s(.*?);/', '', $content, 1);
        }
        // 取文件所在目录
        $dir = dirname($file);
        if (!is_dir($dir)) { // 如果目录不存在
            mkdir($dir, 0755, true);
        }
        // 写入文件
        file_put_contents($file, $content);
        // 如果是 Service 层
        if ('Service' == $mname) {
            // 重建基类文件
            if ('Common' != $module) {
                self::buildModel($module, 'AbstractService');
            }
        } else if ('Model' == $mname) {
            if ('Common' != $module) {
                self::buildModel($module, 'AbstractModel');
            }
        }

        return true;
    }

    /**
     * 创建模板文件
     *
     * @param string $tplFile 模板文件路径
     * @param string $tpl     模板内容
     * @return bool
     */
    static public function build_tpl($tplFile, $tpl)
    {

        $dir = dirname($tplFile);
        if (!is_dir($dir)) {
            mkdir($dir, 0755, true);
        }
        self::chk_dir_secure($dir);
        file_put_contents($tplFile, empty($tpl) ? self::$template : $tpl);

        return true;
    }

    /**
     * 生成目录安全文件
     *
     * @param array $dirs
     */
    static public function buildDirSecure($dirs = array())
    {

        // 目录安全写入(默认开启)
        defined('BUILD_DIR_SECURE') or define('BUILD_DIR_SECURE', true);
        if (BUILD_DIR_SECURE) {
            defined('DIR_SECURE_FILENAME') or define('DIR_SECURE_FILENAME', 'index.html');
            defined('DIR_SECURE_CONTENT') or define('DIR_SECURE_CONTENT', ' ');
            // 自动写入目录安全文件
            $content = DIR_SECURE_CONTENT;
            $files = explode(',', DIR_SECURE_FILENAME);
            foreach ($files as $filename) {
                foreach ($dirs as $dir) {
                    file_put_contents($dir . $filename, $content);
                }
            }
        }
    }

    /**
     * 创建默认操作方法
     * by zhuxun begin.
     *
     * @param string $module     模块名称
     * @param string $controller 控制器名称
     * @param string $action     具体操作名称
     * @return bool
     */
    static public function buildAction($module, $controller, $action, $isapi = false)
    {

        $c_layer = ucfirst(C('DEFAULT_C_LAYER'));
        $file = APP_PATH . $module . '/' . $c_layer . '/' . $controller . $c_layer . EXT;
        // 如果不是文件
        if (!is_file($file)) {
            return false;
        }
        $actionName = ucfirst(ACTION_NAME);
        // 模板路径
        $tpl_path = $controller . '/' . $actionName;
        if ($isapi) {
            $actionName = $actionName . '_' . strtolower(REQUEST_METHOD);
        }
        // 解析操作方法
        $action = str_replace(array(
            '[ACTION_NAME]',
            '[TPL_PATH]'
        ), array(
            $actionName,
            $tpl_path
        ), $isapi ? self::$action_api : self::$action);
        // 读取类文件
        $content = file_get_contents($file);
        // 增减方法
        $content = preg_replace('/^(.*?)\}\s*$/is', "\\1" . $action . "}\n", $content);
        // 写入文件
        file_put_contents($file, $content);

        return true;
    }
    // end.
}