linux命令列下執行 ZendFramework 架構下的php代碼

來源:互聯網
上載者:User

用zendframework 已經有段時間了, 感覺用其開發很輕鬆,最近需要在linux下定時執行 一段php指令碼。 參考了項目的架構結構,編寫這個指令碼(以下指令碼部分業務相關的函數省略),放在crontab中執行,很正常。寫篇日誌記下,php項目不是天天碰,怕忘記。

 

<?php

$root = dirname(dirname(__FILE__));
$dumpDir = "$root/dump";
$listFile = "$dumpDir/list.csv";

if (!file_exists($listFile)) {
     echo "please check list.csv file exist under dump directory";
     return ;
}

$lib = "$root/library";
$app = "$root/application";
set_include_path($lib . PATH_SEPARATOR . $app . PATH_SEPARATOR . get_include_path());

require_once 'Zend/Loader.php';
Zend_Loader::registerAutoload();

$config = new Zend_Config_Ini("$app/config/install.ini");
if (!empty($config->database)) {
    $datasource = $config->database->main;
} else {
    throw new Scarab_Exception_Config('The DSN is not configured while using the database!');
}

$db = Zend_Db::factory($datasource);
Zend_Registry::set('db', $db);
$db->query("SET NAMES utf8");

$date = new Zend_Date(NULL, 'zh_CN');
require_once "$app/core/models/Synchronization.php";
require_once "$app/core/models/Tables.php";
$sync = new Sync();
$personList = parseFile($listFile);

try {
    foreach ($personList as $item) {
        $from = new Zend_Date($item[1], 'Y-m-d');
        $to = new Zend_Date($item[2], 'Y-m-d');
        
        if ($from->compare($to) > 0 ) {
            continue;
        }
        
        while ($from->compare($to) < 0) {
            
            $fromStr = $from->toString('Y-m-dd');
            $from->add('1', Zend_Date::DAY);
            $fromStr_ = $from->toString('Y-m-dd');
            
            $dataOfHour = $sync->getAppXML($permid,$fromStr, $fromStr_);
            if(!empty($dataOfHour)) {
                $sync->saveToFile($dataOfHour,"$dumpDir/$name/$fromStr.hour.csv");
            }
            $dataOfminute = $sync->getAppDataXml($permid, $fromStr);
            if (!empty($dataOfminute)) {
                $sync->saveToFile($dataOfminute,"$dumpDir/$name/$fromStr.minute.csv");
            }
            
            
        }
    }
    
} catch (Zend_Db_Exception $e) {
    echo $e->getMessage();
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.