From cb589eb91ec9b2977a44c89bf17212b63a0e16e3 Mon Sep 17 00:00:00 2001 From: Mike Bronner Date: Tue, 3 Oct 2017 11:59:42 -0700 Subject: [PATCH 1/4] Removed comment --- src/CachedBuilder.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/CachedBuilder.php b/src/CachedBuilder.php index fd073f8..f4dcb4f 100644 --- a/src/CachedBuilder.php +++ b/src/CachedBuilder.php @@ -73,7 +73,6 @@ protected function getQueryColumns(array $columns) : string protected function getWhereClauses() : string { - // dump($this->query->wheres); return collect($this->query->wheres)->reduce(function ($carry, $where) { if (! $where['column'] ?? false) { return $carry . ''; From 93b8605c3511c3b8a8365c0d7b0376ac5e6730b8 Mon Sep 17 00:00:00 2001 From: Mike Bronner Date: Tue, 3 Oct 2017 15:04:54 -0700 Subject: [PATCH 2/4] Fixed where clause parsing to provide for NotNull --- src/CachedBuilder.php | 1 + tests/Fixtures/UncachedBook.php | 4 ++-- tests/Fixtures/UncachedProfile.php | 4 ++-- tests/Fixtures/UncachedPublisher.php | 4 ++-- tests/Fixtures/UncachedStore.php | 4 ++-- tests/Unit/CachedBuilderTest.php | 24 +++++++++--------------- 6 files changed, 18 insertions(+), 23 deletions(-) diff --git a/src/CachedBuilder.php b/src/CachedBuilder.php index f4dcb4f..39e998d 100644 --- a/src/CachedBuilder.php +++ b/src/CachedBuilder.php @@ -85,6 +85,7 @@ protected function getWhereClauses() : string } $value = $where['type'] === 'Null' ? 'null' : $value; + $value = $where['type'] === 'NotNull' ? 'notnull' : $value; return "{$carry}-{$where['column']}_{$value}"; }) ?: ''; diff --git a/tests/Fixtures/UncachedBook.php b/tests/Fixtures/UncachedBook.php index 0e92d45..18aa289 100644 --- a/tests/Fixtures/UncachedBook.php +++ b/tests/Fixtures/UncachedBook.php @@ -1,10 +1,10 @@ push( - (new Author)->with('books.publisher') - ->first() - ); + $authors = collect([(new Author)->with('books.publisher') + ->first()]); + $key = 'genealabslaravelmodelcachingtestsfixturesauthor-books-books.publisher-first'; $tags = [ 'genealabslaravelmodelcachingtestsfixturesauthor', @@ -426,15 +425,10 @@ public function testNestedRelationshipEagerloading() 'genealabslaravelmodelcachingtestsfixturespublisher', ]; - $cachedResults = collect()->push( - cache()->tags($tags) - ->get($key) - ); - - $liveResults = collect()->push( - (new UncachedAuthor)->with('books.publisher') - ->first() - ); + $cachedResults = collect([cache()->tags($tags) + ->get($key)]); + $liveResults = collect([(new UncachedAuthor)->with('books.publisher') + ->first()]); $this->assertEmpty($authors->diffAssoc($cachedResults)); $this->assertEmpty($liveResults->diffAssoc($cachedResults)); @@ -443,7 +437,7 @@ public function testNestedRelationshipEagerloading() public function testLazyLoadedRelationshipResolvesThroughCachedBuilder() { $books = (new Author)->first()->books; - $key = 'genealabslaravelmodelcachingtestsfixturesbook-books.author_id_1'; + $key = 'genealabslaravelmodelcachingtestsfixturesbook-books.author_id_1-books.author_id_notnull'; $tags = [ 'genealabslaravelmodelcachingtestsfixturesbook', ]; @@ -458,7 +452,7 @@ public function testLazyLoadedRelationshipResolvesThroughCachedBuilder() public function testLazyLoadingOnResourceIsCached() { $books = (new AuthorResource((new Author)->first()))->books; - $key = 'genealabslaravelmodelcachingtestsfixturesbook-books.author_id_1'; + $key = 'genealabslaravelmodelcachingtestsfixturesbook-books.author_id_1-books.author_id_notnull'; $tags = [ 'genealabslaravelmodelcachingtestsfixturesbook', ]; From 9e1905bd62eb16dbe23332003d6317b63cb6aef3 Mon Sep 17 00:00:00 2001 From: Mike Bronner Date: Tue, 3 Oct 2017 15:08:24 -0700 Subject: [PATCH 3/4] Add .gitignore file to reduce downloads for prod environments --- .gitattributes | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 .gitattributes diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..60777e7 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,9 @@ +* text=auto + +/.github export-ignore +/.gitattributes export-ignore +/.gitignore export-ignore +/.scrutinizer.yml export-ignore +/.travis.yml export-ignore +/phpunit.xml export-ignore +/tests export-ignore From 44e9f2bcf39d485ca4263d19fd47b6ae7e56b908 Mon Sep 17 00:00:00 2001 From: Mike Bronner Date: Tue, 3 Oct 2017 15:20:14 -0700 Subject: [PATCH 4/4] Refactor where clause parsing --- src/CachedBuilder.php | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/src/CachedBuilder.php b/src/CachedBuilder.php index 39e998d..07a8e77 100644 --- a/src/CachedBuilder.php +++ b/src/CachedBuilder.php @@ -74,18 +74,15 @@ protected function getQueryColumns(array $columns) : string protected function getWhereClauses() : string { return collect($this->query->wheres)->reduce(function ($carry, $where) { - if (! $where['column'] ?? false) { - return $carry . ''; - } - - $value = $where['value'] ?? ''; + $value = array_get($where, 'value'); - if ($where['values'] ?? false) { - $value .= 'in_' . implode('_', $where['values']); + if (in_array($where['type'], ['In', 'Null', 'NotNull'])) { + $value = strtolower($where['type']); } - $value = $where['type'] === 'Null' ? 'null' : $value; - $value = $where['type'] === 'NotNull' ? 'notnull' : $value; + if (is_array(array_get($where, 'values'))) { + $value .= '_' . implode('_', $where['values']); + } return "{$carry}-{$where['column']}_{$value}"; }) ?: '';