Exemplo n.º 1
0
 /**
  * Parse a file to retrieve the class (and namespace) within it.
  *
  * @param string $file The filename
  *
  * @return string|null
  */
 private function getClassForFile($file)
 {
     // Open a file pointer to the file
     $fp = fopen($file, 'r');
     // Initialise some variables
     $class = $namespace = $buffer = '';
     $i = 0;
     // Loop through each line of the file until a class is found
     while (!$class) {
         // If we reach the end of the file then exit the loop
         if (feof($fp)) {
             break;
         }
         // Read a portion from the file and append it to the buffer
         $buffer .= fread($fp, 512);
         // Scan the file for tokens
         //
         // We suppress errors here, as we expect errors because we are
         // only parsing a portion of the file at a time
         $tokens = @token_get_all($buffer);
         // Don't bother trying to parse the output until we
         // can see an opening curly brace
         if (strpos($buffer, '{') === false) {
             continue;
         }
         // Loop through each of the found tokens
         for (; $i < count($tokens); $i++) {
             // Check if the namespace keyword has been found yet
             if ($tokens[$i][0] === T_NAMESPACE) {
                 // Continue looping through the found tokens
                 for ($j = $i + 1; $j < count($tokens); $j++) {
                     // A string immediately after the namespace keyword
                     // is the name of the namespace
                     if ($tokens[$j][0] === T_STRING) {
                         // Add each section of the namespace to
                         // our predefined variable
                         $namespace .= '\\' . $tokens[$j][1];
                     } elseif ($tokens[$j] === '{' || $tokens[$j] === ';') {
                         // If we reach a curly brace or a semicolon
                         // we've got the full namespace
                         break;
                     }
                 }
             }
             // Check if the class keyword has been found yet
             if ($tokens[$i][0] === T_CLASS) {
                 // Continue looping through the found tokens
                 for ($j = $i + 1; $j < count($tokens); $j++) {
                     // When we reach the curly brace, store the
                     // class name in the predefined variable
                     if ($tokens[$j] === '{') {
                         $class = $tokens[$i + 2][1];
                     }
                 }
             }
         }
     }
     // If no class is found, return null
     if (!$class) {
         return;
     }
     // Return the fully-qualified class name
     return Str::namespaced("{$namespace}\\{$class}");
 }
Exemplo n.º 2
0
 /**
  * Tests conversion of naughty strings to namespaced.
  *
  * @dataProvider bigListOfNaughtyStrings
  *
  * @covers Molovo\Str\Str::slug
  */
 public function testNamespacedWithNaughtyStrings($string)
 {
     $string = Str::namespaced(base64_decode($string));
     if (!empty($string)) {
         verify($string)->regExp('/[a-zA-Z0-9\\\\]/');
     }
 }