我的使用依赖注入的清单程序无法加载

时间:2019-06-24 15:25:17

标签: php xml dependency-injection interface computer-science

我应该制作一个清单程序,让您输入计算机零件以进行跟踪,并且我应该通过xml通过Dependency Injection将接口层和业务层分开。

使用此处的示例作为参考http://www.littleoceanwaves.com/securephp/examples/getc4exfiles.php,我认为我对代码的外观有所了解,并且能够编写大部分代码。

lab.html

<html>
<head>
<title>ABC Computer Parts Inventory</title>
<script src="e42validator.js"></script>

<style type="text/css">

#JS { display:none; }

</style>

<script>
function checkJS() {

document.getElementById('JS').style.display = "inline";

}
</script>

    </head>

<body onload="checkJS();">
<h1>Inventory Managment</h1>
<div id="JS">
<form method="post" action="lab_interface.php" onSubmit="return validate_input(this)">
<h2>Please complete ALL fields. Please note the required format of information.</h2>
Enter Product Name (max 20 characters, alphabetic) <input type="text" pattern="[a-zA-Z]*"  title="Up to 20 Alphabetic Characters" maxlength="20" name="product_name" id="product_name" required/><br /><br />
Enter Product Code (numeric only) <input type="number" min="1" max="999999" name="product_code" id="product_code" required /><br /><br />
Enter Product price (numeric only) <input type="number" min="1" max="999999" name="product_price" id="product_price" required /><br /><br />
Enter Product Quantity (numeric only) <input type="number" min="1" max="999999" name="product_quantity" id="product_quantity" required /><br /><br />
Enter Product Name (max 20 characters, alphabetic) <input type="text" pattern="[a-zA-Z]*"  title="Up to 40 Alphabetic Characters" maxlength="40" name="product_needs" id="product_needs" required/><br /><br />
<input type="submit" value="Click to add product" />
</form>
</div>
<noscript>
<div id="noJS">
<form method="post" action="lab_interface.php">
<h2>Please complete ALL fields. Please note the required format of information.</h2>
Enter Product Name (max 20 characters, alphabetic) <input type="text" pattern="[a-zA-Z]*"  title="Up to 20 Alphabetic Characters" maxlength="20" name="product_name" id="product_name" required/><br /><br />
Enter Product Code (numeric only) <input type="number" min="1" max="999999" name="product_code" id="product_code" required /><br /><br />
Enter Product price (numeric only) <input type="number" min="1" max="999999" name="product_price" id="product_price" required /><br /><br />
Enter Product Quantity (numeric only) <input type="number" min="1" max="999999" name="product_quantity" id="product_quantity" required /><br /><br />
Enter Product Name (max 20 characters, alphabetic) <input type="text" pattern="[a-zA-Z]*"  title="Up to 40 Alphabetic Characters" maxlength="40" name="product_needs" id="product_needs" required/><br /><br />
<input type="submit" value="Click to add product" />
</form>
</div>
</noscript>
</body>
</html>

lab_interface.php

<?php

function clean_input($value)
{


 $value = htmlentities($value);
        // Removes any html from the string and turns it into &lt; format
        $value = strip_tags($value);


    if (get_magic_quotes_gpc())
    {
        $value = stripslashes($value);

        // Gets rid of unwanted slashes
    }
        $value = htmlentities($value);

        // Removes any html from the string and turns it into &lt; format

       $bad_chars = array( "{", "}", "(", ")", ";", ":", "<", ">", "/", "$" );
       $value = str_ireplace($bad_chars,"",$value);         
        return $value;

}

function error_check_product_app($lab)
{

list($name_error, $code_error, $price_error, $quantity_error, ) = explode(',', $lab);

print $name_error == 'TRUE' ? 'Name update successful<br/>' : 'Name update not successful<br/>';
print $code_error == 'TRUE' ? 'Breed update successful<br/>' : 'Breed update not successful<br/>';
print $price_error == 'TRUE' ? 'Color update successful<br/>' : 'Color update not successful<br/>';
print $quantity_error == 'TRUE' ? 'Weight update successful<br/>' : 'Weight update not successful<br/>';
print $needs_error == 'TRUE' ? 'Name update successful<br/>' : 'Name update not successful<br/>';

}

function get_product_app_properties($lab)
{

print "product name is " . $lab->get_product_name() . "<br/>";
print "product quantity is " . $lab->get_product_quantity() . " lbs. <br />";
print "product code is " . $lab->get_product_code() . "<br />";
print "product price is " . $lab->get_product_price() . "<br />";
print "product needs " . $lab->get_product_needs() . "<br/>";

}
//----------------Main Section-------------------------------------

if ( file_exists("product__container.php"))
{
  Require_once("product__container.php");
 }
 else
 {
    print "System Error #1";
    exit;
 }

if (isset($_POST['product_app']))
  {

if ((isset($_POST['product_name'])) && (isset($_POST['product_code'])) && (isset($_POST['product_price'])) && (isset($_POST['product_quantity'])) && (isset($_POST['product_needs'])))
{

     $container = new product_container(clean_input($_POST['product_app']));

     $product_name = clean_input($_POST['product_name']);
     $product_code = clean_input($_POST['product_code']);
     $product_price = clean_input($_POST['product_price']);
     $product_quantity = clean_input($_POST['product_quantity']);
     $product_needs = clean_input($_POST['product_needs']);


     $properties_array = array($product_name,$product_code,$product_price,$product_quantity,$product_needs);
     $lab = $container->create_object($properties_array);

     if ($lab != FALSE)
     {
     error_check_product_app($lab);
     get_product_app_properties($lab);
     }
     else
     {
     print "System Error #2";
     }
}
  }
?>

product_container.php

<?php

class product_container
{

private $app;
private $product_location;

function __construct($value)
{
if (function_exists('clean_input'))
{
$this->app = $value;
}
else
{
exit;
}
}

public function set_app($value)
{
$this->app = $value;
}

public function get_product_application($search_value)
{

$xmlDoc = new DOMDocument(); 

if ( file_exists("product_applications.xml") )

{
$xmlDoc->load( 'product_applications.xml' ); 

$searchNode = $xmlDoc->getElementsByTagName( "type" ); 

foreach( $searchNode as $searchNode ) 
{ 
    $valueID = $searchNode->getAttribute('ID'); 

    if($valueID == $search_value)
    {

    $xmlLocation = $searchNode->getElementsByTagName( "location" ); 
    return $xmlLocation->item(0)->nodeValue;
    break;

    }

}
}
    throw new Exception("System Error: file not found in xml");
}
function create_object($properties_array)
{

  $product_loc = $this->get_product_application($this->app);
  if(($product_loc == FALSE) || (!file_exists($product_loc)))
  {
    throw new Exception("System Error: Could not create object");
  }
  else
  {

    require_once($product_loc);
    $class_array = get_declared_classes();
    $last_position = count($class_array) - 1;
    $class_name = $class_array[$last_position]; 
    $product_object = new $class_name($properties_array);

    return $product_object;
    }

}

}

?>

product_application.xml

<?xml version="1.0" encoding="UTF-8"?>
<dog_applications>
<application>
<type ID='product'>
<location>inventory.php</location>
</type>
</application>
</dog_applications> 

inventory.php

<?php

class Product {
    // ----------------------------------------- Properties -----------------------------------------
    private $product_name = "no name";
    private $product_code = 0;
    private $product_price = 0;
    private $product_quantity = 90;
    private $product_needs = "no needs";
    private $error_message = "??";

    // ---------------------------------- Set Methods ----------------------------------------------
    function set_product_name($value) {
        $error_message = TRUE;
        (ctype_alpha($value) && strlen($value) <= 20) ? $this-> product_name = $value: $this-> error_message = FALSE;
        return $this-> error_message;
    }

    function set_product_code($value) {
        $error_message = TRUE;
        (is_numeric($value) && ($value > 0 && $value <= 999999)) ? $this-> product_code = $value: $this-> error_message = FALSE;
        return $this-> error_message;
    }

    function set_product_price($value) {
        $error_message = TRUE;
        (is_numeric($value) && ($value > 0 && $value <= 999999)) ? $this-> product_price = $value: $this-> error_message = FALSE;
        return $this-> error_message;
    }

    function set_product_quantity($value) {
        $error_message = TRUE;
        (is_numeric($value) && ($value > 0 && $value <= 999999)) ? $this-> product_quantity = $value: $this-> error_message = FALSE;
        return $this-> error_message;
    }

    function set_product_needs($value) {
        $error_message = TRUE;
        (preg_match('/[^a-z_\-0-9]/i', $value) && strlen($value) <= 40) ? $this-> product_needs = $value: $this-> error_message = FALSE;
        return $this-> error_message;
    }
    // ----------------------------------------- Get Methods ------------------------------------------------------------
    function get_product_name() {
        return $this-> product_name;
    }

    function get_product_code() {
        return $this-> product_code;
    }

    function get_product_price() {
        return $this-> product_price;
    }

    function get_product_quantity() {
        return $this-> product_quantity;
    }

    function get_product_needs() {
        return $this-> product_needs;
    }

    function get_properties() {
        return "$this->product_name,$this->product_code,$this->product_price,$this->product_quantity,$this->product_needs.";
    }
}

还有一个验证器javascript文件,但我认为它工作正常,我认为不需要对其进行调整

当我运行程序时,将加载html,并且可以很好地输入信息,但是当我单击“添加产品”时,所有显示的都是空白页。因此,我需要帮助弄清楚要解决的问题,或者即使我正确执行了所有这些操作。记录下来,我知道像这样在lab_interface.php内部调用stock.php可能会更容易

Require_once("inventory.php");

但是我的作业希望我使用依赖注入,因此我不确定是否真的允许这样做。

0 个答案:

没有答案