コード例 #1
0
ファイル: Route.class.php プロジェクト: noahshrader/zdp
 public function __construct($path)
 {
     try {
         $this->configuration = new ZdpArrayObject(parse_ini_file('zdp.ini'));
         try {
             $this->database = new PDO('mysql:host=' . $this->configuration['dbhost'] . ';dbname=' . $this->configuration['dbname'] . ';port=3306', $this->configuration['dbuser'], $this->configuration['dbpass']);
         } catch (PDOException $err) {
             $this->database = new ZdpArrayObject(['error' => 'Unable to connect to the database.']);
         }
         //Determines if any restrictions on classes are set and, if so, retrieves them from the config then combines them with the declared classes of the environment,
         //otherwise, grabs only the declared environment classes. NOTE: ALL classes that are preloaded before route is loaded WILL BE RESTRICTED. There is NO exception
         //to this until a 'restriction-exception' option can be added to the INI.
         if ($this->configuration->keyExists('restrictions') && trim($this->configuration['restrictions']) != '') {
             $this->configuration['restrictions'] = new ZdpArrayObject(array_merge(explode(',', $this->configuration['restrictions']), get_declared_classes()));
         } else {
             $this->configuration['restrictions'] = new ZdpArrayObject(get_declared_classes());
         }
         $this->classStack = new ZdpArrayObject();
         //Creates a new ArrayObject (using the ZdpArrayObject extension object) and explodes the path into it.
         $workingPath = new ZdpArrayObject(explode('/', $path));
         //Determines if the first and last elements are empty space and, if so, shifts and pops them off of the stack.
         //Determines if the first element matches the UUID definition.
         if (preg_match('/.{8}\\-.{4}\\-.{4}\\-.{4}\\-.{8}/', $workingPath[0]) === 1 || Marathons::IsMarathon(new ZdpArrayObject(['Name' => $workingPath[0]]), $this->configuration, null, null, $this->database)['result']['marathon'][0]['is_marathon'] > 0) {
             $this->marathon = $workingPath[0];
             $workingPath->shift();
         }
         if (preg_match('/.{8}\\-.{4}\\-.{4}\\-.{4}\\-.{8}/', $workingPath[0]) === 1 || Campaigns::IsCampaign(new ZdpArrayObject(['Name' => $workingPath[0]]), $this->configuration, null, null, $this->database)['result']['campaign'][0]['is_campaign'] > 0) {
             $this->campaign = $workingPath[0];
             $workingPath->shift();
         }
         if (preg_match('/.{8}\\-.{4}\\-.{4}\\-.{4}\\-.{8}/', $workingPath[0]) === 1) {
             $this->key = $workingPath[0];
             $workingPath->shift();
         }
         $this->validateIdentity();
         //Creates a class and method comparer to see if a class has been found and iterates over the ZdpArrayObject. The current class is populated once a new class is found and added to the object's class stack.
         $currentClass = '';
         $currentMethod = '';
         //This is a work around to determine if the first element is a class and implements page
         //(pages should NEVER be called after the first class. This is undefined behavior and will only work if 'render' is explicitly provided as the method being called)
         //and if its next element is NOT the render method.
         $implements = new ZdpArrayObject(class_implements($workingPath[0]));
         if (class_exists($workingPath[0]) && $implements->contains('Page') && ($workingPath->count() > 1 && $workingPath[1] != 'render' && !method_exists($workingPath[0], $workingPath[1]) || $workingPath->count() === 1)) {
             $workingPath->splice(1, 0, ['render']);
         }
         foreach ($workingPath as $component) {
             //Determines if the class exists and is not the same as the current class and, if so, creates a new class container and sets the class comparer, else, it determines if a class doesn't exist
             //with the component's name and that the class name is set and begins handling potential method code.
             if (class_exists($component) && $currentClass !== $component && !$this->configuration['restrictions']->contains($component)) {
                 $this->classStack[$component] = new ZdpArrayObject();
                 $currentClass = $component;
             } else {
                 if (!class_exists($component) && trim($currentClass) != '' && trim($component) != '') {
                     //Determines if a method exists in the class with the name of component and, if so, creates a new method container, else, determines if the current method string is set and begins handling
                     //potential parameter code.
                     if (method_exists($currentClass, $component)) {
                         $this->classStack[$currentClass][$component] = new ZdpArrayObject();
                         $currentMethod = $component;
                     } else {
                         if (trim($currentMethod) != '' && trim($component) != '') {
                             //Determines if the parameter contains an equals sign and, if so, splits on the sign and adds a parameter with the name of index 0 and the value of index 1, else, adds a parameter with the
                             //name of component and the value of true.
                             if (strpos($component, '=') !== false) {
                                 $holder = explode('=', $component);
                                 //Determines the type of the parameter and sets it to the proper type.
                                 if (ctype_digit($holder[1])) {
                                     $holder[1] = intval($holder[1]);
                                 } else {
                                     if (is_numeric($holder[1])) {
                                         $holder[1] = floatval($holder[1]);
                                     } else {
                                         if (strtoupper($holder[1]) == 'TRUE') {
                                             $holder[1] = true;
                                         } else {
                                             if (strtoupper($holder[1]) == 'FALSE') {
                                                 $holder[1] = false;
                                             }
                                         }
                                     }
                                 }
                                 $this->classStack[$currentClass][$currentMethod][$holder[0]] = $holder[1];
                             } else {
                                 $this->classStack[$currentClass][$currentMethod]->push($component);
                             }
                         } else {
                             //Do something when a method isn't found.
                         }
                     }
                 } else {
                     //Do something when a path isn't found.
                 }
             }
         }
     } catch (ApiException $err) {
         $this->error = $err;
     }
 }