code
stringlengths
31
1.39M
docstring
stringlengths
23
16.8k
func_name
stringlengths
1
126
language
stringclasses
1 value
repo
stringlengths
7
63
path
stringlengths
7
166
url
stringlengths
50
220
license
stringclasses
7 values
protected function assertMoneyEURCents(int $value, Money $money) { $this->assertMoney($value, 'EUR', $money); }
@param int $value @param \Money\Money $money
assertMoneyEURCents
php
laravel/cashier-mollie
tests/BaseTestCase.php
https://github.com/laravel/cashier-mollie/blob/master/tests/BaseTestCase.php
MIT
protected function withMockedCouponRepository(Coupon $coupon = null, $couponHandler = null, $context = null) { if (is_null($couponHandler)) { $couponHandler = new FixedDiscountHandler; } if (is_null($context)) { $context = [ 'description' => 'Test coupon', 'discount' => [ 'value' => '5.00', 'currency' => 'EUR', ], ]; } if (is_null($coupon)) { $coupon = new Coupon( 'test-coupon', $couponHandler, $context ); } return $this->mock(CouponRepository::class, function ($mock) use ($coupon) { return $mock->shouldReceive('findOrFail')->with($coupon->name())->andReturn($coupon); }); }
@param \Laravel\Cashier\Coupon\Coupon $coupon @param null $couponHandler @param null $context @return CouponRepository The mocked coupon repository
withMockedCouponRepository
php
laravel/cashier-mollie
tests/BaseTestCase.php
https://github.com/laravel/cashier-mollie/blob/master/tests/BaseTestCase.php
MIT
protected function instance($abstract, $instance) { $this->app->instance($abstract, $instance); return $instance; }
Register an instance of an object in the container. Included for Laravel 5.5 / 5.6 compatibility. @param string $abstract @param object $instance @return object
instance
php
laravel/cashier-mollie
tests/BaseTestCase.php
https://github.com/laravel/cashier-mollie/blob/master/tests/BaseTestCase.php
MIT
protected function mock($abstract, \Closure $mock = null) { return $this->instance($abstract, Mockery::mock(...array_filter(func_get_args()))); }
Mock an instance of an object in the container. Included for Laravel 5.5 / 5.6 compatibility. @param string $abstract @param \Closure|null $mock @return object
mock
php
laravel/cashier-mollie
tests/BaseTestCase.php
https://github.com/laravel/cashier-mollie/blob/master/tests/BaseTestCase.php
MIT
protected function assertOrderItemCounts(Model $user, int $processed, int $unprocessed) { $this->assertEquals( $processed, $user->orderItems()->processed()->count(), 'Unexpected amount of processed orderItems.' ); $this->assertEquals( $unprocessed, $user->orderItems()->unprocessed()->count(), 'Unexpected amount of unprocessed orderItems.' ); $this->assertEquals( $processed + $unprocessed, $user->orderItems()->count(), 'Unexpected total amount of orderItems.' ); }
@param \Illuminate\Database\Eloquent\Model $user @param int $processed @param int $unprocessed
assertOrderItemCounts
php
laravel/cashier-mollie
tests/CashierTest.php
https://github.com/laravel/cashier-mollie/blob/master/tests/CashierTest.php
MIT
protected function assertFromPayloadToPayload($overrides = []) { $payload = array_filter(array_merge([ 'handler' => StartSubscription::class, 'description' => 'Monthly payment', 'subtotal' => [ 'value' => '10.00', 'currency' => 'EUR', ], 'taxPercentage' => 20, 'plan' => 'monthly-10-1', 'name' => 'default', 'quantity' => 1, ], $overrides)); $action = StartSubscription::createFromPayload( $payload, $this->getMandatedUser() ); $result = $action->getPayload(); $this->assertEquals($payload, $result); }
Check if the action can be built using the payload, and then can return the same payload. @param array $overrides @throws \Exception
assertFromPayloadToPayload
php
laravel/cashier-mollie
tests/FirstPayment/Actions/StartSubscriptionTest.php
https://github.com/laravel/cashier-mollie/blob/master/tests/FirstPayment/Actions/StartSubscriptionTest.php
MIT
public function getInvoiceInformation() { return [$this->name, $this->email]; }
Get the receiver information for the invoice. Typically includes the name and some sort of (E-mail/physical) address. @return array An array of strings
getInvoiceInformation
php
laravel/cashier-mollie
tests/Fixtures/User.php
https://github.com/laravel/cashier-mollie/blob/master/tests/Fixtures/User.php
MIT
public function getExtraBillingInformation() { return $this->extra_billing_information; }
Get additional information to be displayed on the invoice. Typically a note provided by the customer. @return string|null
getExtraBillingInformation
php
laravel/cashier-mollie
tests/Fixtures/User.php
https://github.com/laravel/cashier-mollie/blob/master/tests/Fixtures/User.php
MIT
protected function getWebhookRequest($id) { return Request::create('/', 'POST', ['id' => $id]); }
Get a request that mimics Mollie calling the webhook. @param $id @return Request
getWebhookRequest
php
laravel/cashier-mollie
tests/Http/Controllers/WebhookControllerTest.php
https://github.com/laravel/cashier-mollie/blob/master/tests/Http/Controllers/WebhookControllerTest.php
MIT
public function handle(OrderItemCollection $items) { $this->items[] = $items; return $this->result ?: $items; }
@param \Laravel\Cashier\Order\OrderItemCollection $items @return \Laravel\Cashier\Order\OrderItemCollection
handle
php
laravel/cashier-mollie
tests/Order/FakeOrderItemPreprocessor.php
https://github.com/laravel/cashier-mollie/blob/master/tests/Order/FakeOrderItemPreprocessor.php
MIT
protected function getFakePreprocessor(OrderItemCollection $items) { return (new FakeOrderItemPreprocessor)->withResult($items); }
@param \Laravel\Cashier\Order\OrderItemCollection $items @return \Laravel\Cashier\Tests\Order\FakeOrderItemPreprocessor
getFakePreprocessor
php
laravel/cashier-mollie
tests/Order/OrderItemPreprocessorCollectionTest.php
https://github.com/laravel/cashier-mollie/blob/master/tests/Order/OrderItemPreprocessorCollectionTest.php
MIT
public function findOrFailCorrect() { $this->assertInstanceOf(Plan::class, ConfigPlanRepository::findOrFail('Test')); }
@test @throws \Laravel\Cashier\Exceptions\PlanNotFoundException
findOrFailCorrect
php
laravel/cashier-mollie
tests/Plan/ConfigPlanRepositoryTest.php
https://github.com/laravel/cashier-mollie/blob/master/tests/Plan/ConfigPlanRepositoryTest.php
MIT
public function getPath(Media $media): string { return md5($media->id.config('services.media-library.salt')).'/'; }
Get the path for the given media, relative to the root storage path.
getPath
php
spatie/freek.dev
app/Services/MediaLibrary/CustomPathGenerator.php
https://github.com/spatie/freek.dev/blob/master/app/Services/MediaLibrary/CustomPathGenerator.php
MIT
public function getPathForConversions(Media $media): string { return md5($media->id.config('services.media-library.salt')).'/conversions/'; }
Get the path for conversions of the given media, relative to the root storage path.
getPathForConversions
php
spatie/freek.dev
app/Services/MediaLibrary/CustomPathGenerator.php
https://github.com/spatie/freek.dev/blob/master/app/Services/MediaLibrary/CustomPathGenerator.php
MIT
public function getPathForResponsiveImages(Media $media): string { return md5($media->id.config('services.media-library.salt')).'/responsive-images/'; }
Get the path for responsive images of the given media, relative to the root storage path.
getPathForResponsiveImages
php
spatie/freek.dev
app/Services/MediaLibrary/CustomPathGenerator.php
https://github.com/spatie/freek.dev/blob/master/app/Services/MediaLibrary/CustomPathGenerator.php
MIT
public function __construct($path, $modifiedTime, $storage) { $this->path = $path; $this->modifiedTime = $modifiedTime; $this->storage = $storage; }
Creates the chunk file. @param string $path @param int $modifiedTime @param ChunkStorage $storage
__construct
php
pionl/laravel-chunk-upload
src/ChunkFile.php
https://github.com/pionl/laravel-chunk-upload/blob/master/src/ChunkFile.php
MIT
public function getPath() { return $this->path; }
@return string relative to the disk
getPath
php
pionl/laravel-chunk-upload
src/ChunkFile.php
https://github.com/pionl/laravel-chunk-upload/blob/master/src/ChunkFile.php
MIT
public function move($pathTo) { return $this->storage->disk()->move($this->path, $pathTo); }
Moves the chunk file to given relative path (within the disk). @param string $pathTo @return bool
move
php
pionl/laravel-chunk-upload
src/ChunkFile.php
https://github.com/pionl/laravel-chunk-upload/blob/master/src/ChunkFile.php
MIT
public function delete() { return $this->storage->disk()->delete($this->path); }
Deletes the chunk file. @return bool
delete
php
pionl/laravel-chunk-upload
src/ChunkFile.php
https://github.com/pionl/laravel-chunk-upload/blob/master/src/ChunkFile.php
MIT
public function __toString() { return sprintf('ChunkFile %s uploaded at %s', $this->getPath(), date('Y-m-d H:i:s', $this->getModifiedTime())); }
The __toString method allows a class to decide how it will react when it is converted to a string. @return string @see http://php.net/manual/en/language.oop5.magic.php#language.oop5.magic.tostring
__toString
php
pionl/laravel-chunk-upload
src/ChunkFile.php
https://github.com/pionl/laravel-chunk-upload/blob/master/src/ChunkFile.php
MIT
public function __construct($targetFile) { // open the target file if (!$this->destinationFile = @fopen($targetFile, 'ab')) { throw new ChunkSaveException('Failed to open output stream.', 102); } }
FileMerger constructor. @param string $targetFile @throws ChunkSaveException
__construct
php
pionl/laravel-chunk-upload
src/FileMerger.php
https://github.com/pionl/laravel-chunk-upload/blob/master/src/FileMerger.php
MIT
public function appendFile($sourceFilePath) { // open the new uploaded chunk if (!$in = @fopen($sourceFilePath, 'rb')) { @fclose($this->destinationFile); throw new ChunkSaveException('Failed to open input stream', 101); } // read and write in buffs while ($buff = fread($in, 4096)) { fwrite($this->destinationFile, $buff); } @fclose($in); return $this; }
Appends given file. @param string $sourceFilePath @return $this @throws ChunkSaveException
appendFile
php
pionl/laravel-chunk-upload
src/FileMerger.php
https://github.com/pionl/laravel-chunk-upload/blob/master/src/FileMerger.php
MIT
public function close() { @fclose($this->destinationFile); }
Closes the connection to the file.
close
php
pionl/laravel-chunk-upload
src/FileMerger.php
https://github.com/pionl/laravel-chunk-upload/blob/master/src/FileMerger.php
MIT
public function handle(ChunkStorage $storage) { $verbouse = OutputInterface::VERBOSITY_VERBOSE; // try to get the old chunk files $oldFiles = $storage->oldChunkFiles(); if ($oldFiles->isEmpty()) { $this->warn('Chunks: no old files'); return; } $this->info(sprintf('Found %d chunk files', $oldFiles->count()), $verbouse); $deleted = 0; /** @var ChunkFile $file */ foreach ($oldFiles as $file) { // debug the file info $this->comment('> '.$file, $verbouse); // delete the file if ($file->delete()) { ++$deleted; } else { $this->error('> chunk not deleted: '.$file); } } $this->info('Chunks: cleared '.$deleted.' '.Str::plural('file', $deleted)); }
Clears the chunks upload directory. @param ChunkStorage $storage injected chunk storage
handle
php
pionl/laravel-chunk-upload
src/Commands/ClearChunksCommand.php
https://github.com/pionl/laravel-chunk-upload/blob/master/src/Commands/ClearChunksCommand.php
MIT
public static function config() { return app(AbstractConfig::class); }
Returns the config from the application container. @return AbstractConfig @see app()
config
php
pionl/laravel-chunk-upload
src/Config/AbstractConfig.php
https://github.com/pionl/laravel-chunk-upload/blob/master/src/Config/AbstractConfig.php
MIT
public function handlers() { return $this->get('handlers', []); }
Returns a list custom handlers (custom, override). @return array
handlers
php
pionl/laravel-chunk-upload
src/Config/FileConfig.php
https://github.com/pionl/laravel-chunk-upload/blob/master/src/Config/FileConfig.php
MIT
public function chunksDiskName() { return $this->get('storage.disk'); }
Returns the disk name to use for the chunk storage. @return string
chunksDiskName
php
pionl/laravel-chunk-upload
src/Config/FileConfig.php
https://github.com/pionl/laravel-chunk-upload/blob/master/src/Config/FileConfig.php
MIT
public function chunksStorageDirectory() { return $this->get('storage.chunks'); }
The storage path for the chunks. @return string the full path to the storage @see FileConfig::get()
chunksStorageDirectory
php
pionl/laravel-chunk-upload
src/Config/FileConfig.php
https://github.com/pionl/laravel-chunk-upload/blob/master/src/Config/FileConfig.php
MIT
public function clearTimestampString() { return $this->get('clear.timestamp'); }
Returns the time stamp string for clear command. @return string @see FileConfig::get()
clearTimestampString
php
pionl/laravel-chunk-upload
src/Config/FileConfig.php
https://github.com/pionl/laravel-chunk-upload/blob/master/src/Config/FileConfig.php
MIT
public function scheduleConfig() { return $this->get('clear.schedule'); }
Returns the shedule config array. @return array<enable,cron>
scheduleConfig
php
pionl/laravel-chunk-upload
src/Config/FileConfig.php
https://github.com/pionl/laravel-chunk-upload/blob/master/src/Config/FileConfig.php
MIT
public function chunkUseSessionForName() { return $this->get('chunk.name.use.session', true); }
Should the chunk name add a session? @return bool
chunkUseSessionForName
php
pionl/laravel-chunk-upload
src/Config/FileConfig.php
https://github.com/pionl/laravel-chunk-upload/blob/master/src/Config/FileConfig.php
MIT
public function chunkUseBrowserInfoForName() { return $this->get('chunk.name.use.browser', false); }
Should the chunk name add a ip address? @return bool
chunkUseBrowserInfoForName
php
pionl/laravel-chunk-upload
src/Config/FileConfig.php
https://github.com/pionl/laravel-chunk-upload/blob/master/src/Config/FileConfig.php
MIT
public function get($key, $default = null) { return config(self::FILE_NAME.'.'.$key, $default); }
Returns a chunks config value. @param string $key the config name is prepended to the key value @param mixed|null $default @return mixed @see \Config::get()
get
php
pionl/laravel-chunk-upload
src/Config/FileConfig.php
https://github.com/pionl/laravel-chunk-upload/blob/master/src/Config/FileConfig.php
MIT
public function __construct( $message = 'The chunk parameters are invalid', $code = 500, Exception $previous = null ) { parent::__construct($message, $code, $previous); }
ChunkInvalidValueException constructor. @param string $message @param int $code @param Exception|null $previous
__construct
php
pionl/laravel-chunk-upload
src/Exceptions/ChunkInvalidValueException.php
https://github.com/pionl/laravel-chunk-upload/blob/master/src/Exceptions/ChunkInvalidValueException.php
MIT
public function __construct($message = 'The request is missing a file', $code = 400, Exception $previous = null) { parent::__construct($message, $code, $previous); }
Construct the exception. Note: The message is NOT binary safe. @see http://php.net/manual/en/exception.construct.php @param string $message [optional] The Exception message to throw @param int $code [optional] The Exception code @param Exception $previous [optional] The previous exception used for the exception chaining. Since 5.3.0 @since 5.1.0
__construct
php
pionl/laravel-chunk-upload
src/Exceptions/UploadMissingFileException.php
https://github.com/pionl/laravel-chunk-upload/blob/master/src/Exceptions/UploadMissingFileException.php
MIT
public function __construct(Request $request, $file, $config) { $this->request = $request; $this->file = $file; $this->config = $config; }
AbstractReceiver constructor. @param Request $request @param UploadedFile $file @param AbstractConfig $config
__construct
php
pionl/laravel-chunk-upload
src/Handler/AbstractHandler.php
https://github.com/pionl/laravel-chunk-upload/blob/master/src/Handler/AbstractHandler.php
MIT
public static function canBeUsedForRequest(Request $request) { return false; }
Checks if the current abstract handler can be used via HandlerFactory. @param Request $request @return bool
canBeUsedForRequest
php
pionl/laravel-chunk-upload
src/Handler/AbstractHandler.php
https://github.com/pionl/laravel-chunk-upload/blob/master/src/Handler/AbstractHandler.php
MIT
public static function canUseSession() { // Get the session driver and check if it was started - fully inited by laravel $session = session(); $driver = $session->getDefaultDriver(); $drivers = $session->getDrivers(); // Check if the driver is valid and started - allow using session if (isset($drivers[$driver]) && true === $drivers[$driver]->isStarted()) { return true; } return false; }
Checks the current setup if session driver was booted - if not, it will generate random hash. @return bool
canUseSession
php
pionl/laravel-chunk-upload
src/Handler/AbstractHandler.php
https://github.com/pionl/laravel-chunk-upload/blob/master/src/Handler/AbstractHandler.php
MIT
public function createChunkFileName($additionalName = null, $currentChunkIndex = null) { // prepare basic name structure $array = [ $this->file->getClientOriginalName(), ]; // ensure that the chunk name is for unique for the client session $useSession = $this->config->chunkUseSessionForName(); $useBrowser = $this->config->chunkUseBrowserInfoForName(); if ($useSession && false === static::canUseSession()) { $useBrowser = true; $useSession = false; } // the session needs more config on the provider if ($useSession) { $array[] = Session::getId(); } // can work without any additional setup if ($useBrowser) { $array[] = md5($this->request->ip().$this->request->header('User-Agent', 'no-browser')); } // Add additional name for more unique chunk name if (!is_null($additionalName)) { $array[] = $additionalName; } // Build the final name - parts separated by dot $namesSeparatedByDot = [ implode('-', $array), ]; // Add the chunk index for parallel upload if (null !== $currentChunkIndex) { $namesSeparatedByDot[] = $currentChunkIndex; } // Add extension $namesSeparatedByDot[] = ChunkStorage::CHUNK_EXTENSION; // build name return implode('.', $namesSeparatedByDot); }
Builds the chunk file name per session and the original name. You can provide custom additional name at the end of the generated file name. All chunk files has .part extension. @param string|null $additionalName Make the name more unique (example: use id from request) @param string|null $currentChunkIndex Add the chunk index for parallel upload @return string @see UploadedFile::getClientOriginalName() @see Session::getId()
createChunkFileName
php
pionl/laravel-chunk-upload
src/Handler/AbstractHandler.php
https://github.com/pionl/laravel-chunk-upload/blob/master/src/Handler/AbstractHandler.php
MIT
protected function getCurrentChunkFromRequest(Request $request) { // the chunk is indexed from 1 (for 5 chunks: 1,2,3,4,5) return intval($request->get(static::KEY_CHUNK_NUMBER)); }
Returns current chunk from the request. @param Request $request @return int
getCurrentChunkFromRequest
php
pionl/laravel-chunk-upload
src/Handler/ChunksInRequestSimpleUploadHandler.php
https://github.com/pionl/laravel-chunk-upload/blob/master/src/Handler/ChunksInRequestSimpleUploadHandler.php
MIT
public function __construct(Request $request, $file, $config) { parent::__construct($request, $file, $config); $this->currentChunk = $this->getCurrentChunkFromRequest($request); $this->chunksTotal = $this->getTotalChunksFromRequest($request); }
AbstractReceiver constructor. @param Request $request @param UploadedFile $file @param AbstractConfig $config
__construct
php
pionl/laravel-chunk-upload
src/Handler/ChunksInRequestUploadHandler.php
https://github.com/pionl/laravel-chunk-upload/blob/master/src/Handler/ChunksInRequestUploadHandler.php
MIT
public static function canBeUsedForRequest(Request $request) { return $request->has(static::KEY_CHUNK_NUMBER) && $request->has(static::KEY_ALL_CHUNKS); }
Checks if the current abstract handler can be used via HandlerFactory. @param Request $request @return bool
canBeUsedForRequest
php
pionl/laravel-chunk-upload
src/Handler/ChunksInRequestUploadHandler.php
https://github.com/pionl/laravel-chunk-upload/blob/master/src/Handler/ChunksInRequestUploadHandler.php
MIT
public function startSaving($chunkStorage) { return new ChunkSave($this->file, $this, $chunkStorage, $this->config); }
Returns the chunk save instance for saving. @param ChunkStorage $chunkStorage the chunk storage @return ChunkSave @throws ChunkSaveException
startSaving
php
pionl/laravel-chunk-upload
src/Handler/ChunksInRequestUploadHandler.php
https://github.com/pionl/laravel-chunk-upload/blob/master/src/Handler/ChunksInRequestUploadHandler.php
MIT
protected function getCurrentChunkFromRequest(Request $request) { // the chunk is indexed from zero (for 5 chunks: 0,1,2,3,4) return intval($request->get(static::KEY_CHUNK_NUMBER)) + 1; }
Returns current chunk from the request. @param Request $request @return int
getCurrentChunkFromRequest
php
pionl/laravel-chunk-upload
src/Handler/ChunksInRequestUploadHandler.php
https://github.com/pionl/laravel-chunk-upload/blob/master/src/Handler/ChunksInRequestUploadHandler.php
MIT
protected function getTotalChunksFromRequest(Request $request) { return intval($request->get(static::KEY_ALL_CHUNKS)); }
Returns current chunk from the request. @param Request $request @return int
getTotalChunksFromRequest
php
pionl/laravel-chunk-upload
src/Handler/ChunksInRequestUploadHandler.php
https://github.com/pionl/laravel-chunk-upload/blob/master/src/Handler/ChunksInRequestUploadHandler.php
MIT
public function isFirstChunk() { return 1 == $this->currentChunk; }
Returns the first chunk. @return bool
isFirstChunk
php
pionl/laravel-chunk-upload
src/Handler/ChunksInRequestUploadHandler.php
https://github.com/pionl/laravel-chunk-upload/blob/master/src/Handler/ChunksInRequestUploadHandler.php
MIT
public function isLastChunk() { // the bytes starts from zero, remove 1 byte from total return $this->currentChunk == $this->chunksTotal; }
Checks if the chunk is last. @return int
isLastChunk
php
pionl/laravel-chunk-upload
src/Handler/ChunksInRequestUploadHandler.php
https://github.com/pionl/laravel-chunk-upload/blob/master/src/Handler/ChunksInRequestUploadHandler.php
MIT
public function isChunkedUpload() { return $this->chunksTotal > 1; }
Returns the current chunk index. @return bool
isChunkedUpload
php
pionl/laravel-chunk-upload
src/Handler/ChunksInRequestUploadHandler.php
https://github.com/pionl/laravel-chunk-upload/blob/master/src/Handler/ChunksInRequestUploadHandler.php
MIT
public function getChunkFileName() { return $this->createChunkFileName($this->chunksTotal); }
Returns the chunk file name. Uses the original client name and the total bytes. @return string returns the original name with the part extension @see createChunkFileName()
getChunkFileName
php
pionl/laravel-chunk-upload
src/Handler/ChunksInRequestUploadHandler.php
https://github.com/pionl/laravel-chunk-upload/blob/master/src/Handler/ChunksInRequestUploadHandler.php
MIT
public function getPercentageDone() { return ceil($this->currentChunk / $this->chunksTotal * 100); }
Returns the percentage of the uploaded file. @return int
getPercentageDone
php
pionl/laravel-chunk-upload
src/Handler/ChunksInRequestUploadHandler.php
https://github.com/pionl/laravel-chunk-upload/blob/master/src/Handler/ChunksInRequestUploadHandler.php
MIT
public function __construct(Request $request, $file, $config) { parent::__construct($request, $file, $config); $contentRange = $this->request->header(self::CONTENT_RANGE_INDEX, ''); $this->tryToParseContentRange($contentRange); }
AbstractReceiver constructor. @param Request $request @param UploadedFile $file @param AbstractConfig $config @throws ContentRangeValueToLargeException
__construct
php
pionl/laravel-chunk-upload
src/Handler/ContentRangeUploadHandler.php
https://github.com/pionl/laravel-chunk-upload/blob/master/src/Handler/ContentRangeUploadHandler.php
MIT
public static function canBeUsedForRequest(Request $request) { return (new static($request, null, null))->isChunkedUpload(); }
Checks if the current abstract handler can be used via HandlerFactory. @param Request $request @return bool @throws ContentRangeValueToLargeException
canBeUsedForRequest
php
pionl/laravel-chunk-upload
src/Handler/ContentRangeUploadHandler.php
https://github.com/pionl/laravel-chunk-upload/blob/master/src/Handler/ContentRangeUploadHandler.php
MIT
public function startSaving($chunkStorage) { return new ChunkSave($this->file, $this, $chunkStorage, $this->config); }
Returns the chunk save instance for saving. @param ChunkStorage $chunkStorage the chunk storage @return ChunkSave @throws ChunkSaveException
startSaving
php
pionl/laravel-chunk-upload
src/Handler/ContentRangeUploadHandler.php
https://github.com/pionl/laravel-chunk-upload/blob/master/src/Handler/ContentRangeUploadHandler.php
MIT
protected function tryToParseContentRange($contentRange) { // try to get the content range if (preg_match("/bytes ([\d]+)-([\d]+)\/([\d]+)/", $contentRange, $matches)) { $this->chunkedUpload = true; // write the bytes values $this->bytesStart = $this->convertToNumericValue($matches[1]); $this->bytesEnd = $this->convertToNumericValue($matches[2]); $this->bytesTotal = $this->convertToNumericValue($matches[3]); } }
Tries to parse the content range from the string. @param string $contentRange @throws ContentRangeValueToLargeException
tryToParseContentRange
php
pionl/laravel-chunk-upload
src/Handler/ContentRangeUploadHandler.php
https://github.com/pionl/laravel-chunk-upload/blob/master/src/Handler/ContentRangeUploadHandler.php
MIT
protected function convertToNumericValue($value) { $floatVal = floatval($value); if (INF === $floatVal) { throw new ContentRangeValueToLargeException(); } return $floatVal; }
Converts the string value to float - throws exception if float value is exceeded. @param string $value @return float @throws ContentRangeValueToLargeException
convertToNumericValue
php
pionl/laravel-chunk-upload
src/Handler/ContentRangeUploadHandler.php
https://github.com/pionl/laravel-chunk-upload/blob/master/src/Handler/ContentRangeUploadHandler.php
MIT
public function isFirstChunk() { return 0 == $this->bytesStart; }
Returns the first chunk. @return bool
isFirstChunk
php
pionl/laravel-chunk-upload
src/Handler/ContentRangeUploadHandler.php
https://github.com/pionl/laravel-chunk-upload/blob/master/src/Handler/ContentRangeUploadHandler.php
MIT
public function isLastChunk() { // the bytes starts from zero, remove 1 byte from total return $this->bytesEnd >= ($this->bytesTotal - 1); }
Returns the chunks count. @return int
isLastChunk
php
pionl/laravel-chunk-upload
src/Handler/ContentRangeUploadHandler.php
https://github.com/pionl/laravel-chunk-upload/blob/master/src/Handler/ContentRangeUploadHandler.php
MIT
public function isChunkedUpload() { return $this->chunkedUpload; }
Returns the current chunk index. @return bool
isChunkedUpload
php
pionl/laravel-chunk-upload
src/Handler/ContentRangeUploadHandler.php
https://github.com/pionl/laravel-chunk-upload/blob/master/src/Handler/ContentRangeUploadHandler.php
MIT
public function getBytesStart() { return $this->bytesStart; }
@return int returns the starting bytes for current request
getBytesStart
php
pionl/laravel-chunk-upload
src/Handler/ContentRangeUploadHandler.php
https://github.com/pionl/laravel-chunk-upload/blob/master/src/Handler/ContentRangeUploadHandler.php
MIT
public function getBytesEnd() { return $this->bytesEnd; }
@return int returns the ending bytes for current request
getBytesEnd
php
pionl/laravel-chunk-upload
src/Handler/ContentRangeUploadHandler.php
https://github.com/pionl/laravel-chunk-upload/blob/master/src/Handler/ContentRangeUploadHandler.php
MIT
public function getBytesTotal() { return $this->bytesTotal; }
@return int returns the total bytes for the file
getBytesTotal
php
pionl/laravel-chunk-upload
src/Handler/ContentRangeUploadHandler.php
https://github.com/pionl/laravel-chunk-upload/blob/master/src/Handler/ContentRangeUploadHandler.php
MIT
public function getChunkFileName() { return $this->createChunkFileName($this->bytesTotal); }
Returns the chunk file name. Uses the original client name and the total bytes. @return string returns the original name with the part extension @see createChunkFileName()
getChunkFileName
php
pionl/laravel-chunk-upload
src/Handler/ContentRangeUploadHandler.php
https://github.com/pionl/laravel-chunk-upload/blob/master/src/Handler/ContentRangeUploadHandler.php
MIT
public function __construct(Request $request, $file, $config) { parent::__construct($request, $file, $config); $this->fileUuid = $request->get(self::CHUNK_UUID_INDEX); }
AbstractReceiver constructor. @param Request $request @param UploadedFile $file @param AbstractConfig $config
__construct
php
pionl/laravel-chunk-upload
src/Handler/DropZoneUploadHandler.php
https://github.com/pionl/laravel-chunk-upload/blob/master/src/Handler/DropZoneUploadHandler.php
MIT
public function getChunkFileName() { return $this->createChunkFileName($this->fileUuid, $this->getCurrentChunk()); }
Builds the chunk file name from file uuid and current chunk. @return string
getChunkFileName
php
pionl/laravel-chunk-upload
src/Handler/DropZoneUploadHandler.php
https://github.com/pionl/laravel-chunk-upload/blob/master/src/Handler/DropZoneUploadHandler.php
MIT
protected function getCurrentChunkFromRequest(Request $request) { return intval($request->get(self::CHUNK_INDEX, 0)) + 1; }
Returns current chunk from the request. @param Request $request @return int
getCurrentChunkFromRequest
php
pionl/laravel-chunk-upload
src/Handler/DropZoneUploadHandler.php
https://github.com/pionl/laravel-chunk-upload/blob/master/src/Handler/DropZoneUploadHandler.php
MIT
protected function getTotalChunksFromRequest(Request $request) { return intval($request->get(self::CHUNK_TOTAL_INDEX, 1)); }
Returns current chunk from the request. @param Request $request @return int
getTotalChunksFromRequest
php
pionl/laravel-chunk-upload
src/Handler/DropZoneUploadHandler.php
https://github.com/pionl/laravel-chunk-upload/blob/master/src/Handler/DropZoneUploadHandler.php
MIT
public static function canBeUsedForRequest(Request $request) { return $request->has(self::CHUNK_UUID_INDEX) && $request->has(self::CHUNK_TOTAL_INDEX) && $request->has(self::CHUNK_INDEX); }
Checks if the current abstract handler can be used via HandlerFactory. @param Request $request @return bool
canBeUsedForRequest
php
pionl/laravel-chunk-upload
src/Handler/DropZoneUploadHandler.php
https://github.com/pionl/laravel-chunk-upload/blob/master/src/Handler/DropZoneUploadHandler.php
MIT
public static function classFromRequest(Request $request, $fallbackClass = null) { /** @var AbstractHandler $handlerClass */ foreach (static::$handlers as $handlerClass) { if ($handlerClass::canBeUsedForRequest($request)) { return $handlerClass; break; } } if (is_null($fallbackClass)) { // the default handler return static::$fallbackHandler; } return $fallbackClass; }
Returns handler class based on the request or the fallback handler. @param Request $request @param string|null $fallbackClass @return string
classFromRequest
php
pionl/laravel-chunk-upload
src/Handler/HandlerFactory.php
https://github.com/pionl/laravel-chunk-upload/blob/master/src/Handler/HandlerFactory.php
MIT
public static function register($handlerClass) { static::$handlers[] = $handlerClass; }
Adds a custom handler class. @param string $handlerClass
register
php
pionl/laravel-chunk-upload
src/Handler/HandlerFactory.php
https://github.com/pionl/laravel-chunk-upload/blob/master/src/Handler/HandlerFactory.php
MIT
public static function setHandlers($handlers) { static::$handlers = $handlers; }
Overrides the handler list. @param array $handlers
setHandlers
php
pionl/laravel-chunk-upload
src/Handler/HandlerFactory.php
https://github.com/pionl/laravel-chunk-upload/blob/master/src/Handler/HandlerFactory.php
MIT
public static function getHandlers() { return static::$handlers; }
Returns the handler list. @return array
getHandlers
php
pionl/laravel-chunk-upload
src/Handler/HandlerFactory.php
https://github.com/pionl/laravel-chunk-upload/blob/master/src/Handler/HandlerFactory.php
MIT
public function setFallbackHandler($fallbackHandler) { static::$fallbackHandler = $fallbackHandler; }
Sets the default fallback handler when the detection fails. @param string $fallbackHandler
setFallbackHandler
php
pionl/laravel-chunk-upload
src/Handler/HandlerFactory.php
https://github.com/pionl/laravel-chunk-upload/blob/master/src/Handler/HandlerFactory.php
MIT
public static function canBeUsedForRequest(Request $request) { $hasChunkParams = $request->has(static::KEY_CHUNK_NUMBER) && $request->has(static::KEY_TOTAL_SIZE) && $request->has(static::KEY_CHUNK_SIZE) && $request->has(static::KEY_CHUNK_CURRENT_SIZE); return $hasChunkParams && self::checkChunkParams($request); }
Checks if the current handler can be used via HandlerFactory. @param Request $request @return bool @throws ChunkInvalidValueException
canBeUsedForRequest
php
pionl/laravel-chunk-upload
src/Handler/NgFileUploadHandler.php
https://github.com/pionl/laravel-chunk-upload/blob/master/src/Handler/NgFileUploadHandler.php
MIT
protected static function checkChunkParams($request) { $isInteger = ctype_digit($request->input(static::KEY_CHUNK_NUMBER)) && ctype_digit($request->input(static::KEY_TOTAL_SIZE)) && ctype_digit($request->input(static::KEY_CHUNK_SIZE)) && ctype_digit($request->input(static::KEY_CHUNK_CURRENT_SIZE)); if ($request->get(static::KEY_CHUNK_SIZE) < $request->get(static::KEY_CHUNK_CURRENT_SIZE)) { throw new ChunkInvalidValueException(); } if ($request->get(static::KEY_CHUNK_NUMBER) < 0) { throw new ChunkInvalidValueException(); } if ($request->get(static::KEY_TOTAL_SIZE) < 0) { throw new ChunkInvalidValueException(); } return $isInteger; }
@param Request $request @return bool @throws ChunkInvalidValueException
checkChunkParams
php
pionl/laravel-chunk-upload
src/Handler/NgFileUploadHandler.php
https://github.com/pionl/laravel-chunk-upload/blob/master/src/Handler/NgFileUploadHandler.php
MIT
protected function getTotalChunksFromRequest(Request $request) { if (!$request->get(static::KEY_CHUNK_SIZE)) { return 0; } return intval( ceil($request->get(static::KEY_TOTAL_SIZE) / $request->get(static::KEY_CHUNK_SIZE)) ); }
Returns current chunk from the request. @param Request $request @return int
getTotalChunksFromRequest
php
pionl/laravel-chunk-upload
src/Handler/NgFileUploadHandler.php
https://github.com/pionl/laravel-chunk-upload/blob/master/src/Handler/NgFileUploadHandler.php
MIT
public function __construct(Request $request, $file, $config) { parent::__construct($request, $file, $config); $this->fileUuid = $request->get(self::CHUNK_UUID_INDEX); }
AbstractReceiver constructor. @param Request $request @param UploadedFile $file @param AbstractConfig $config
__construct
php
pionl/laravel-chunk-upload
src/Handler/ResumableJSUploadHandler.php
https://github.com/pionl/laravel-chunk-upload/blob/master/src/Handler/ResumableJSUploadHandler.php
MIT
public function getChunkFileName() { return $this->createChunkFileName(substr($this->fileUuid,0,40), $this->getCurrentChunk()); }
Append the resumable file - uuid and pass the current chunk index for parallel upload. @return string
getChunkFileName
php
pionl/laravel-chunk-upload
src/Handler/ResumableJSUploadHandler.php
https://github.com/pionl/laravel-chunk-upload/blob/master/src/Handler/ResumableJSUploadHandler.php
MIT
protected function getCurrentChunkFromRequest(Request $request) { return $request->get(self::CHUNK_NUMBER_INDEX); }
Returns current chunk from the request. @param Request $request @return int
getCurrentChunkFromRequest
php
pionl/laravel-chunk-upload
src/Handler/ResumableJSUploadHandler.php
https://github.com/pionl/laravel-chunk-upload/blob/master/src/Handler/ResumableJSUploadHandler.php
MIT
protected function getTotalChunksFromRequest(Request $request) { return $request->get(self::TOTAL_CHUNKS_INDEX); }
Returns current chunk from the request. @param Request $request @return int
getTotalChunksFromRequest
php
pionl/laravel-chunk-upload
src/Handler/ResumableJSUploadHandler.php
https://github.com/pionl/laravel-chunk-upload/blob/master/src/Handler/ResumableJSUploadHandler.php
MIT
public static function canBeUsedForRequest(Request $request) { return $request->has(self::CHUNK_NUMBER_INDEX) && $request->has(self::TOTAL_CHUNKS_INDEX) && $request->has(self::CHUNK_UUID_INDEX); }
Checks if the current abstract handler can be used via HandlerFactory. @param Request $request @return bool
canBeUsedForRequest
php
pionl/laravel-chunk-upload
src/Handler/ResumableJSUploadHandler.php
https://github.com/pionl/laravel-chunk-upload/blob/master/src/Handler/ResumableJSUploadHandler.php
MIT
public function startSaving($chunkStorage) { return new SingleSave($this->file, $this, $this->config); }
Returns the chunks ave instance for saving. @param ChunkStorage $chunkStorage the chunk storage @param AbstractConfig $config the config manager @return SingleSave
startSaving
php
pionl/laravel-chunk-upload
src/Handler/SingleUploadHandler.php
https://github.com/pionl/laravel-chunk-upload/blob/master/src/Handler/SingleUploadHandler.php
MIT
public function getChunkFileName() { return null; // never used }
Returns the chunk file name for a storing the tmp file. @return string
getChunkFileName
php
pionl/laravel-chunk-upload
src/Handler/SingleUploadHandler.php
https://github.com/pionl/laravel-chunk-upload/blob/master/src/Handler/SingleUploadHandler.php
MIT
public function isFirstChunk() { return true; }
Checks if the request has first chunk. @return bool
isFirstChunk
php
pionl/laravel-chunk-upload
src/Handler/SingleUploadHandler.php
https://github.com/pionl/laravel-chunk-upload/blob/master/src/Handler/SingleUploadHandler.php
MIT
public function isLastChunk() { return true; }
Checks if the current request has the last chunk. @return bool
isLastChunk
php
pionl/laravel-chunk-upload
src/Handler/SingleUploadHandler.php
https://github.com/pionl/laravel-chunk-upload/blob/master/src/Handler/SingleUploadHandler.php
MIT
public function isChunkedUpload() { return false; // force the `SingleSave` instance }
Checks if the current request is chunked upload. @return bool
isChunkedUpload
php
pionl/laravel-chunk-upload
src/Handler/SingleUploadHandler.php
https://github.com/pionl/laravel-chunk-upload/blob/master/src/Handler/SingleUploadHandler.php
MIT
public function getPercentageDone() { return 100; }
Returns the percentage of the upload file. @return int
getPercentageDone
php
pionl/laravel-chunk-upload
src/Handler/SingleUploadHandler.php
https://github.com/pionl/laravel-chunk-upload/blob/master/src/Handler/SingleUploadHandler.php
MIT
public function startSaving($chunkStorage) { // Build the parallel save return new ParallelSave( $this->file, $this, $chunkStorage, $this->config ); }
Returns the chunk save instance for saving. @param ChunkStorage $chunkStorage the chunk storage @return ParallelSave @throws ChunkSaveException
startSaving
php
pionl/laravel-chunk-upload
src/Handler/Traits/HandleParallelUploadTrait.php
https://github.com/pionl/laravel-chunk-upload/blob/master/src/Handler/Traits/HandleParallelUploadTrait.php
MIT
public function setPercentageDone(int $percentageDone) { $this->percentageDone = $percentageDone; return $this; }
Sets percentegage done - should be calculated from chunks count. @param int $percentageDone @return HandleParallelUploadTrait
setPercentageDone
php
pionl/laravel-chunk-upload
src/Handler/Traits/HandleParallelUploadTrait.php
https://github.com/pionl/laravel-chunk-upload/blob/master/src/Handler/Traits/HandleParallelUploadTrait.php
MIT
public function boot() { // Get the schedule config $config = $this->app->make(AbstractConfig::class); $scheduleConfig = $config->scheduleConfig(); // Run only if schedule is enabled if (true === Arr::get($scheduleConfig, 'enabled', false)) { // Wait until the app is fully booted $this->app->booted(function () use ($scheduleConfig) { // Get the scheduler instance /** @var Schedule $schedule */ $schedule = $this->app->make(Schedule::class); // Register the clear chunks with custom schedule $schedule->command('uploads:clear') ->cron(Arr::get($scheduleConfig, 'cron', '* * * * *')); }); } $this->registerHandlers($config->handlers()); }
When the service is being booted.
boot
php
pionl/laravel-chunk-upload
src/Providers/ChunkUploadServiceProvider.php
https://github.com/pionl/laravel-chunk-upload/blob/master/src/Providers/ChunkUploadServiceProvider.php
MIT
public function register() { // Register the commands $this->commands([ ClearChunksCommand::class, ]); // Register the config $this->registerConfig(); // Register the config via abstract instance $this->app->singleton(AbstractConfig::class, function () { return new FileConfig(); }); // Register the config via abstract instance $this->app->singleton(ChunkStorage::class, function ($app) { /** @var AbstractConfig $config */ $config = $app->make(AbstractConfig::class); // Build the chunk storage return new ChunkStorage($this->disk($config->chunksDiskName()), $config); }); /* * Bind a FileReceiver for dependency and use only the first object */ $this->app->bind(FileReceiver::class, function ($app) { /** @var Request $request */ $request = $app->make('request'); // Get the first file object - must be converted instances of UploadedFile $file = Arr::first($request->allFiles()); // Build the file receiver return new FileReceiver($file, $request, HandlerFactory::classFromRequest($request)); }); }
Register the package requirements. @see ChunkUploadServiceProvider::registerConfig()
register
php
pionl/laravel-chunk-upload
src/Providers/ChunkUploadServiceProvider.php
https://github.com/pionl/laravel-chunk-upload/blob/master/src/Providers/ChunkUploadServiceProvider.php
MIT
protected function disk($diskName) { return Storage::disk($diskName); }
Returns disk name. @param string $diskName @return \Illuminate\Contracts\Filesystem\Filesystem
disk
php
pionl/laravel-chunk-upload
src/Providers/ChunkUploadServiceProvider.php
https://github.com/pionl/laravel-chunk-upload/blob/master/src/Providers/ChunkUploadServiceProvider.php
MIT
protected function registerConfig() { // Config options $configIndex = FileConfig::FILE_NAME; $configFileName = FileConfig::FILE_NAME.'.php'; $configPath = __DIR__.'/../../config/'.$configFileName; // Publish the config $this->publishes([ $configPath => config_path($configFileName), ]); // Merge the default config to prevent any crash or unfilled configs $this->mergeConfigFrom( $configPath, $configIndex ); return $this; }
Publishes and mergers the config. Uses the FileConfig. Registers custom handlers. @see FileConfig @see ServiceProvider::publishes @see ServiceProvider::mergeConfigFrom @return $this
registerConfig
php
pionl/laravel-chunk-upload
src/Providers/ChunkUploadServiceProvider.php
https://github.com/pionl/laravel-chunk-upload/blob/master/src/Providers/ChunkUploadServiceProvider.php
MIT
protected function registerHandlers(array $handlersConfig) { $overrideHandlers = Arr::get($handlersConfig, 'override', []); if (count($overrideHandlers) > 0) { HandlerFactory::setHandlers($overrideHandlers); return $this; } foreach (Arr::get($handlersConfig, 'custom', []) as $handler) { HandlerFactory::register($handler); } return $this; }
Registers handlers from config. @param array $handlersConfig @return $this
registerHandlers
php
pionl/laravel-chunk-upload
src/Providers/ChunkUploadServiceProvider.php
https://github.com/pionl/laravel-chunk-upload/blob/master/src/Providers/ChunkUploadServiceProvider.php
MIT
public function __construct($fileIndexOrFile, Request $request, $handlerClass, $chunkStorage = null, $config = null) { $this->request = $request; $this->file = is_object($fileIndexOrFile) ? $fileIndexOrFile : $request->file($fileIndexOrFile); $this->chunkStorage = is_null($chunkStorage) ? ChunkStorage::storage() : $chunkStorage; $this->config = is_null($config) ? AbstractConfig::config() : $config; if ($this->isUploaded()) { if (!$this->file->isValid()) { throw new UploadFailedException($this->file->getErrorMessage()); } $this->handler = new $handlerClass($this->request, $this->file, $this->config); } }
The file receiver for the given file index. @param string|UploadedFile $fileIndexOrFile the desired file index to use in request or the final UploadedFile @param Request $request the current request @param string $handlerClass the handler class name for detecting the file upload @param ChunkStorage|null $chunkStorage the chunk storage, on null will use the instance from app container @param AbstractConfig|null $config the config, on null will use the instance from app container @throws UploadFailedException
__construct
php
pionl/laravel-chunk-upload
src/Receiver/FileReceiver.php
https://github.com/pionl/laravel-chunk-upload/blob/master/src/Receiver/FileReceiver.php
MIT
public function isUploaded() { return is_object($this->file) && UPLOAD_ERR_NO_FILE !== $this->file->getError(); }
Checks if the file was uploaded. @return bool
isUploaded
php
pionl/laravel-chunk-upload
src/Receiver/FileReceiver.php
https://github.com/pionl/laravel-chunk-upload/blob/master/src/Receiver/FileReceiver.php
MIT
public function receive() { if (false === is_object($this->handler)) { return false; } return $this->handler->startSaving($this->chunkStorage, $this->config); }
Tries to handle the upload request. If the file is not uploaded, returns false. If the file is present in the request, it will create the save object. If the file in the request is chunk, it will create the `ChunkSave` object, otherwise creates the `SingleSave` which doesn't nothing at this moment. @return bool|AbstractSave
receive
php
pionl/laravel-chunk-upload
src/Receiver/FileReceiver.php
https://github.com/pionl/laravel-chunk-upload/blob/master/src/Receiver/FileReceiver.php
MIT
public function __construct(UploadedFile $file, AbstractHandler $handler, $config) { $this->file = $file; $this->handler = $handler; $this->config = $config; }
AbstractUpload constructor. @param UploadedFile $file the uploaded file (chunk file) @param AbstractHandler $handler the handler that detected the correct save method @param AbstractConfig $config the config manager
__construct
php
pionl/laravel-chunk-upload
src/Save/AbstractSave.php
https://github.com/pionl/laravel-chunk-upload/blob/master/src/Save/AbstractSave.php
MIT
public function isFinished() { return $this->isValid(); }
Checks if the file upload is finished. @return bool
isFinished
php
pionl/laravel-chunk-upload
src/Save/AbstractSave.php
https://github.com/pionl/laravel-chunk-upload/blob/master/src/Save/AbstractSave.php
MIT
public function isValid() { return $this->file->isValid(); }
Checks if the upload is valid. @return bool
isValid
php
pionl/laravel-chunk-upload
src/Save/AbstractSave.php
https://github.com/pionl/laravel-chunk-upload/blob/master/src/Save/AbstractSave.php
MIT
public function getErrorMessage() { return $this->file->getErrorMessage(); }
Returns the error message. @return string
getErrorMessage
php
pionl/laravel-chunk-upload
src/Save/AbstractSave.php
https://github.com/pionl/laravel-chunk-upload/blob/master/src/Save/AbstractSave.php
MIT