#!@PHP-BIN@
<?php
include_once("AutoSOAP/AutoSOAP.php");

$cmd = new AutoSOAP_cmd($argv);
$cmd->execute();

class AutoSOAP_cmd {
    private $modes = array(
        '-c'=>array('set_class',1," NX    NX"),
        '-l'=>array('set_location',1," P[V  P[V"),
        '-o'=>array('set_output',1," fBNg    o͐"),
        '--with-xsd'=>array('set_output_xsd',0,"    XSDt@C"),
        '--show-wsdl'=>array('set_show_wsdl',0,"    AWSDL̏Ԃ"),
    );
    private $target_file_path = null;
    private $classes = null;
    private $output = null;
    private $flg_output_xsd = false;
    private $flg_show_wsdl = false;
    private $location = 'http://localhost/';

    private $server = null;

    public function __construct($argv) {
        date_default_timezone_set('UTC');
        $this->target_file_path = $argv[1];

        $len = count($argv);
        if(1==$len) {
            echo "php2wsdl NXt@CpX [options]\r\n";
            foreach ($this->modes as $command => $info) {
                echo $command . $info[2] . "\r\n";
            }
            return ;
        }
        $i = 2;
        while ($i<$len) {
            if (array_key_exists($argv[$i], $this->modes)) {
                $func = $this->modes[$argv[$i]][0];
                if (0==$this->modes[$argv[$i]][1]) {
                    $this->$func();
                }
                else {
                    $this->$func($argv[$i+1]);
                    $i += $this->modes[$argv[$i]][1];
                }
            }
            else {
                trigger_error("Command '{$argv[$i]}' is not valid.",E_USER_ERROR);
            }
            $i++;
        }

        /*
        * SOAPT[o
        */
        $this->server = AutoSOAP_Server::getInstance();
        $this->server->setEncoding("UTF-8", "Shift_JIS");
        restore_error_handler();
    }
  
    public function execute() {
        if (is_null($this->server))
            return ;
        //o͐̎擾
        if(is_null($this->output)) {
            $this->output = dirname($this->target_file_path);
        }
        $this->output .= '/';

        //ϊNX̎擾
        if(is_null($this->classes)) {
            $contents = file_get_contents($this->target_file_path);
            $matches = array();
            if (preg_match_all("/\s*class\s+(\S+)/",$contents,$matches)) {
                $this->classes = $matches[1];
            }
        }
        foreach ($this->classes as $class_name) {
            $this->create_files($class_name);
        }
    }
  
    public function create_files($class_name) {
        /*
         * ݒ
         */
        $wsdl_path     = $this->output .$class_name.".wsdl";
        $xsd_path      = (true===$this->flg_output_xsd) ? $this->output .$class_name.".xsd" : null;
        $classmap_path = $this->output .$class_name.".classmap.php";
        $location      = $this->location.$class_name;
        $namespace     = "urn:".$class_name;

        $timestamp     = date("Y/m/d H:i:s");

        /*
         * T[rX
         */
        $service = new AutoSOAP_Service(
                        $class_name,
                        $this->target_file_path,
                        $wsdl_path,
                        $xsd_path,
                        $classmap_path,
                        $location,
                        $namespace,
                        $timestamp
                    );

        /*
         * SOAPT[rXT[o֐ݒ肷
         */
        $this->server->setService($service);
        
        echo "t@C:@".$wsdl_path."܂B\n";
        
        //WSDĽʂ
        if ($this->flg_show_wsdl) {
              $client = new SoapClient($wsdl_path, array('soap_version' => SOAP_1_1));
              echo "\bhꗗ:\n";
              print_r($client->__getFunctions());
              echo "^ꗗ:\n";
             print_r($client->__getTypes());
        }
    }

    public function set_output($param) {
        $this->output = $param;
    }
    public function set_location($param) {
        $this->location = $param;
    }

    public function set_class($param) {
        $this->classes = array($param);
    }
    public function set_output_xsd() {
        $this->flg_output_xsd = true;
    }
    public function set_show_wsdl() {
        $this->flg_show_wsdl = true;
    }
    public function set_output_classmap() {
        $this->flg_output_classmap = true;
    }
}
?>