-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add "watch" functionality (fix koel#213)
- Loading branch information
An Phan
committed
Feb 2, 2016
1 parent
3e6922f
commit 46f6141
Showing
8 changed files
with
382 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
namespace App\Events; | ||
|
||
class LibraryChanged extends Event | ||
{ | ||
/** | ||
* Create a new event instance. | ||
*/ | ||
public function __construct() | ||
{ | ||
} | ||
|
||
/** | ||
* Get the channels the event should be broadcast on. | ||
* | ||
* @return array | ||
*/ | ||
public function broadcastOn() | ||
{ | ||
return []; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
<?php | ||
|
||
namespace App\Helpers; | ||
|
||
class FSWatchRecord | ||
{ | ||
/** | ||
* The event separator used in our fswatch command. | ||
*/ | ||
const FSWATCH_FLAG_SEPARATOR = '::'; | ||
|
||
/** | ||
* Path of the file/directory that triggers the fswatch event. | ||
* | ||
* @var string | ||
*/ | ||
protected $path; | ||
|
||
/** | ||
* The flags of the fswatch event. | ||
* | ||
* @var array | ||
*/ | ||
protected $eventFlags; | ||
|
||
/** | ||
* Construct an FSWatchRecord object for a record string. | ||
* | ||
* @param string $string The record string, e.g. | ||
* "/full/path/to/changed/file Renamed::IsFile" | ||
*/ | ||
public function __construct($string) | ||
{ | ||
$parts = explode(' ', $string); | ||
$this->eventFlags = explode(self::FSWATCH_FLAG_SEPARATOR, array_pop($parts)); | ||
$this->path = implode(' ', $parts); | ||
} | ||
|
||
/** | ||
* Determine if the file/directory is deleted from the system. | ||
* We can't rely on fswatch, since the event is OS-dependent. | ||
* For example, deleting on OSX will be reported as "Renamed", as | ||
* the file/directory is "renamed" into the Trash folder. | ||
* | ||
* @return boolean | ||
*/ | ||
public function isDeleted() | ||
{ | ||
return !file_exists($this->path); | ||
} | ||
|
||
/** | ||
* Determine if the object is renamed. | ||
* | ||
* @return boolean | ||
*/ | ||
public function isRenamed() | ||
{ | ||
return in_array('Renamed', $this->eventFlags); | ||
} | ||
|
||
/** | ||
* Determine if the changed object is a file. | ||
* | ||
* @return bool | ||
*/ | ||
public function isFile() | ||
{ | ||
return in_array('IsFile', $this->eventFlags); | ||
} | ||
|
||
/** | ||
* Determine if the changed object is a directory. | ||
* | ||
* @return bool | ||
*/ | ||
public function isDir() | ||
{ | ||
return in_array('IsDir', $this->eventFlags); | ||
} | ||
|
||
/** | ||
* Get the full path of the changed file/directory. | ||
* | ||
* @return string | ||
*/ | ||
public function getPath() | ||
{ | ||
return $this->path; | ||
} | ||
|
||
/** | ||
* Get the event flags of the fswatch record. | ||
* | ||
* @return array | ||
*/ | ||
public function getEventFlags() | ||
{ | ||
return $this->eventFlags; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
namespace App\Listeners; | ||
|
||
use App\Models\Album; | ||
use App\Models\Artist; | ||
use App\Models\Song; | ||
|
||
class TidyLibrary | ||
{ | ||
/** | ||
* Create the event listener. | ||
*/ | ||
public function __construct() | ||
{ | ||
} | ||
|
||
/** | ||
* Fired every time a LibraryChanged event is triggered. | ||
* Remove empty albums and artists from our system. | ||
*/ | ||
public function handle() | ||
{ | ||
$inUseAlbums = Song::select('album_id')->groupBy('album_id')->get()->lists('album_id'); | ||
$inUseAlbums[] = Album::UNKNOWN_ID; | ||
Album::whereNotIn('id', $inUseAlbums)->delete(); | ||
|
||
$inUseArtists = Album::select('artist_id')->groupBy('artist_id')->get()->lists('artist_id'); | ||
$inUseArtists[] = Artist::UNKNOWN_ID; | ||
Artist::whereNotIn('id', $inUseArtists)->delete(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.