/** * Gets the value of an environment variable. Supports boolean, empty and null. * * @param $key string * @param $default mixed * @return mixed */ function env($key, $default = null) { $value = getenv($key); if ($value === false) { return value($default); } switch (strtolower($value)) { case 'true': case '(true)': return true; break; case 'false': case '(false)': return false; break; case 'empty': case '(empty)': return ''; break; case 'null': case '(null)': return; break; default: break; } if (strlen($value) > 1 && Str::startsWith($value, '"') && Str::endsWith($value, '"')) { return substr($value, 1, -1); } return $value; }
public function putUpdate() { // Handle the rest of the settings $fields = Input::all(); foreach ($fields as $key => $value) { // Check if it's a private key if (\Str::startsWith($key, '_')) { continue; } // Void unchanged/unset values. if ($value == '') { continue; } // Handle salt if ($key == 'salt') { exit('this isn\'t ready'); $auth = Config::get('salt.credentials'); foreach ($value as $k => $v) { if ($k == 'auth_username') { $auth['username'] = $v; } if ($k == 'auth_password' && $v != '') { $auth['password'] = $v; } Config::set($k, $v); } } $key = str_replace('_', '.', $key); Setting::set($key, $value); } return Redirect::action('Admin\\SettingController@getIndex'); }
public function formAfterDelete($model) { $fields = json_decode($model->fields); foreach ($fields as $value) { if (\Str::startsWith($value, 'attachment::')) { $id = explode('::', $value)[1]; $file = \System\Models\File::find($id); if ($file) { $file->delete(); } } } }
/** * Extract all of the parameters from the tokens. * * @param array $tokens * * @return array */ protected static function parameters(array $tokens) { $arguments = []; $options = []; foreach ($tokens as $token) { if (!Str::startsWith($token, '--')) { $arguments[] = static::parseArgument($token); } else { $options[] = static::parseOption(ltrim($token, '-')); } } return [$arguments, $options]; }
function extract_doc_annotations($comment, $annotation) { $values = []; foreach (explode("\n", $comment) as $line) { if (!Str::startsWith(trim($line), '* ' . $annotation)) { continue; } $value = Str::substringAfter($line, $annotation); if ($value !== '') { $values[] = trim($value); } } return $values; }
protected function displayBuildMessage($buildMessage) { $message = trim($buildMessage); $change = BuildCommand::CHANGE; $delete = BuildCommand::DELETE; $notice = BuildCommand::NOTICE; $error = BuildCommand::ERROR; if (Str::startsWith($message, $change)) { $this->info($this->parseMessage($message, $change)); } elseif (Str::startsWith($message, $delete)) { $this->warn($this->parseMessage($message, $delete)); } elseif (Str::startsWith($message, $notice)) { $this->comment($this->parseMessage($message, $notice)); } elseif (Str::startsWith($message, $error)) { $this->error($this->parseMessage($message, $error)); } elseif ($message) { $this->line($this->parseMessage($message)); } }
/** * {@inheritdoc} */ public final function getRequestTarget() { if (null !== $this->requestTarget) { return $this->requestTarget; } $uri = $this->getUri(); if (null === $uri) { return '/'; } $path = $uri->getPath(); if (is_callable([$uri, 'getBasePath']) and !Str::startsWith($path, '/') and '' !== ($basePath = $uri->getBasePath())) { $path = $basePath . ('/' === $basePath ? '' : '/') . $path; } $requestTarget = $path; $query = $uri->getQuery(); if ('' !== $query) { $requestTarget .= '?' . $query; } $this->setRequestTarget($requestTarget); return $requestTarget; }
/** * Determine if a given string starts with a given substring. * * @param string $haystack * @param string|array $needles * @return bool */ function starts_with($haystack, $needles) { return Str::startsWith($haystack, $needles); }
public function isBooleanField($field) { return Str::startsWith($field, 'ind_'); }
/** * Register an after filter to automatically display the profiler. * * @return void */ public function registerProfilerToOutput() { $app = $this->app; $sessionHash = static::SESSION_HASH; $app['router']->after(function ($request, $response) use($app, $sessionHash) { $profiler = $app['profiler']; $session = $app['session']; if ($session->has($sessionHash)) { $profiler->enable($session->get($sessionHash)); } // Do not display profiler on ajax requests or non-HTML responses. $isHTML = \Str::startsWith($response->headers->get('Content-Type'), 'text/html'); if (!$profiler->isEnabled() or $request->ajax() or !$isHTML) { return; } $responseContent = $response->getContent(); // Don't do anything if the response content is not a string. if (is_string($responseContent)) { $profiler = $profiler->render(); // If we can find a closing HTML tag in the response, let's add the // profiler content inside it. if (($pos = strrpos($responseContent, '</html>')) !== false) { $responseContent = substr($responseContent, 0, $pos) . $profiler . substr($responseContent, $pos); } else { $responseContent .= $profiler; } $response->setContent($responseContent); } }); }
public function testStartsWithIsFalse() { $this->assertFalse(Str::startsWith('abcdefghij', 'ahh')); }
/** * @param \Illuminate\Database\Eloquent\Model $model */ protected function getPropertiesFromMethods($model) { $methods = get_class_methods($model); if ($methods) { foreach ($methods as $method) { if (\Str::startsWith($method, 'get') && \Str::endsWith($method, 'Attribute') && $method !== 'setAttribute') { //Magic get<name>Attribute $name = \Str::snake(substr($method, 3, -9)); if (!empty($name)) { $this->setProperty($name, null, true, null); } } elseif (\Str::startsWith($method, 'set') && \Str::endsWith($method, 'Attribute') && $method !== 'setAttribute') { //Magic set<name>Attribute $name = \Str::snake(substr($method, 3, -9)); if (!empty($name)) { $this->setProperty($name, null, null, true); } } elseif (\Str::startsWith($method, 'scope') && $method !== 'scopeQuery') { //Magic set<name>Attribute $name = \Str::camel(substr($method, 5)); if (!empty($name)) { $reflection = new \ReflectionMethod($model, $method); $args = $this->getParameters($reflection); //Remove the first ($query) argument array_shift($args); $this->setMethod($name, $reflection->class, $args); } } elseif (!method_exists('Eloquent', $method) && !\Str::startsWith($method, 'get')) { //Use reflection to inspect the code, based on Illuminate/Support/SerializableClosure.php $reflection = new \ReflectionMethod($model, $method); $file = new \SplFileObject($reflection->getFileName()); $file->seek($reflection->getStartLine() - 1); $code = ''; while ($file->key() < $reflection->getEndLine()) { $code .= $file->current(); $file->next(); } $begin = strpos($code, 'function('); $code = substr($code, $begin, strrpos($code, '}') - $begin + 1); $begin = stripos($code, 'return $this->'); $parts = explode("'", substr($code, $begin + 14), 3); //"return $this->" is 14 chars if (isset($parts[2])) { list($relation, $returnModel, $rest) = $parts; $returnModel = "\\" . ltrim($returnModel, "\\"); $relation = trim($relation, ' ('); if ($relation === "belongsTo" or $relation === 'hasOne') { //Single model is returned $this->setProperty($method, $returnModel, true, null); } elseif ($relation === "belongsToMany" or $relation === 'hasMany') { //Collection or array of models (because Collection is Arrayable) $this->setProperty($method, '\\Illuminate\\Database\\Eloquent\\Collection|' . $returnModel . '[]', true, null); } } else { //Not a relation } } } } }
/** * Output data on route after * * @return void */ protected function afterListener() { $this->app['router']->after(function ($request, $response) { // Do not display profiler on non-HTML responses. if (\Str::startsWith($response->headers->get('Content-Type'), 'text/html')) { $content = $response->getContent(); $output = Profiler::outputData(); $body_position = strripos($content, '</body>'); if ($body_position !== FALSE) { $content = substr($content, 0, $body_position) . $output . substr($content, $body_position); } else { $content .= $output; } $response->setContent($content); } }); }
public static function isPhar($path = null) { $path = $path === null ? __FILE__ : $path; return Str::startsWith($path, 'phar://'); }
public function isFillable($key) { if (static::$unguarded) { return true; } // If the key is in the "fillable" array, we can of course assume that it's // a fillable attribute. Otherwise, we will check the guarded array when // we need to determine if the attribute is black-listed on the model. if (in_array($key, $this->fillable)) { return true; } //This is the only one line that has been changed from original method. if ($this->isTranslatableFillable($key)) { return true; } if ($this->isGuarded($key)) { return false; } return empty($this->fillable) && !Str::startsWith($key, '_'); }
/** * Determines headers from global variables. * * @throws \BadRequestException For invalid header name or value * * @return array */ protected static final function determineHeadersFromGlobals() { $headers = []; $specialHeaders = ['CONTENT_MD5' => 'Content-MD5']; $standardlizeHeaderName = function ($name) { return ucwords(strtolower(strtr($name, '_', '-')), '-'); }; foreach ($_SERVER as $name => $value) { if (isset($specialHeaders[$name])) { $name = $specialHeaders[$name]; } elseif (Str::startsWith($name, 'HTTP_')) { $name = $standardlizeHeaderName(substr($name, 5)); } elseif (Str::startsWith($name, 'REDIRECT_')) { $name = $standardlizeHeaderName(substr($name, 9)); } elseif (Str::startsWith($name, 'CONTENT_')) { $name = $standardlizeHeaderName($name); } else { continue; } if (!Message::isValidToken($name)) { throw new BadRequestException(sprintf('Invalid header name "%s" detected; must be valid HTTP token.', $name)); } if (!Message::isValidHeaderValue($value)) { throw new BadRequestException(sprintf('Invalid header value "%s" detected; must be valid HTTP header value.', $value)); } $headers[$name] = $value; } return $headers; }
break; } } if (!$allowed) { Response::code(403); return; } Response::download(J_PATH . $path, Request::get("name")); }); Router::register("GET", "thumb/", function () { $pieces = explode("/", trim(Request::get("path"), "/")); $path = implode(DS, $pieces); $allowedPaths = Config::item("application", "thumbPaths", array("app/storage/")); $allowed = false; foreach ($allowedPaths as $dir) { if (Str::startsWith($path, File::formatDir($dir))) { $allowed = true; break; } } if (!$allowed || count($pieces) == 0) { return Response::code(403); } $path = implode(DS, $pieces); if (!File::exists(J_PATH . $path) || is_dir(J_PATH . $path)) { return Response::code(404); } $im = new Image(J_PATH . $path); $im->resize((int) Request::get("width"), (int) Request::get("height"), Request::get("method", "fit"), Request::get("background", 0xffffff)); $im->header(); });