Created
December 10, 2013 04:55
-
-
Save felipepodesta/7885899 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
{% autoescape false %} | |
<?php | |
namespace SRF\Bundles\CloudStackClientBundle\Client; | |
use SRF\Bundles\CloudStackClientBundle\Exceptions\ApiClientException; | |
/** | |
* | |
* {{ class.name }} | |
* | |
* This class was generated by a script. Please be careful changing it. | |
* If something will not work properly, try to regenerate this running $ php app/console cloudstack:generate:api-client --force | |
* | |
* @since: {{ class.generated }} | |
* @package SRF\Bundles\CloudStackClientBundle\Client | |
*/ | |
class {{ class.name }} extends AbstractClient | |
{ | |
{% for method in class.methods %} | |
{{ method }} | |
{% endfor %} | |
} | |
{% endautoescape %} |
This file contains hidden or 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
<?php | |
use Symfony\Component\DomCrawler\Crawler; | |
use RuntimeException; | |
// crate crawler instance from body HTML code | |
$crawler = new Crawler($response->getBody(true)); | |
// apply css selector filter | |
$filter = $crawler->filter('div.apismallbullet_box'); | |
$result = array(); | |
if (iterator_count($filter) > 1) { | |
// iterate over filter results | |
foreach ($filter as $i => $content) { | |
// create crawler instance for result | |
$cralwer = new Crawler($content); | |
// extract the values needed | |
$result[$i] = array( | |
'topic' => $crawler->filter('h5')->text(); | |
'className' => trim(str_replace(' ', '', $result[$i]['topic'])) . 'Client' | |
); | |
} | |
} else { | |
throw new RuntimeException('Got empty result processing the dataset!'); | |
} |
This file contains hidden or 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
<? php | |
namespace YMC\Example\Logging; | |
use Monolog\Logger; | |
use Monolog\Handler\StreamHandler; | |
use Psr\Log\LoggerInterface; | |
use Psr\Log\LoggerAwareInterface; | |
class ExampleLogging implements LoggerAwareInterface { | |
/** | |
* @var LoggerInterface | |
*/ | |
private $logger; | |
/** | |
* @param LoggerInterface $logger | |
*/ | |
public function setLogger(LoggerInterface $logger){ | |
$this->logger = $logger; | |
} | |
public function doSomething() { | |
$this->logger->info('Start doing something'); | |
// ... | |
$this->logger->error('An error occured'); | |
// ... | |
$this->logger->debug('Some debug-only infos...'); | |
} | |
} | |
// #### usage #### | |
// create a log channel | |
$logger = new Logger('name'); | |
$logger->pushHandler(new StreamHandler('path/to/your.log', Logger::WARNING)); | |
// create example class instance | |
$example = new ExampleLogging(); | |
$example->setLogger($logger); | |
$example->doSomething(); | |
// output logs | |
print file_get_contents('path/to/your.log', FILE_USE_INCLUDE_PATH); |
This file contains hidden or 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
{% autoescape false %} | |
/** | |
* {{ method.description }} | |
* | |
{% for param in method.params %} | |
* @param string ${{ param.name }} {{ param.description }}{% if not param.required %} (optional){% endif %}. | |
{% endfor %} | |
* @throws ApiClientException | |
* @return \stdClass | |
*/ | |
public function {{ method.name }}({% for param in method.params %}${{ param.name }}{% if not param.required %} = null{% endif %}{% if not loop.last %}, {% endif %}{% endfor %}) | |
{ | |
{% for param in method.params if param.required %} | |
if(!is_string(${{ param.name }}) or empty(${{ param.name }})) { | |
throw new ApiClientException("Value for parameter ${{ param.name }} is not a strig or empty!"); | |
}; | |
{% endfor %} | |
return $this->execute( | |
'{{ method.name }}', | |
array( | |
{% for param in method.params %} | |
'{{ param.name }}' => ${{ param.name }}{% if not loop.last %},{% endif %}{{ " " }} | |
{% endfor %} | |
) | |
); | |
} | |
{% endautoescape %} |
This file contains hidden or 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
<?php | |
use Guzzle\Http\Client; | |
// create http client instance | |
$client = new Client('http://download.cloud.com/releases'); | |
// create a request | |
$request = $client->get('/3.0.6/api_3.0.6/TOC_Domain_Admin.html'); | |
// send request / get response | |
$response = $request->send(); | |
// this is the response body from the requested page (usually html) | |
$result = $response->getBody(); |
This file contains hidden or 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
<?php | |
// create template variables | |
$result = // put your data array here | |
$methods = array(); | |
$classname = $result['classname']; | |
// get twig environement (symfony2) | |
$twig = $this->get('templateing'); | |
// OR create twig environement (non symfony) | |
$loader = new Twig_Loader_Filesystem('/path/to/templates'); | |
$twig = new Twig_Environment($loader, array( | |
'cache' => '/path/to/compilation_cache', | |
)); | |
// render method data | |
foreach($result['methods'] as $method) { | |
$methods[] = $twig->render( | |
'generator/method.php.twig', | |
array( | |
'method' => array( | |
'name' => trim($method['name']), | |
'description' => wordwrap($method['description'], 80, "\n * "), | |
'params' => $method['parameters'] | |
) | |
) | |
); | |
} | |
// render class data and include rendered methods | |
$classContentString = $twig->render( | |
'generator/class.php.twig', | |
array( | |
'class' => array( | |
'name' => trim($className), | |
'generated' => date(DATE_RFC822), | |
'methods' => $methods | |
) | |
) | |
); | |
// write file | |
file_put_contents($classname . '.php', $classContentString); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment