Esempio n. 1
0
 /**
  * Get the table for the current model.
  *
  * @return Table|null
  */
 public static function getTable()
 {
     $class = implode('', array_slice(explode('\\', static::class), -1));
     if (static::$tableName === null) {
         static::$tableName = Str::pluralize(Str::snakeCase($class));
     }
     $alias = Str::pluralize(Str::snakeCase($class));
     return Table::find(static::$tableName, $alias);
 }
Esempio n. 2
0
 /**
  * Retrieve the CSRF token for the current request.
  *
  * @return string The CSRF token
  */
 private static function token()
 {
     if (static::$token !== null) {
         return static::$token;
     }
     $token = Str::random(32);
     // Store the new token
     Session::set(self::SESSION_KEY, $token);
     return static::$token = $token;
 }
Esempio n. 3
0
 function resource($route, $controller)
 {
     $application = Application::instance();
     if (!is_array($route)) {
         $route = [$route, Str::snakeCase(str_replace('Controller', '', $controller))];
     }
     list($route, $name) = $route;
     get([$route, $name], $controller . '@index');
     post([$route, $name . '.create'], $controller . '@create');
     get([$route . '/{id:int}', $name . '.show'], $controller . '@show');
     post([$route . '/{id:int}', $name . '.update'], $controller . '@update');
     get([$route . '/{id:int}/edit', $name . '.edit'], $controller . '@edit');
     delete([$route . '/{id:int}', $name . '.delete'], $controller . '@destroy');
     post([$route . '/{id:int}/destroy', $name . '.destroy'], $controller . '@destroy');
 }
Esempio n. 4
0
 /**
  * Get the field names for a given table.
  *
  * @param Table $table The table
  *
  * @return string[] An array of field names
  */
 public function relationshipsForTable(Table $table)
 {
     $database = $this->instance->databaseName;
     $relationships = [];
     // Fetch all the to-one relationships
     $result = $this->client->query("SELECT `constraint_name`, `column_name`, `referenced_table_name`, `referenced_column_name` FROM `information_schema`.`key_column_usage` WHERE `referenced_table_name` IS NOT NULL AND `table_schema`='{$database}' AND `table_name`='{$table->name}'");
     if ($result !== false) {
         // Loop through the relationships, and add each one to the array
         while ($data = $result->fetch(PDO::FETCH_ASSOC)) {
             $relationships[Str::singularize($data['referenced_table_name'])] = (object) ['column' => $data['column_name'], 'table' => $data['referenced_table_name'], 'references' => $data['referenced_column_name']];
         }
     }
     // Fetch all the to-many relationships
     $result = $this->client->query("SELECT `constraint_name`, `column_name`, `table_name`, `referenced_column_name` FROM `information_schema`.`key_column_usage` WHERE `table_name` IS NOT NULL AND `table_schema`='{$database}' AND `referenced_table_name`='{$table->name}'");
     // If no results are returned, return an empty array
     if ($result !== false) {
         // Loop through the relationships, and add each one to the array
         while ($data = $result->fetch(PDO::FETCH_ASSOC)) {
             $relationships[Str::pluralize($data['table_name'])] = (object) ['column' => $data['referenced_column_name'], 'table' => $data['table_name'], 'references' => $data['column_name']];
         }
     }
     return $relationships;
 }
Esempio n. 5
0
 /**
  * Work the model class based on the table name, and cache it for later.
  *
  * @param Table $table The table to get a model for
  *
  * @return string The name of a model class
  */
 protected function getModelClass($table)
 {
     if (isset(static::$modelClassCache[$table->name])) {
         return static::$modelClassCache[$table->name];
     }
     $namespace = null;
     $config = Config::get($table->instance->name);
     if (isset($config->model_namespace)) {
         $namespace = $config->model_namespace;
     }
     if ($namespace === null) {
         $config = Config::get('default');
         if (isset($config->model_namespace)) {
             $namespace = $config->model_namespace;
         }
         if ($namespace === null) {
             $namespace = 'Models';
         }
     }
     $modelClass = Str::singularize($table->name);
     $modelClass = Str::camelCaps($namespace . '\\' . $modelClass);
     if (!class_exists($modelClass)) {
         $modelClass = Model::class;
     }
     return static::$modelClassCache[$table->name] = $modelClass;
 }
Esempio n. 6
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}");
 }
Esempio n. 7
0
 /**
  * Execute the command.
  */
 public function executeCommand()
 {
     $command = $this->command;
     $class = 'Pug\\Cli\\Command\\' . Str::camelCaps($command);
     if (!class_exists($class)) {
         Prompt::outputend(ANSI::fg('The command "' . $command . '" could not be found.' . "\n", ANSI::RED));
         $this->command = 'help';
         Help::execute($this);
         exit(127);
     }
     $class::execute($this);
 }
Esempio n. 8
0
 public function setHeader($key, $value)
 {
     $key = Str::camelCaps($key);
     return header("{$key}: {$value}");
 }
Esempio n. 9
0
 /**
  * Get a safe cache key.
  *
  * @return string The cache key
  */
 private function cacheKey()
 {
     return 'views.' . Str::slug($this->name);
 }
Esempio n. 10
0
 /**
  * Magic method which allows for dynamically adding where clauses by
  * including the camelCased column name in the method name.
  *
  * e.g. `$query->whereColumnName(3)` is equivalent to `$query->where('column_name', 3)`
  *
  * @param string $methodName The method being called
  * @param array  $args       The arguments provided to the method
  *
  * @return $this
  */
 public function __call($methodName, $args)
 {
     // Convert the method name to snake_case
     $methodName = Str::snakeCase($methodName);
     // Explode the method name, and separate the first word
     $words = explode('_', $methodName);
     $firstWord = array_shift($words);
     // If the method name begins with 'where', convert the rest of the
     // method name into a column name, and add a where clause to the query
     if ($firstWord === 'where') {
         // Get the column name from the method name
         $columnName = implode('_', $words);
         // Add the column name to the beginning of the args array
         array_unshift($args, $columnName);
         // Create new reflection on the Query class
         $ref = new \ReflectionClass(self::class);
         // Invoke the `where` method with the provided arguments
         $method = $ref->getMethod('where');
         $method->invokeArgs($this, $args);
     }
     // Return the query for chaining
     return $this;
 }
Esempio n. 11
0
 /**
  * Tests that the output of random strings is truly random.
  *
  * @covers Molovo\Str\Str::random
  */
 public function testRandomStringUniqueness()
 {
     $strings = [];
     $i = 0;
     while ($i <= 100) {
         $strings[] = Str::random(10);
         $i++;
     }
     verify(count(array_unique($strings)))->equals(count($strings));
 }
Esempio n. 12
0
 /**
  * Run all tests assigned to the runner.
  */
 public function run()
 {
     // Set the total count to zero
     $this->totalCount = 0;
     // Load the suites defined in the options
     $suites = $this->options->suites;
     // Check if a suite name has been passed via the command line
     if ($suite = $this->options->suite) {
         // If the suite hasn't been defined, throw an exception
         if (!$this->options->suites->{$suite}) {
             throw new TestNotFoundException('The suite ' . $suite . ' is not defined');
         }
         // Overwrite the array of suites, so that only the passed
         // suite is run
         $suites = [$suite => $this->options->suites->{$suite}];
     }
     // If arguments exist, then we create a new suite from the
     // files/directories passed as arguments
     if ($this->args) {
         $suites = ['tests' => $this->args];
     }
     // Loop through each of the suites
     foreach ($suites as $suite => $dirs) {
         // Reset the runner's tests
         $this->tests = [];
         // Load the tests for the suite
         $this->loadTests($dirs);
         // Get the number of tests and increment the total count
         $total = count($this->tests);
         $this->totalCount += $total;
         // Get the suite name
         $name = Str::title($suite);
         // Output a message to the user
         echo "\n";
         echo $this->output->gray->render("Running {$total} tests in suite {$name}...");
         // Render the table of tests
         $this->output->table();
         // If the random option is set, shuffle the array of tests
         if ($this->options->random === true) {
             shuffle($this->tests);
         }
         // Loop through each of the tests
         foreach ($this->tests as $test) {
             // Run the test inside a try/catch block so that we can
             // include errors in results without stopping testing
             try {
                 $test->run();
             } catch (Exception $e) {
                 $bits = explode('\\', get_class($e));
                 // Mark the test as errored
                 $test->error(array_pop($bits) . ': ' . $e->getMessage());
             }
         }
     }
     // Output the results to the CLI
     $this->output->results();
 }