ただ今laravel5.5を使用し勉強中なのですが、その中で用意されているhelper関数などの中身がどうなっているのかも興味があったので見ていました。
まだまだこのぐらいのソースコードをすらすら読めるレベルでないので、自分なりに日本語でコメントをつけてみました。そこで皆様には
①この考え方(コメントの書き方)では間違っている、またはこう考えてコメントにこう書いたほうが良い
②関数をずっと追っていくとキリがないのですが、皆さんはどこをどのくらいまで読んで「あ~こういうこと中でやってるのね」と理解するのか
をお尋ねしたいです。今回はold関数を例にしてみました。よろしくお願いいたします。
src/illuminate/Foundation/Validation/helper.php function old($key = null, $default = null)//フォームのkey名(emailなど)を受け取る { return app('request')->old($key, $default); //app関数実行される } //上記のapp関数の中身 if (! function_exists('app')) { //app関数が定義されているか確認 /** * Get the available container instance. * * @param string $make * @param array $parameters * @return mixed|\Illuminate\Foundation\Application */ function app($make = null, $parameters = []) { if (is_null($make)) { //$makeはここではformで送られた情報 return Container::getInstance(); } return Container::getInstance()->make($make, $parameters);//getInstance関数,make関数実行される } } public static function getInstance() { if (is_null(static::$instance)) { //static関数の$instanceが存在していなければ? static::$instance = new static; //staticをinstance化 } return static::$instance;//static関数の$instance実行?? } (以下はilluminate/Container/Container.php) public function make($abstract, array $parameters = []) { return $this->resolve($abstract, $parameters);//resolve関数実行 } //正直ここらへんから「これおっていくとキリないな。。。」と思い始める。。。 protected function resolve($abstract, $parameters = []) { $abstract = $this->getAlias($abstract); $needsContextualBuild = ! empty($parameters) || ! is_null( $this->getContextualConcrete($abstract) ); // If an instance of the type is currently being managed as a singleton we'll // just return an existing instance instead of instantiating new instances // so the developer can keep using the same objects instance every time. if (isset($this->instances[$abstract]) && ! $needsContextualBuild) { return $this->instances[$abstract]; } $this->with[] = $parameters; $concrete = $this->getConcrete($abstract); // We're ready to instantiate an instance of the concrete type registered for // the binding. This will instantiate the types, as well as resolve any of // its "nested" dependencies recursively until all have gotten resolved. if ($this->isBuildable($concrete, $abstract)) { $object = $this->build($concrete); } else { $object = $this->make($concrete); } // If we defined any extenders for this type, we'll need to spin through them // and apply them to the object being built. This allows for the extension // of services, such as changing configuration or decorating the object. foreach ($this->getExtenders($abstract) as $extender) { $object = $extender($object, $this); } // If the requested type is registered as a singleton we'll want to cache off // the instances in "memory" so we can return it later without creating an // entirely new instance of an object on each subsequent request for it. if ($this->isShared($abstract) && ! $needsContextualBuild) { $this->instances[$abstract] = $object; } $this->fireResolvingCallbacks($abstract, $object); // Before returning, we will also set the resolved flag to "true" and pop off // the parameter overrides for this build. After those two things are done // we will be ready to return back the fully constructed class instance. $this->resolved[$abstract] = true; array_pop($this->with); return $object; }
回答3件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/02/05 13:25