1. Go to this page and download the library: Download cobaia/marsvin library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
cobaia / marsvin example snippets
Symfony\Component\Console\Application;
use Symfony\Component\Console\Helper\HelperSet;
$console = new Application('Your Project', '1.0');
$console->addCommands(
array(
new Marsvin\Command\GenerateProviderCommand(),
new Marsvin\Command\RequestProviderCommand(),
)
);
//You are able to pass as much helper set you want, like Doctrine, Monolog, etc...
//$console->setHelperSet($helperSet);
$console->run();
namespace MyProject\Github;
use Marsvin\Provider\AbstractProvider;
use Marsvin\Provider\ProviderInterface;
use Marsvin\Requester\Adapter\BuzzAdapter;
use Marsvin\Parser\Adapter\DomAdapter;
use Marsvin\Persister\Adapter\DefaultAdapter;
class GithubProvider extends AbstractProvider implements ProviderInterface
{
public function getRequesterAdapter()
{
return new BuzzAdapter();
}
public function getParserAdapter()
{
return new DomAdapter();
}
public function getPersisterAdapter()
{
return new DefaultAdapter();
}
}
namespace MyProject\Github;
use Marsvin\Requester\AbstractRequester;
use Marsvin\Requester\RequesterInterface;
use Marsvin\Response;
class GithubRequester extends AbstractRequester implements RequesterInterface
{
const GITHUB_URL = 'https://github.com/%s?tab=repositories';
public function request()
{
$adapter = $this->getAdapter();
$profiles = array(
'krolow',
'gquental',
'moacirosa',
'fabpot',
);
$self = $this;
foreach ($profiles as $profile) {
$this->process(function () use ($self, $adapter, $profile) {
$self->done(
new Response(
$adapter->request(
sprintf(
GithubRequester::GITHUB_URL,
$profile
)
)
)
);
});
}
}
}
namespace MyProject\Github;
use Marsvin\Parser\AbstractParser;
use Marsvin\Parser\ParserInterface;
use Marsvin\Response;
use Marsvin\ResponseInterface;
use DOMXPath;
use DOMDocument;
class GithubParser extends AbstractParser implements ParserInterface
{
public function parse(ResponseInterface $response)
{
$adapter = $this->getAdapter();
$dom = $adapter->parse($response->get());
$xpath = new DOMXPath($dom);
$nodes = $xpath->query('//span[@itemprop="name"]');
$author = $nodes->item(0)->nodeValue;
$nodes = $xpath->query('//li[contains(@class, "public")]');
$projects = array();
foreach ($nodes as $node) {
array_push(
$projects,
$this->parseProject($node, $author)
);
}
$this->done(new Response($projects));
}
protected function parseProject($node, $author)
{
$doc = new DOMDocument();
$doc->appendChild($doc->importNode($node, true));
$name = $doc->getElementsByTagName('h3');
$url = $doc->getElementsByTagName('a');
$project = array(
'name' => trim($name->item(0)->nodeValue),
'url' => 'http://github.com/' . $url->item(2)->getAttribute('href'),
'author' => $author
);
return $project;
}
}
namespace MyProject\Github;
use Marsvin\Persister\AbstractPersister;
use Marsvin\Persister\PersisterInterface;
use Marsvin\Response;
use Marsvin\ResponseInterface;
class GithubPersister extends AbstractPersister implements PersisterInterface
{
public function persists(ResponseInterface $response)
{
$adapter = $this->getAdapter();
$adapter->persist($response->get());
file_put_contents('/tmp/marsvin.log', var_export($adapter->flush(), true), FILE_APPEND);
}
}