// choose a random song $pos = rand(0, count($songs) - 1); // turn on shuffle Music::shuffle(true); } // start playing the selected song Music::send('play', $pos); // redirect to "now playing" header('Location: /'); die; }); // artist/1/song - list of all songs for artist=1 // $klein->respond('GET', '/artist/[:artist]/song', function ($request, $response) { // get the parameters $artist = Music::decode($request->param('artist')); // get the song list $list = Artist::getSongs($artist); // walk the array and construct URLs // The encoded URL value is actually "artist name|album title". The artist // name is included to ensure that albums with the same name are not // conflated and the pipe character is a delimiter array_walk($list, function (&$v, $k) use($artist) { $v = array('name' => $v['Title'], 'url' => '/artist/' . Music::encode($artist) . '/song/' . Music::encode($v['file'])); }); // build the "previous" link data $previous = array('path' => '/artist', 'text' => 'Artists'); // build the shuffle link $shuffle = '/artist/' . Music::encode($artist) . '/song/shuffle'; return ListPage::render($artist, $previous, $shuffle, false, $list); });
$v = array('name' => $v['Title'], 'url' => '/album/' . Music::encode($artist . '|' . $album) . '/song/' . Music::encode($v['file'])); }); // build the "previous" link data $previous = array('path' => '/album', 'text' => 'Albums'); // build the shuffle link $shuffle = '/album/' . Music::encode($artist . '|' . $album) . '/song/shuffle'; return ListPage::render($album, $previous, $shuffle, false, $list); }); // album/1/song/2 - load all songs for album=1, play song=2, go to nowplaying // $klein->respond('GET', '/album/[:album]/song/[:song]', function ($request, $response) { // get the parameters // get the artist & album values list($artist, $album) = explode('|', Music::decode($request->param('album'))); $song = $request->param('song'); $song = $song == 'shuffle' ? 'shuffle' : Music::decode($song); // clear the playlist Music::send('clear'); // get the list of songs $songs = Album::getSongs($artist, $album); // load the playlist with the requested songs (and figure out the current // song position) $pos = 0; for ($i = 0; $i < count($songs); $i++) { // add the current song to the current playlist Music::send('add', $songs[$i]['file']); // see if the current song is the one the user selected if ($songs[$i]['file'] == $song) { $pos = $i; } }
/** * Build a list of artists from a query * * @param array $query * The URI, parsed into an array of elements * * @return array */ public static function query($query = array()) { // artist - list of artist // list: artist // artist/1/album - list of albums for artist=1 // search: artist, ID - extract album names // artist/1/album/2/song - list of songs for artist=1, album=2 // search: artist, ID, album, ID - extract song names // artist/1/album/2/song/3 - load all songs for artist=1, album=2, play song=3, go to nowplaying // search: artist, ID, album, ID, song, ID - get song info & start playing // artist/1/song - list of all songs for artist=1 // search: artist, ID - get song titles // chop the array up into pairs of entries $query = array_chunk($query, 2); // start building the command $command = null; $args = null; // step through the chunks while (count($query) > 0) { $a = array_shift($query); // is it a single element array with the value 'artist'? if (count($a) == 1 && $a[0] == 'artist') { // Yes! update the command and exit the loop $command = 'list'; $args = array('artist'); break; } // No. add to the command $command = 'search'; $args[] = $a[0]; if (isset($a[1])) { $args[] = Music::decode($a[1]); } // $args[] = $a[1]; } // at this point, the last item in the $args array tells us what kind // of list we want $list = array_pop($args); // is the $list values actually "song"? if ($list == 'song') { // Yes! We actually want the "title" tag from the results $list = 'title'; } // run the command $result = Music::send($command, $args); Kint::dump($result['values']); // build the appropriate type of list return static::buildListOfKeys($list, $result['values']); }