/** * Get the authorization bearer token from the headers, useful when AUTHing a user hitting the API that * has already logged in. */ public function getTokenFromHeaders() { $bearer = Request::header('authorization'); if (Str::startsWith($bearer, 'Bearer ')) { $this->token = Str::substr($bearer, 7); } }
public function getTestPath($name) { $name = $this->getClassNamespace($name); $name = Str::substr($name, strlen($this->getRootNamespace()) + 1); $switchToDirectorySlashes = Utils::switchToDirectorySlashes($name); return 'tests' . DIRECTORY_SEPARATOR . str_replace(DIRECTORY_SEPARATOR, "", $switchToDirectorySlashes) . 'Test.php'; }
public function setData($data) { $fullClassName = $this->composer->getClassNamespace($data['name']); $testName = str_replace("\\", "", Str::substr($fullClassName, strlen($this->composer->getRootNamespace()) + 1)) . 'Test'; $data = array_merge(['namespace' => Utils::getJustNamespace($fullClassName), 'className' => Utils::getJustClassName($fullClassName), 'testName' => $testName], $data); $this->data = $data; return $this; }
public function handle(Request $request, Closure $next) { $default = $request->session()->get('locale', 'en'); $locale = $request->input('locale', $default); $locale = Str::substr($locale, 0, 2); $request->session()->put('locale', $locale); $this->app->setLocale($locale); return $next($request); }
/** * Put a request handler * * @param string $message * @param callable $callback * @return $this */ public function put($message, callable $callback) { list($method, $path) = explode(' ', $message); if (Str::startsWith($path, '/')) { $path = Str::substr($path, 1); } $method = Str::upper($method); $this->handlers[$method][$path] = $callback; return $this; }
public static function __callStatic($name, $attributes) { if (Str::startsWith('get', $name)) { $method = Str::camel($name); if (method_exists(new static(), $method)) { $class = static::class; if (Arr::exists($attributes, 0)) { return $class::$method($attributes[0]); } return $class::$method(); } } return env(Str::upper(Str::snake(Str::substr($name, 3)))); }
/** * @return void */ public function boot() { $this->getRouter()->get('/', function () { $home = $this->getSetting()->get('site.home', 'default'); $page_id = 0; if ($home != 'default' && Str::contains($home, 'page_')) { $page_id = Str::substr($home, 5); } if ($page_id && Page::whereEnabled(true)->whereId($page_id)->count()) { return $this->app->call('Notadd\\Page\\Controllers\\PageController@show', ['id' => $page_id]); } $this->app->make('view')->share('logo', file_get_contents(realpath($this->app->frameworkPath() . '/views/install') . DIRECTORY_SEPARATOR . 'logo.svg')); return $this->app->make('view')->make('default::index'); }); }
public function handle() { if (($table = $this->option('table')) === null) { $tables = $this->DBHelper->listTables(); $table = $this->choice('Choose table:', $tables, null); } $columns = collect($this->DBHelper->listColumns($table)); $this->transformer->setColumns($columns); $namespace = config('thunderclap.namespace'); $moduleName = str_replace('_', '', title_case($table)); $containerPath = config('thunderclap.target_dir', base_path('modules')); $modulePath = $containerPath . DIRECTORY_SEPARATOR . $moduleName; // 1. check existing module if (is_dir($modulePath)) { $overwrite = $this->confirm("Folder {$modulePath} already exist, do you want to overwrite it?"); if ($overwrite) { File::deleteDirectory($modulePath); } else { return false; } } // 2. create modules directory $this->info('Creating modules directory...'); $this->packerHelper->makeDir($containerPath); $this->packerHelper->makeDir($modulePath); // 3. copy module skeleton $stubs = __DIR__ . '/../../stubs'; $this->info('Copying module skeleton into ' . $modulePath); File::copyDirectory($stubs, $modulePath); $templates = ['module-name' => str_replace('_', '-', $table), 'route-prefix' => config('thunderclap.routes.prefix')]; // 4. rename file and replace common string $search = [':Namespace:', ':module_name:', ':module-name:', ':module name:', ':Module Name:', ':moduleName:', ':ModuleName:', ':FILLABLE:', ':TRANSFORMER_FIELDS:', ':VALIDATION_RULES:', ':LANG_FIELDS:', ':TABLE_HEADERS:', ':TABLE_FIELDS:', ':DETAIL_FIELDS:', ':FORM_CREATE_FIELDS:', ':FORM_EDIT_FIELDS:', ':VIEW_EXTENDS:', ':route-prefix:', ':route-middleware:', ':route-url-prefix:']; $replace = [$namespace, snake_case($table), $templates['module-name'], str_replace('_', ' ', strtolower($table)), ucwords(str_replace('_', ' ', $table)), str_replace('_', '', camel_case($table)), str_replace('_', '', title_case($table)), $this->transformer->toFillableFields(), $this->transformer->toTransformerFields(), $this->transformer->toValidationRules(), $this->transformer->toLangFields(), $this->transformer->toTableHeaders(), $this->transformer->toTableFields(), $this->transformer->toDetailFields(), $this->transformer->toFormCreateFields(), $this->transformer->toFormUpdateFields(), config('thunderclap.view.extends'), $templates['route-prefix'], $this->toArrayElement(config('thunderclap.routes.middleware')), $this->getRouteUrlPrefix($templates['route-prefix'], $templates['module-name'])]; foreach (File::allFiles($modulePath) as $file) { if (is_file($file)) { $newFile = $deleteOriginal = false; if (Str::endsWith($file, '.stub')) { $newFile = Str::substr($file, 0, -5); $deleteOriginal = true; } $this->packerHelper->replaceAndSave($file, $search, $replace, $newFile, $deleteOriginal); } } $this->warn('Add following service provider to config/app.php ====> ' . $namespace . '\\' . ucwords(str_replace('_', ' ', $table)) . '\\ServiceProvider::class,'); }
/** * Handle the event. * * @param \App\Events\WechatUserSubscribed $event * @return \App\Models\Message */ public function handle(WechatUserSubscribed $event) { $m = $event->message; /** * openId is always unique to our official account, so if user subscribes * again, we just need to toggle un-subscribed flag. */ if ($subscriber = Subscriber::where('openId', $m->fromUserName)->where('unsubscribed', true)->first()) { $subscriber->unsubscribed = false; } else { $subscriber = new Subscriber(); $subscriber->openId = $m->fromUserName; } $subscriber->save(); // Link profile with subscriber if subscribe comes from profile page. if ($key = $m->messageable->eventKey) { Profile::find(Str::substr($key, Str::length('qrscene_')))->update(['weixin' => $m->fromUserName]); event(new ChangeSubscriberGroup($subscriber)); } return $this->greetMessage($m->fromUserName, !is_null($key)); }
/** * Substr * * @param string $origin origin text * @param int $len cut length * @return string */ protected function substr($origin, $len) { $text = Str::substr($origin, 0, $len); return $origin == $text ? $text : $text . '...'; }
/** * Call the given directive with the given value. * * @param string $name * @param string|null $value * @return string */ protected function callCustomDirective($name, $value) { if (Str::startsWith($value, '(') && Str::endsWith($value, ')')) { $value = Str::substr($value, 1, -1); } return call_user_func($this->customDirectives[$name], trim($value)); }
/** * Save photos uploaded with the form into * the Photo model. * * @param object $request * @param object $work * * @return void */ public function savePhotos($request, $work) { // Bail early if we don't have any files if (!$request->hasFile('photos')) { return; } $files = $request->file('photos'); // Loop through all files and create new entries foreach ($files as $key => $file) { $photoTitle = Str::substr($work->title, 0, 10); // Create new photo $photo = Photo::create(['title' => $photoTitle]); // Set image name as slugify title in lowercase $imageName = $photo->id . '.' . Str::slug($photoTitle) . '.' . $file->getClientOriginalExtension(); $imageName = Str::lower($imageName); // Move file to storage location $imagePath = storage_path() . '/uploads/img/'; $file->move($imagePath, $imageName); // Add image filename to model $photo->update(['filename' => $imageName, 'work_id' => $work->id]); } }
protected function prepareRequest($request, array &$options) { $method = $request->getMethod(); $uri = $request->getUri(); $basePath = $options['base_uri']->getPath(); $path = Str::substr($uri->getPath(), Str::length($basePath)); if ($method === 'GET') { parse_str($uri->getQuery(), $options['query']); } else { $body = (string) $request->getBody(); $options['json'] = json_decode($body, true); } return [$method, $path]; }
/** * Get the bearer token from the request headers. * * @return string|null */ public function bearerToken() { $header = $this->header('Authorization', ''); if (Str::startsWith($header, 'Bearer ')) { return Str::substr($header, 7); } }
/** * @param string $path * @return mixed|void * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException */ public function registerCss($path) { if ($this->finder->exits($path)) { switch (str_replace(Str::substr($path, strpos($path, '.')), '', Str::substr($path, strpos($path, '::') + 2))) { case 'css': $this->material->registerCssMaterial($path); break; case 'less': $this->material->registerLessMaterial($path); break; case 'sass': $this->material->registerSassMaterial($path); break; } } else { throw new FileNotFoundException('Css file [' . $path . '] does not exits!'); } }
}); } if (!Str::hasMacro('before')) { /** * Get the part of haystack before needle. * * @param string $haystack * @param string $needle * @return string */ Str::macro('before', function ($haystack, $needle) { $pos = strpos($haystack, $needle); if ($pos === false) { return $haystack; } return Str::substr($haystack, 0, $pos); }); } if (!Str::hasMacro('between')) { /** * Get the substring between the given start and end. * * @param string $string * @param string $start * @param string $end * * @return string */ Str::macro('between', function ($string, $start, $end) { if ($startPos = strpos($string, $start)) { $result = substr($string, $startPos + strlen($start));
/** * 批量更新成绩 * @author FuRongxin * @date 2016-05-06 * @version 2.1 * @param \Illuminate\Http\Request $request 更新成绩请求 * @param string $kcxh 12位课程序号 * @return \Illuminate\Http\Response 学生成绩 */ public function batchUpdate(Request $request, $kcxh) { if ($request->isMethod('put')) { $inputs = $request->all(); $snos = array_unique(array_map(function ($val) { return Str::substr($val, 0, 12); }, array_filter(array_keys($inputs), function ($val) { return is_numeric($val); }))); $task = Task::whereKcxh($kcxh)->whereNd(session('year'))->whereXq(session('term'))->whereJsgh(Auth::user()->jsgh)->firstOrFail(); $ratios = []; $items = Ratio::whereFs($task->cjfs)->orderBy('id')->get(); foreach ($items as $ratio) { $ratios[] = ['id' => $ratio->id, 'name' => $ratio->idm, 'value' => $ratio->bl / $ratio->mf, 'allow_failed' => $ratio->jg]; } foreach ($snos as $sno) { $student = Score::whereNd(session('year'))->whereXq(session('term'))->whereKcxh($kcxh)->whereXh($sno)->firstOrFail(); $rules = []; foreach ($items as $item) { $rules[$student->xh . $item->id] = 'numeric|min:0|max:100'; } $rules[$student->xh . 'kszt'] = 'numeric'; $this->validate($request, $rules); foreach ($items as $item) { $student->{'cj' . $item->id} = isset($inputs[$student->xh . $item->id]) ? $inputs[$student->xh . $item->id] : 0; } if (isset($inputs[$student->xh . 'kszt'])) { $student->kszt = $inputs[$student->xh . 'kszt']; } $total = 0; $fails = []; foreach ($ratios as $ratio) { if (config('constants.score.passline') > $student->{'cj' . $ratio['id']} && config('constants.status.enable') == $ratio['allow_failed']) { $fails[] = $student->{'cj' . $ratio['id']}; } else { $total += $student->{'cj' . $ratio['id']} * $ratio['value']; } } $student->zpcj = round(empty($fails) ? $total : min($fails)); $student->save(); } } return redirect()->route('score.edit', $kcxh)->withStatus('保存成绩成功'); }
/** * 教学计划 * @author FuRongxin * @date 2016-03-24 * @version 2.0 * @return object 所属对象 */ public function plan() { return $this->belongsTo('App\\Models\\Plan', 'zy', 'zy')->whereNj($this->nj)->whereZsjj($this->zsjj)->whereKch(Str::substr($this->kcxh, 2, 8)); }
/** * 12位课程序号转换为8位课程号 * @author FuRongxin * @date 2016-03-12 * @version 2.0 * @param string $kcxh 12位课程序号 * @return string 8位课程号 */ public static function getCno($kcxh) { return Str::substr($kcxh, 2, 8); }
/** * Returns the portion of string specified by the start and length parameters. * * @param int $start * @param int|null $length * @return static */ public function substring($start, $length = null) { return new static(Str::substr($this->string, $start, $length)); }
/** * @param string $path * @param string $dots * @param \Illuminate\Support\Collection $data * @return \Illuminate\Support\Collection */ protected function pathSplit($path, $dots, $data = null) { $dots = explode(',', $dots); $data = $data ? $data : new Collection(); $offset = 0; foreach ($dots as $dot) { $data->push(Str::substr($path, $offset, $dot)); $offset += $dot; } return $data; }