Skip to content

Instantly share code, notes, and snippets.

View gist:e282b41494b0b25f7e80
<?php
foreach (range(1, 10) as $n) {
echo "calculating $n connection...\n";
$c = new Connection($n, $n === 1 ? null : $c);
echo 'got answer. connection patterns: ', count($c->answers), "\n";
}
class Connection {
View gist:470538
<?php
if (!empty($this->params['path'])) {
$path = $this->params['path'];
$type = array_shift($path);
$types = array('origin', 'incomplete', 'review');
$types = array_diff($types, array($type));
$moveTo = '<h4>' . __('Move to', true) . '</h4>';
$copyTo = '<h4>' . __('Copy to', true) . '</h4>';
$this->Menu->add('actions', $moveTo, false, null, array('escapeTitle' => false));
$this->Menu->add('actions', $copyTo, false, null, array('escapeTitle' => false));
@hiromi2424
hiromi2424 / gist:759536
Created Dec 30, 2010
dynamic call static method
View gist:759536
class StaticClass {
function hoge() {
echo 'hoge';
}
function piyo() {
echo 'piyo';
}
}
foreach (array('hoge', 'piyo') as $name) {
View webroot_controller.php
<?php
class WebrootController extends AppController {
var $uses = null;
var $_ext = '.php';
function show() {
$path = func_get_args();
$path = implode(DS, $path);
if (in_array($path, array('index', 'css', 'test'))) {
@hiromi2424
hiromi2424 / app_fixture_manager.php
Created Feb 6, 2011
auto loading fixtures for cakephp 2.x
View app_fixture_manager.php
<?php
require_once CAKE_TESTS_LIB . 'cake_fixture_manager.php';
class AppFixtureManager extends CakeFixtureManager {
public function fixturize(CakeTestCase $test) {
$this->_autoLoadFixtures($test);
@hiromi2424
hiromi2424 / app_controller.php
Created Mar 4, 2011
service model example for CakePHP
View app_controller.php
<?php
class AppController extends Controller {
var $uses = array('Service');
}
@hiromi2424
hiromi2424 / articles_controller.php
Created Mar 6, 2011
service model example with transaction for CakePHP
View articles_controller.php
<?php
class ArticlesController extends AppController {
function add() {
if (!empty($this->data)) {
if ($this->Service->transaction()->saveNewArticle($this->data)) {
$this->Session->setFlash(__('Article has been saved.', true));
$this->redirect(array('action' => 'index'));
} else {
View gist:857327
<?php
class Service {
function addArticle($data) {
$this->Transaction->begin();
if (!$this->Article->save($this->Article->create($data))) {
$this->Transaction->begin();
return false;
}
$this->Transaction->commit();
View SiteAdminUser.php
<?php
namespace App\Model\Entity;
use Cake\ORM\Entity;
use Cake\Auth\DefaultPasswordHasher;
/**
* SiteAdminUser Entity.
*
* @property int $id
@hiromi2424
hiromi2424 / electricity.js
Created Mar 25, 2011
one-liner for japan electricity usage api
View electricity.js
<script type="text/javascript">(function () { $.ajax({type: "GET", url: "http://tepco-usage-api.appspot.com/latest.json", dataType: "jsonp", success: function (data) { document.write(parseInt(100 * data.usage/data.capacity) + "%"); } }); })(); </script>