Skip to content

Commit

Permalink
Related with issue #39 (Adapt to PSR-4)
Browse files Browse the repository at this point in the history
- Move tests to "tests/Sifo" folder to reproduce the same structure under /src
- Apply correct namespace to /src/Sifo files (most of them keep the same namespace. Only those under /Cache and /Debug folders have changed.)
- Fix names for those classes under /Cache and /Debug folders.
- Separate some classes in multiple files (MysqlStatement for instance).
- Add sifo-common-instance
- Add coverall actions to travis config.
  • Loading branch information
obokaman-com committed Oct 9, 2015
1 parent 051f006 commit a9317b7
Show file tree
Hide file tree
Showing 37 changed files with 472 additions and 395 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ logs/*
instances/*/
!instances/common
vendor/
composer.lock
14 changes: 11 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,26 @@ php:
- 5.3
- 5.4
- 5.5
- 5.6
- 5.6
- 7.0
- hhvm

matrix:
allow_failures:
- php: hhvm
- php: 7.0
- php: 5.6

before_install:
- php /home/travis/.phpenv/versions/5.3.27/bin/composer.phar self-update
- composer self-update

install:
- composer install
- composer require satooshi/php-coveralls
- mkdir -p build/logs

script: phpunit --configuration phpunit.xml.dist
script:
- vendor/bin/phpunit --coverage-clover build/logs/clover.xml

after_success:
- sh -c 'if [ "$TRAVIS_PHP_VERSION" != "hhvm" ]; then php vendor/bin/coveralls -v; fi;'
49 changes: 36 additions & 13 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,38 @@
{
"name": "sifophp/sifo",
"description": "SIFO framework.",
"license": "Apache-2.0",
"keywords": [],
"homepage": "https://github.com/sifophp/SIFO",
"type": "project",
"require": {
"php": ">=5.3.2",
"smarty/smarty": "dev-master#c8ecad0",
"tpyo/amazon-s3-php-class": "0.5.*",
"phpmailer/phpmailer": "5.2.*",
"symfony/yaml": "2.6.0"
}
"name": "sifophp/sifo",
"description": "SIFO framework.",
"license": "Apache-2.0",
"keywords": [],
"homepage": "https://github.com/sifophp/SIFO",
"type": "project",
"repositories": [
{
"type": "vcs",
"url": "https://github.com/sifophp/sifo-common-instance"
}
],
"require": {
"php": ">=5.3.2",
"sifophp/sifo-common-instance": "*@dev",
"smarty/smarty": "dev-master#c8ecad0",
"tpyo/amazon-s3-php-class": "^0.5.1",
"phpmailer/phpmailer": "^5.2",
"symfony/yaml": "^2.7"
},
"require-dev": {
"phpunit/phpunit": "^4.0"
},
"autoload": {
"psr-4": {
"Sifo\\": "src/Sifo"
}
},
"autoload-dev": {
"psr-4": {
"SifoTest\\": "tests/Sifo"
}
},
"config": {
"optimize-autoloader": true
}
}
4 changes: 2 additions & 2 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
>
<testsuites>
<testsuite name="SIFO Test Suite">
<directory>./tests/libs/Sifo/</directory>
<directory>./tests/Sifo/</directory>
</testsuite>
</testsuites>
</phpunit>
</phpunit>
2 changes: 0 additions & 2 deletions src/Sifo/Bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@
/**
* Class Bootstrap
*/
require_once ROOT_PATH . '/vendor/sifophp/sifo/src/Sifo/Exceptions.php';
require_once ROOT_PATH . '/vendor/sifophp/sifo/src/Sifo/Config.php';
require_once ROOT_PATH . '/vendor/autoload.php';

class Bootstrap
Expand Down
17 changes: 11 additions & 6 deletions src/Sifo/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,15 @@

namespace Sifo;

use Sifo\Cache\Base;
use Sifo\Cache\Disk;
use Sifo\Cache\Memcache;
use Sifo\Cache\Memcached;

/**
* Proxy class that handles all Cache types in a single interface.
*/
class Cache extends CacheBase
class Cache extends Base
{

const CACHE_TYPE_AUTODISCOVER = 'AUTODISCOVER';
Expand Down Expand Up @@ -60,15 +65,15 @@ static public function getInstance( $type = self::CACHE_TYPE_AUTODISCOVER, $lock
case self::CACHE_TYPE_MEMCACHED:
// http://php.net/manual/en/book.memcached.php
// Memcached offers more methods than Memcache (like append, cas, replaceByKey...)
self::$instance[$type][$lock_enabled] = new CacheMemcached();
self::$instance[$type][$lock_enabled] = new Memcached();
break;
case self::CACHE_TYPE_MEMCACHE:
// http://php.net/manual/en/book.memcache.php:
self::$instance[$type][$lock_enabled] = new CacheMemcache();
self::$instance[$type][$lock_enabled] = new Memcache();
break;
case self::CACHE_TYPE_DISK:
// Use cache disk instead:
self::$instance[$type][$lock_enabled] = new CacheDisk();
self::$instance[$type][$lock_enabled] = new Disk();
break;
default:
throw new Exception_500( 'Unknown cache type requested' );
Expand All @@ -82,7 +87,7 @@ static public function getInstance( $type = self::CACHE_TYPE_AUTODISCOVER, $lock
trigger_error( 'Memcached is down! Falling back to Disk cache if available...' );

// Use cache disk instead:
self::$instance[$type][$lock_enabled] = new CacheDisk();
self::$instance[$type][$lock_enabled] = new Disk();
self::$cache_type = self::CACHE_TYPE_DISK;
}

Expand Down Expand Up @@ -122,4 +127,4 @@ static protected function discoverCacheType()
}


}
}
17 changes: 11 additions & 6 deletions src/Sifo/Cache/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,17 @@
*
*/

namespace Sifo;
namespace Sifo\Cache;

use Sifo\Config;
use Sifo\Domains;
use Sifo\FilterCookie;
use Sifo\FilterGet;

/**
* Common methods available to every Cache instance.
*/
class CacheBase
class Base
{
/**
* Define the format of the stored cache tag.
Expand Down Expand Up @@ -119,19 +124,19 @@ public function get( $key )
return false;
}

$lock = CacheLock::getInstance( $sha1, $this->cache_object );
$lock = Lock::getInstance( $sha1, $this->cache_object );

if ( $lock->isLocked() )
{
do
{
usleep( CacheLock::WAIT_TIME );
usleep( Lock::WAIT_TIME );
}
while( $lock->isLocked() );

if ( !( $content = $this->cache_object->get( $sha1 ) ) )
{
trigger_error( "Cache lock timeout.Lock for $key (SHA1: $sha1) has not released after ".CacheLock::TTL." seconds of script running.", E_USER_WARNING );
trigger_error( "Cache lock timeout.Lock for $key (SHA1: $sha1) has not released after ".Lock::TTL." seconds of script running.", E_USER_WARNING );
}
}
else
Expand Down Expand Up @@ -172,7 +177,7 @@ public function set( $key, $content, $expiration )

if( $this->use_locking )
{
CacheLock::getInstance( $key, $this->cache_object )->release();
Lock::getInstance( $key, $this->cache_object )->release();
}

return $set_result;
Expand Down
8 changes: 5 additions & 3 deletions src/Sifo/Cache/Disk.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,16 @@
*
*/

namespace Sifo;
namespace Sifo\Cache;

use Sifo\Bootstrap;

/**
* Caching system based on Disk. Only TEXT objects can be cached.
*
* Serialize data before setting elements if needed.
*/
class CacheDisk extends CacheBase
class Disk extends Base
{
/**
* Creates the CacheDisk object and inits the "cache_object" used by parent.
Expand Down Expand Up @@ -212,4 +214,4 @@ private function getPathBase()
{
return ROOT_PATH . '/instances/' . Bootstrap::$instance . '/templates/_smarty/cache/';
}
}
}
4 changes: 2 additions & 2 deletions src/Sifo/Cache/Lock.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
*
*/

namespace Sifo;
namespace Sifo\Cache;

class CacheLock
class Lock
{
/**
* Maximum time a lock is effective.
Expand Down
16 changes: 7 additions & 9 deletions src/Sifo/Cache/Memcache.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,20 @@
* This class is based on Memcache object implementation: http://www.php.net/manual/en/book.memcache.php
*
*/
namespace Sifo;
use \Memcache;
namespace Sifo\Cache;

/**
* Wrapper for the PECL Memcache extension.
*
* @see http://www.php.net/manual/en/memcache.installation.php
*/
class CacheMemcache extends CacheBase
class Memcache extends Base
{
protected $cache_object = null;
/** @var \Memcache */
protected $cache_object;

/**
* Returns an instance of the Memcache object with the configured servers.
*
* @return Memcache
* Returns an instance of the \Memcache object with the configured servers.
*/
public function __construct()
{
Expand All @@ -43,7 +41,7 @@ public function __construct()
$servers = array( array( '127.0.0.1' => 11211 ) );
}

$this->cache_object = new \CacheMemcacheAdapter();
$this->cache_object = new MemcacheAdapter();

foreach ( $servers[0] as $server => $port )
{
Expand All @@ -52,4 +50,4 @@ public function __construct()

return $this->cache_object;
}
}
}
46 changes: 24 additions & 22 deletions src/Sifo/Cache/MemcacheAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
Expand All @@ -18,31 +18,33 @@
*
*/

namespace Sifo\Cache;

/**
* Common methods available to every Cache instance.
*/
class CacheMemcacheAdapter extends Memcache
class MemcacheAdapter extends \Memcache
{

/**
* Stores the given "content" under the key "$key" on the memcached server.
*
* Parameter expire is expiration time in seconds. If it's 0, the item never expires (but memcached server doesn't
* guarantee this item to be stored all the time, it could be deleted from the cache to make place for other items)
*
* The params order is changed to make it compatible with the rest of caching systems.
*
* @param string $key
* @param mixed $content
* @param integer $expire Timestamp or number of seconds until expiration. If passed in seconds value over 30 days is not understood.
*
* @return boolean True on success or false on failure.
*/
public function set( $key, $content, $expire = 0 )
{
// Compression parameter is not needed in the framework implementation, also it does not work well with small values.
$compress = 0; // or MEMCACHE_COMPRESSED for compression.
/**
* Stores the given "content" under the key "$key" on the memcached server.
*
* Parameter expire is expiration time in seconds. If it's 0, the item never expires (but memcached server doesn't
* guarantee this item to be stored all the time, it could be deleted from the cache to make place for other items)
*
* The params order is changed to make it compatible with the rest of caching systems.
*
* @param string $key
* @param mixed $content
* @param integer $expire Timestamp or number of seconds until expiration. If passed in seconds value over 30 days is not understood.
*
* @return boolean True on success or false on failure.
*/
public function set($key, $content, $expire = 0)
{
// Compression parameter is not needed in the framework implementation, also it does not work well with small values.
$compress = 0; // or MEMCACHE_COMPRESSED for compression.

return parent::set( $key, $content, $compress, $expire );
}
return parent::set($key, $content, $compress, $expire);
}
}
9 changes: 6 additions & 3 deletions src/Sifo/Cache/Memcached.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,18 @@
*
*/

namespace Sifo;
namespace Sifo\Cache;

use Sifo\Config;
use Sifo\Exception_Configuration;

/**
* Provides an interface to communicate with the Memcached server.
*
* This class is based on Memcached PHP's object implementation: http://www.php.net/manual/es/book.memcached.php
*
*/
class CacheMemcached extends CacheBase
class Memcached extends Base
{

/**
Expand Down Expand Up @@ -55,4 +58,4 @@ public function __construct()

return $this->cache_object;
}
}
}
2 changes: 1 addition & 1 deletion src/Sifo/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ protected function __construct( $instance_name )
$this->instance_name = $instance_name;
if ( $instance_name === 'tests' )
{
$this->config_path = ROOT_PATH . '/' . $instance_name ."/config/";
$this->config_path = ROOT_PATH . '/vendor/sifophp/sifo-common-instance/config/';
}
else
{
Expand Down
Loading

0 comments on commit a9317b7

Please sign in to comment.